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.
Also there are some discussion regarding Windows store application. (I am using windows form application)
Thanks in advance if you can give me some ideas.
Talk1:
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!
Talk2:
: 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?
Talk3:
Hm, I don't know. I think I set MultiSelect to false before and reset it afterwards..
Talk4:
: 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...
Talk5:
: OK, it is working after I set MultiSelect to false.
Solutions1
I have used this code in my project's form's Shown-event:
dataGridView1.ClearSelection();
Talk1:
u r a lifesaver man!!!
Solutions2
DataGridView1.FirstDisplayedCell.Selected = False
Solutions3
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.)
Solutions4
In my case, i got it working using this piece of code:
dataGridView1.EndEdit();
Hope this helps someone.
Solutions5
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();