Owen Lee
Owen Lee

Reputation: 405

Disable default cell selection in a datagridview after form load in WinForm Application

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.

Upvotes: 3

Views: 15913

Answers (5)

reversebind
reversebind

Reputation: 1316

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();

Upvotes: 4

Sameer
Sameer

Reputation: 71

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

dataGridView1.EndEdit();

Hope this helps someone.

Upvotes: 0

Luiz F C Leite
Luiz F C Leite

Reputation: 21

DataGridView1.FirstDisplayedCell.Selected = False

Upvotes: 2

W0lfw00ds
W0lfw00ds

Reputation: 2086

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

dataGridView1.ClearSelection();

Upvotes: 11

TaW
TaW

Reputation: 54433

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.)

Upvotes: 1

Related Questions