Click here to Skip to main content
11,706,979 members (41,558 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hello,

I have this little problem.

I have datagridview and on CellClick event I have this code.
dg.Rows[e.RowIndex].Selected = true;
I use this event to select row on click on any cell, but now here comes the problem when I click on the Header of column to sort the column, I cant.

I get this error.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Anyone? Thanks in advance.
Posted 9-Nov-12 1:49am
Edited 9-Nov-12 5:14am
RaisKazi33.7K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

u can check for Header row in CellClick event and dont process for header row. Header row has index -1. so try this:
private void gv_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex != -1)
    {
        //Your code
    }
}
  Permalink  
v2
Comments
shonezi at 9-Nov-12 8:06am
   
thank you, works great
Gautam Raithatha at 9-Nov-12 8:44am
   
:)
Marcus Kramer at 9-Nov-12 11:56am
   
+5.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I changed code to this

dg.CurrentRow.Selected = true;

now it works, but still its on CellClick event, maybe something different has to be done if it should not be on CellClick event
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,

Cell click event is not good, you try to use Gridview RowCommand event is to be better
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 165
1 OriginalGriff 125
2 chainerlt 110
3 ppolymorphe 89
4 Mika Wendelius 70
0 OriginalGriff 9,178
1 Sergey Alexandrovich Kryukov 8,512
2 CPallini 5,189
3 Maciej Los 4,766
4 Mika Wendelius 3,696


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100