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

cdatagridviewwinforms

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.

Best Answer

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

dataGridView1.ClearSelection();
Related Topic