C# .NET - How to deselect the first row when the form i loaded in DataGridView in Windows Application?

Asked By Eswaran Radhakrishnan on 26-Nov-08 03:23 AM

Hi all,

I am displaying the records in DataGridView in Form Load event in Windows Application.  My problem is after binding all the value it's take the selected index =0 (the first row) even I did not select any row from the DataGridView. After the data is loaded in DataGridView the first is selected automatically and Selected Index =0. 

 I used the "dataGridViewProducts.ClearSelection()" in my load event. But it hides the default selection while the form is loaded but I could not prevent the Selected Index=0.

How to do that?

Thanks

R. Eswaran.

Load Event

 

did you try this - C_A P replied to Eswaran Radhakrishnan on 26-Nov-08 03:44 AM

did you try
DataGridView1.Rows(0).Selected = False

reply - Binny ch replied to Eswaran Radhakrishnan on 26-Nov-08 04:03 AM

'unselect previously selected row
DataGrid1.UnSelect(DataGrid1.CurrentRowIndex)

To get the first deselected:
DataGridView1.Rows(0).Selected = False

still the selected Index is 0 - Eswaran Radhakrishnan replied to Binny ch on 26-Nov-08 04:32 AM

Hi,

It's unselect the first row after binding the value, but still the selectedIndex  is  0 . I want to set the selectedIndex = -1 after the binding is finished in load event.

Any other idea...

Thanks

R. Eswaran.


ClearSelection will work. - [)ia6l0 iii replied to Eswaran Radhakrishnan on 26-Nov-08 08:19 AM

I think you are doing something else to the DataGridView after the Clear Selection.

Otherwise, clear Selection is for this very purpose.



Noorashegh replied to Eswaran Radhakrishnan on 30-May-11 01:50 PM

Hi

I answer this question for another visitors that have same problem.

you can deselect rows in form1.shown event or in datagridview.RowPostPaint event like this:

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
   if (e.IsLastVisibleRow)
   dataGridView1.Rows[0].Selected = false;
}

if you refresh datagridview in your program and you want to select last selected rows you can use int variable for save index of before refresh. 

Vineet replied to Eswaran Radhakrishnan on 01-Aug-11 05:10 AM
Put this in Form_Paint
DataGridView1.Rows[0].Selected=false