2
votes

I have a dataGridView control in Window Form Application. Three columns are added to the dataGridView. I added 20 empty rows into dataGridView in order to show some blank cells.(Otherwise dataGridView just shows grey area).

On dataGridView control, I add three column "a","b", "c". In form load, I add this,

dataGridView1.Rows.Add(20);

after form initialize, datagridview shows some blank cell, but first cell in first row is selected(highlighted with blue color). Please note, there is no data in datagridview. I could not figure out how to clear the default selection.

I did some research. There are some discussion. I tried, ,and they were not working. I think it is because I don't have data in the control. But the method mentioned in the following link, is for a datagridview loaded with data.

Disable default cell selection in datagridView

disable window gridview get focus on first row

Also there are some discussion regarding Windows store application. (I am using windows form application)

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/1dc26694-1147-4d5b-8b7d-11c9c493e605/how-to-disable-default-selection-in-gridview-

how to prevent autoselection of first item in GridView on databind?

Thanks in advance if you can give me some ideas.

5
No. Setting the Selected property of the said cell to false works just fine without any data. If you don't succeed (and I remember having had weird problems with that last year) do this: Set it to true for another cell and then back to false. It will work, promise!TaW
@Taw: Add this in form load : dataGridView3.Rows[0].Cells[0].Selected = false; Not working. Then try this, dataGridView3.Rows[0].Cells[1].Selected = true; dataGridView3.Rows[0].Cells[1].Selected = false; Still no working. Am I wrong somewhere?Apple Juice
Hm, I don't know. I think I set MultiSelect to false before and reset it afterwards..TaW
@Taw: BTW, even I try dataGridView3.Rows[0].Cells[0].Selected = false; ataGridView3.Rows[0].Cells[2].Selected = true; Still the first cell in first row is selected...Apple Juice
@Taw: OK, it is working after I set MultiSelect to false.Apple Juice

5 Answers

9
votes

I have used this code in my project's form's Shown-event:

dataGridView1.ClearSelection();
2
votes

DataGridView1.FirstDisplayedCell.Selected = False

1
votes

I can't reproduce in a fresh test DGV but I have seen it before.

Here is what I found to work:

bool old = dataGridView1.MultiSelect;
dataGridView1.MultiSelect = false;
dataGridView1.Rows[1].Cells[0].Selected = true;
dataGridView1.Rows[1].Cells[0].Selected = false;
dataGridView1.MultiSelect = old;

I don't know what causes this sometimes.. (..but I will add it to the answer once I do.)

0
votes

In my case, i got it working using this piece of code:

dataGridView1.EndEdit();

Hope this helps someone.

0
votes

If anyone is still having troubles with this I have found this worked for me when all the above suggestions failed.

On the DataGridView create an event for DataBindingComplete then add this method datagridview1.ClearSelection();