Does anybody know how to unselect ALL the rows on a Windows Forms DataGridView control? By default, the first row is always selected...
Also, I don't want to allow any kind of selection, do you guys know any method to this?I've already searched here but can't find...
Any help would be great!
Edit:
Ok, I've found another way to unselect a row: on the DataGridViewRow.RowPostPaint()
event, use the Selected
property to unselect the row who sent 开发者_开发问答the event.
private void grid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
this.gridLogEntries.Rows[e.RowIndex].Selected = false;
}
I don't think there's a simple solution like DisableSelection = true
.
Anyway, handling SelectionChanged
event in the following way, should be enough:
private bool skipEvents;
void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (skipEvents)
return;
skipEvents = true;
// disable cell selection
foreach (DataGridViewCell cell in this.dataGridView1.SelectedCells)
cell.Selected = false;
// disable row selection
foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
row.Selected = false;
// disable column selection
foreach (DataGridViewColumn col in this.dataGridView1.SelectedColumns)
col.Selected = false;
skipEvents = false;
}
EDIT:
I slightly changed the code to avoid recursive calls of the method.
datagridview1.clearselection()
This will fix the problem in just one statement, no need to loop through
try disabling the tabstop option, this worked for me on a simular issue
Have you tried this?
myTable.CurrentRowIndex = -1;
You could try calling the unselect method for each selected row:
for (int i = 0; i < bindingSource1.Count; i++)
{
if (myGrid.IsSelected(i))
{
myGrid.UnSelect(i);
}
}
精彩评论