c#winforms - 如何使DataGridViewRow UnSelectable?

时间:2012-06-06 20:45:03

标签: c# winforms datagridview

有没有办法阻止用户选择特定dgvRow中的单元格 Row是ReadOnly,但仍然可以选择。

1 个答案:

答案 0 :(得分:2)

您可以添加自定义委托,以便在更改选择时取消选择相关单元格。

DataGridView1.SelectionChanged += new EventHandler(DataGridView1_SelectionChanged);
private void DataGridView1_SelectionChanged(object sender, EventArgs e){
    List<DataGridViewCell> toBeRemoved = new List<DataGridViewCell>();
    foreach(DataGridViewCell selectedCell in DataGridView1.SelectedCells){
        if (isCellUnSelectable(selectedCell))
            toBeRemoved.Add(selectedCell);
    }
    foreach(DataGridViewCell cell in toBeRemoved){
        cell.Selected = false;
    }
}