【C#】DataGridView中指定的单元格不能编辑

转自 http://blog.csdn.net/tangkechu/article/details/5703816

注意:DataGridView控件是从.NET Framework 2.0版本开始追加的。

ReadOnly属性的使用

DataGridView内所有的单元格不能编辑

当DataGridView.ReadOnly属性设定为True时, DataGridView内所有的单元格不能编辑。

但是使用这种方法可以对行进行删除。而且最下面的一行被表示,但不能输入。

[c-sharp]  view plain copy print ?
  1. // DataGridView1的单元格只读  
  2.  DataGridView1.ReadOnly = true;  

只有被指定的列、行、单元格不能编辑

只有被指定的列、行、单元格不能编辑时,通过设定DataGridViewColumn、DataGridViewRow、DataGridViewCell对象的ReadOnly属性为True即可实现。

[c-sharp]  view plain copy print ?
  1. //DataGridView1的第二列只读  
  2. DataGridView1.Columns[1].ReadOnly = true;  
  3.   
  4. //DataGridView1的第三行只读  
  5. DataGridView1.Rows[2].ReadOnly = true;  
  6.   
  7. //DataGridView1的(0, 0)的单元格只读  
  8. DataGridView1[0, 0].ReadOnly = true;  

DataGridView的ReadOnly设定为True时,DataGridView内的所有行、列、单元格的ReadOnly会自动设定为True。同样,列或行的ReadOnly设定为True时,所设定的列或行的所有的单元格的ReadOnly会自动设定为True。

EditMode属性的使用

当DataGridView.EditMode属性设定为DataGridViewEditMode.EditProgrammatically时,用户就不能对单元格进行编辑。但是,在程序中DataGridView.BeginEdit对象触发时,单元格变为可以进行编辑。

 

[c-sharp]  view plain copy print ?
  1. //用户不能编辑单元格  
  2.  DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;  

根据条件单元格不能编辑

这里所说的并不是每一个单元格的ReadOnly属性设定,而是使用CellBeginEdit事件处理器致使单元格不能编辑。如下面代码所示。

[c-sharp]  view plain copy print ?
  1. //CellBeginEdit事件处理器  
  2. private void DataGridView1_CellBeginEdit(object sender,  
  3.     DataGridViewCellCancelEventArgs e)  
  4. {  
  5.     DataGridView dgv = (DataGridView)sender;  
  6.     //判断是否可以编辑  
  7.     if (dgv.Columns[e.ColumnIndex].Name == "Column1" &&  
  8.         !(bool)dgv["Column2", e.RowIndex].Value)  
  9.     {  
  10.         //编辑不能  
  11.         e.Cancel = true;  
  12.     }  
  13. }  
  14.     

要实现DataGridView指定单元格的合并功能,可以使用DataGridView的CellPainting事件来自定义单元格的绘制。 以下是一个示例,演示如何将DataGridView指定单元格合并为一个单元格: ```csharp private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { // 指定要合并的单元格坐标 int mergeColumnIndex = 1; // 要合并的列索引 int mergeRowIndex = 1; // 要合并的行索引 // 只对指定单元格进行处理 if (e.RowIndex == mergeRowIndex && e.ColumnIndex == mergeColumnIndex) { // 获取指定单元格 DataGridViewCell curCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; // 获取要合并的单元格 DataGridViewCell mergeCell = dataGridView1.Rows[mergeRowIndex + 1].Cells[mergeColumnIndex]; // 判断指定单元格和要合并的单元格的值是否相同 if (curCell.Value != null && mergeCell.Value != null && curCell.Value.Equals(mergeCell.Value)) { // 要合并的单元格被合并到指定单元格 e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None; mergeCell.Visible = false; } } } ``` 在这个示例,我们指定了要合并的单元格的坐标,然后在CellPainting事件判断当前单元格是否为指定单元格。如果是,我们就获取要合并的单元格,判断其值是否和指定单元格的值相同。如果相同,我们将要合并的单元格的可见性设置为false,并将指定单元格的下边框样式设置为None,以达到合并单元格的效果。 需要注意的是,这种方法只能合并指定单元格和它下面的单元格,如果需要合并其他单元格,需要根据实际情况进行适当的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值