Click here to Skip to main content
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 2:49am
Edited 9-Nov-12 6:14am
RaisKazi33.1K
v2
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 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 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 535
1 OriginalGriff 275
2 Shweta N Mishra 244
3 Mathew Soji 225
4 BillWoodruff 220
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
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