其他分享
首页 > 其他分享> > .NET DataGridView 相关操作

.NET DataGridView 相关操作

作者:互联网

1) 使用 ReadOnly 属性 摇∪绻M珼ataGridView 内所有单元格都不可编辑, 那么只要:  [VB.NET]  ' 设置 DataGridView1 为只读  DataGridView1.ReadOnly = True [C#] // 设置 DataGridView1 为只读 DataGridView1.ReadOnly = true; 此时,用户的新增行操作和删除行操作也被屏蔽了。 摇∪绻M珼ataGridView 内某个单元格不可编辑, 那么只要: [VB.NET] ' 设置 DataGridView1 的第2列整列单元格为只读 DataGridView1.Columns(1).ReadOnly = True ' 设置 DataGridView1 的第3行整行单元格为只读 DataGridView1.Rows(2).ReadOnly = True ' 设置 DataGridView1 的[0,0]单元格为只读 DataGridView1(0, 0).ReadOnly = True [C#] // 设置 DataGridView1 的第2列整列单元格为只读 DataGridView1.Columns[1].ReadOnly = true; // 设置 DataGridView1 的第3行整行单元格为只读 DataGridView1.Rows[2].ReadOnly = true; // 设置 DataGridView1 的[0,0]单元格为只读 DataGridView1[0, 0].ReadOnly = true; 2) 使用 EditMode 属性 DataGridView.EditMode 属性被设置为 DataGridViewEditMode.EditProgrammatically 时,用户就不能手动编辑单元格的内容了。但是可以通过程序,调用 DataGridView.BeginEdit 方法,使单元格进入编辑模式进行编辑。 [VB.NET] DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically [C#] DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically; 3) 根据条件设定单元格的不可编辑状态 当一个一个的通过单元格坐标设定单元格 ReadOnly 属性的方法太麻烦的时候,你可以通过 CellBeginEdit 事件来取消单元格的编辑。 [VB.NET] 'CellBeginEdit 事件处理方法 Private Sub DataGridView1_CellBeginEdit(ByVal sender As Object, _         ByVal e As DataGridViewCellCancelEventArgs) _         Handles DataGridView1.CellBeginEdit     Dim dgv As DataGridView = CType(sender, DataGridView)     ' 是否可以进行编辑的条件检查     If dgv.Columns(e.ColumnIndex).Name = "Column1" AndAlso _         Not CBool(dgv("Column2", e.RowIndex).Value) Then         ' 取消编辑         e.Cancel = True     End If End Sub [C#] // CellBeginEdit 事件处理方法 private void DataGridView1_CellBeginEdit(object sender,     DataGridViewCellCancelEventArgs e) {     DataGridView dgv = (DataGridView)sender;     //是否可以进行编辑的条件检查     if (dgv.Columns[e.ColumnIndex].Name == "Column1" &&         !(bool)dgv["Column2", e.RowIndex].Value)     {         // 取消编辑         e.Cancel = true;     } } ________________________________________ ③ DataGridView  不显示最下面的新行: GO TO TOP 通常 DataGridView 的最下面一行是用户新追加的行(行头显示 * )。如果不想让用户新追加行即不想显示该新行,可以将 DataGridView 对象的 AllowUserToAddRows 属性设置为 False。 [VB.NET] ' 设置用户不能手动给 DataGridView1 添加新行 DataGridView1.AllowUserToAddRows = False [C#] // 设置用户不能手动给 DataGridView1 添加新行 DataGridView1.AllowUserToAddRows = false; 但是,可以通过程序: DataGridViewRowCollection.Add 为 DataGridView 追加新行。 补足: 如果 DataGridView 的 DataSource 绑定的是 DataView, 还可以通过设置 DataView.AllowAdd 属性为 False 来达到同样的效果。 ________________________________________ ④ DataGridView  判断新增行: GO TO TOP DataGridView的AllowUserToAddRows属性为True时也就是允许用户追加新行的场合下,DataGridView的最后一行就是新追加的行(*行)。使用 DataGridViewRow.IsNewRow 属性可以判断哪一行是新追加的行。另外,通过DataGridView.NewRowIndex 可以获取新行的行序列号。在没有新行的时候,NewRowIndex = -1。 [VB.NET] If DataGridView1.CurrentRow.IsNewRow Then     Console.WriteLine("当前行为新追加行。") Else     Console.WriteLine("当前行不是新追加行。") End If ________________________________________ ⑤ DataGridView  行的用户删除操作的自定义: GO TO TOP 1) 无条件的限制行删除操作。 默认时,DataGridView 是允许用户进行行的删除操作的。如果设置 DataGridView对象的AllowUserToDeleteRows属性为 False 时, 用户的行删除操作就被禁止了。 [VB.NET] ' 禁止DataGridView1的行删除操作。 DataGridView1.AllowUserToDeleteRows = False [C#] // 禁止DataGridView1的行删除操作。 DataGridView1.AllowUserToDeleteRows = false; 但是,通过 DataGridViewRowCollection.Remove 还是可以进行行的删除。 补足: 如果 DataGridView 绑定的是 DataView 的话,通过 DataView.AllowDelete 也可以控制行的删除。 2) 行删除时的条件判断处理。 用户在删除行的时候,将会引发 DataGridView.UserDeletingRow 事件。 在这个事件里,可以判断条件并取消删除操作。 [VB.NET] ' DataGridView1 的 UserDeletingRow 事件 Private Sub DataGridView1_UserDeletingRow(ByVal sender As Object, _         ByVal e As DataGridViewRowCancelEventArgs) _         Handles DataGridView1.UserDeletingRow     '  删除前的用户确认。     If MessageBox.Show("确认要删除该行数据吗?", "删除确认", _         MessageBoxButtons.OKCancel, MessageBoxIcon.Question) <> _             Windows.Forms.DialogResult.OK Then         '  如果不是 OK,则取消。         e.Cancel = True     End If End Sub [C#] // DataGridView1 的 UserDeletingRow 事件 private void DataGridView1_UserDeletingRow(     object sender, DataGridViewRowCancelEventArgs e) {     // 删除前的用户确认。     if (MessageBox.Show("确认要删除该行数据吗?", "删除确认",         MessageBoxButtons.OKCancel,         MessageBoxIcon.Question) != DialogResult.OK)     {         // 如果不是 OK,则取消。         e.Cancel = true;     } } 分类: C# 数据库

标签:删除,单元格,DataGridView,ReadOnly,DataGridView1,操作,NET
来源: https://www.cnblogs.com/devgis/p/16384966.html