Reputation: 239
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.
Upvotes: 8
Views: 55488
Reputation: 1336
On the DataGridView create an event for DataBindingComplete
then add this method datagridview1.ClearSelection()
private void datagridview1_DataBindingComplete(object sender, EventArgs e)
{
datagridview1.ClearSelection();
}
Upvotes: 16
Reputation: 13960
The only way is this:
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
dataGridView.ClearSelection();
}
Upvotes: 4
Reputation:
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;
}
Upvotes: 2
Reputation: 10422
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;
Upvotes: 10
Reputation: 9322
Set CurrentCell
Selected
property to False
like:
dataGridViewName.CurrentCell.Selected = false;
Upvotes: 16
Reputation: 697
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.
Upvotes: 0