Click here to Skip to main content
Sign Up to vote bad
good
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
shonezi844
Edited 9-Nov-12 5:14am
RaisKazi30.1K


3 solutions

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  
Comments
shonezi - 9-Nov-12 8:06am
thank you, works great
Gautam Raithatha - 9-Nov-12 8:44am
:)
Marcus Kramer - 9-Nov-12 11:56am
+5.
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  
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 6,959
1 Prasad_Kulkarni 3,689
2 OriginalGriff 3,402
3 _Amy 3,332
4 CPallini 2,950


Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid