6
votes

I have DataGridView to load some data programmatically. After inserting my data I am showing the DataGridView . Here by default the 1st row 0th column cell is selected. But I don't need that. I have tried to disable that option.

datagridviewname.currentcell=null   

But it will not work. Any body can help me to solve my problem.

6
What is the desired behaviour? - syclee

6 Answers

10
votes

Why u set it null? It should be like following. I think it will work

dataGridViewName.Rows[0].Cells[0].Selected = false;

if it is 1st row 0th, then

dataGridViewName.Rows[1].Cells[0].Selected = false;
15
votes

Set CurrentCell Selected property to False like:

   dataGridViewName.CurrentCell.Selected = false;
13
votes

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

private void datagridview1_DataBindingComplete(object sender, EventArgs e)
{
    datagridview1.ClearSelection();
}
3
votes

The only way is this:

private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
     dataGridView.ClearSelection();
}
2
votes

You should perform clear selection inside form load event of the form instead of constructor.

private void Form1_Load(object sender, EventArgs e)
{
 // You will get selectedCells count 1 here
 DataGridViewSelectedCellCollection selectedCells = dataGridView.SelectedCells;
 // Call clearSelection 
 dataGridView.ClearSelection();
 // Now You will get selectedCells count 0 here
 selectedCells = dataGridViewSchedule.SelectedCells;
}
0
votes

MSDN

You can set this property to null to temporarily remove the focus rectangle, but when the control receives focus and the value of this property is null, it is automatically set to the value of the FirstDisplayedCell property.

So looks like setting it to null only works if it wasn't the first row first column cell.