Click here to Skip to main content
15,886,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have this little problem.

I have datagridview and on CellClick event I have this code.
C#
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.
C#
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
Updated 9-Nov-12 5:14am
v2

u can check for Header row in CellClick event and dont process for header row. Header row has index -1. so try this:
C#
private void gv_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex != -1)
    {
        //Your code
    }
}
 
Share this answer
 
v2
Comments
shonezi 9-Nov-12 8:06am    
thank you, works great
Gautam Raithatha 9-Nov-12 8:44am    
:)
fjdiewornncalwe 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
 
Share this answer
 
Hi,

Cell click event is not good, you try to use Gridview RowCommand event is to be better
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900