C# DataGridView checkbox independant from row selection

瘦欲@ 提交于 2020-01-04 13:46:15

问题


thx in advance for the help... I have a datagridview (c# Winforms) with a column of checkboxes. When any of these are clicked, it automatically selects the row. (Although not th eother way around). How do I de-couple row selection from the checcking of the checkbox ? In other words, I want to enable multiple row selection, without effecting the corresponding checkboxes, and also click multiple checkboxes without "automatically selecting" the rows where the checkboxes are "checked" ? ~Ron


回答1:


I am not certain if this is what you are looking for, but hopefully it will point you in the right direction. If you want to make certain that no rows are selected when you edit the value of a cell you can handle the CellBeginEdit event of the DataGridView.

this.dataGridView1.CellBeginEdit += new System.Windows.Forms.DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);

void dataGridView1_CellBeginEdit(object sender, System.Windows.Forms.DataGridViewCellCancelEventArgs e)
{
 dataGridView1.ClearSelection();
}

If you want to maintain the existing selection you will need to implement a more complex handler, such as saving the indexes of the currently selected rows and restoring them after.



来源:https://stackoverflow.com/questions/1405849/c-sharp-datagridview-checkbox-independant-from-row-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!