文章目录
  1. 1. 控制按钮列中的某一行不显示按钮。
  2. 2. 控制某单元格是否允许修改。

控制按钮列中的某一行不显示按钮。

屏蔽的方法:把按钮改成普通的单元格。参考代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//定义绘画表格前的事件,在绘画前把按钮转换成普通单元格。
dataGrid.RowPrePaint += dataGrid_RowPrePaint;
//控制第一行和最后一行的按钮不显示。
private void dataGrid_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
if (e.RowIndex == 0 e.RowIndex == dataGrid.RowCount - 1)
{
if (dataGrid[7, e.RowIndex] is DataGridViewButtonCell)
{
dataGrid[7, e.RowIndex] = new DataGridViewTextBoxCell();
dataGrid[7, e.RowIndex].Value = string.Empty;
}
}
}

控制某单元格是否允许修改。

由于没有单独控制某个单元格的ReadOnly属性,因此,只能通过技巧来控制了。原理其实是判断进入的单元格时判断做逻辑处理,对于允许修改的,就把整个表格的ReadOnly设为false,否则就设为 true

1
2
3
4
5
6
7
8
9
10
private void dataGrid_RowEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == dataGrid.RowCount - 1)
dataGrid.ReadOnly = true;
else
dataGrid.ReadOnly = false;

dataGrid.Columns[0].ReadOnly = true;
dataGrid.Columns[3].ReadOnly = true;
}

参考来源:https://blog.csdn.net/kfarvid/article/details/7280427

文章目录
  1. 1. 控制按钮列中的某一行不显示按钮。
  2. 2. 控制某单元格是否允许修改。