datagridview在vbnet中的操作技巧.
- 格式:doc
- 大小:57.50 KB
- 文档页数:13
DataGridView在中的操作技巧目录
1、 取得或者修改当前单元格的内容
2、 设定单元格只读
3、 不显示最下面的新行
4、 判断新增行
5、 行的用户删除操作的自定义
6行、列的隐藏和删除
7、 禁止列或者行的Resize
8、 列宽和行高以及列头的高度和行头的宽度的自动调整
9、 冻结列或行
10、 列顺序的调整
11、 行头列头的单元格
12、 剪切板的操作
13、 单元格的ToolTip的设置
14、 右键菜单(ContextMenuStrip的设置
15、 单元格的边框、网格线样式的设定
16、 单元格表示值的设定
17、 用户输入时,单元格输入值的设定 18、设定新加行的默认值DataGridView1.Curre ntCell = DataGridView1(0, 0
1、DataGridView取得或者修改当前单元格的内容
当前单元格指的是DataGridView焦点所在的单元格,它可以通过DataGridView 对象的Curre ntCell属性取得。如果当前单元格不存在的时候,返回Noth in g(C#是 null
[]
'取得当前单元格内容 MessageBox.Show(DataGridView1.Curre ntCell.Value
'取得当前单元格的列In dex
MessageBox.Show(DataGridView1.Curre ntCell.Colum nln dex
'取得当前单元格的行In dex
MessageBox.Show(DataGridView1.Curre ntCell.Rowl ndex
另外,使用DataGridView.CurrentCellAddress属性(而不是直接访问单元格来确 定单元格所在的行:DataGridView.CurrentCellAddress.Y 和 列:DataGridView.Curre
ntCellAddress.X。这对于避免取消共享行的共享非常有用。
当前的单元格可以通过设定 DataGridView对象的CurrentCell来改变。可以通 过CurrentCell来设定
DataGridView的激活单元格。将 Curre ntCell设为Noth in g( null可以取消激活 的单元格。[]
'设定(0, 0为当前单元格
2、DataGridView设定单元格只读:3、DataGridView不显示最下面的新行
1使用Read On ly属性
如果希望QataGridView内所有单元格都不可编辑,那么只要:
[]
'设置DataGridView1为只读
DataGridView1.ReadO nly = True
如果希望QataGridView内某个单元格不可编辑,那么只要:
[]
'设置DataGridView1的第2列整列单元格为只读
DataGridView1.Colum ns(1.Read Only = True
'设置DataGridView1的第3行整行单元格为只读
DataGridView1.Rows(2.Read Only = True
'设置DataGridView1的[0,0]单元格为只读
DataGridView1(0, O.Read Only = True
2使用EditMode属性
DataGridView.EditMode 属性被设置为
DataGridViewEditMode.EditProgrammatically时,用户就不能手动编辑单元格的内容
了。但是可以通过程序,调用DataGridView.Begin Edit方法,使单元格进入编辑模式 进行编辑。
[]
DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically3根据条件设定单元格的不可编辑状态
当一个一个的通过单元格坐标设定单元格 ReadO nly属性的方法太麻烦的时候
你可以通过CellBeg in Edit事件来取消单元格的编辑。
[]
'CellBegi nEdit事件处理方法
Private Sub DataGridView1_CellBegi nEdit(ByVal sen der As Object, _
ByVal e As DataGridViewCellCa ncelEve ntArgs _
Han dles DataGridView1.CellBegi nEdit
Dim dgv As DataGridView = CType(se nder, DataGridView
'是否可以进行编辑的条件检查
If dgv.Colum ns(e.Colu mnln = "Colu mn 1" A ndAlso _
Not CBool(dgv("Colum n2", e.Row In dex.Value The n
'取消编辑
e.Ca ncel = True
End If
End Sub3、DataGridView不显示最下面的新行
通常DataGridView的最下面一行是用户新追加的行(行头显示*。如果不想让 用户新追加行即不想显示该新行,可以将DataGridView对象的
AllowUserToAddRows 属性设置为 Falsa
[]
'设置用户不能手动给DataGridView1添加新行
DataGridView1.AllowUserToAddRows = False
补足:如果DataGridView的DataSource绑定的是 DataView,还可以通过设置
DataView.AllowAdd
属性为False来达到同样的效果。
4、DataGridView判断新增行:
DataGridView的AllowUserToAddRows属性为True时也就是允许用户追加新 行的场合下,DataGridView的最后一行就是新追加的行(*行。使用
DataGridViewRow.IsNewRow属性可以判断哪一行是新追加的行。另外,通过
DataGridView.NewRowI ndex可以获取新行的行序列号。在没有新行的时
候,NewRowIndex = -1。[]
If DataGridView1.Curre ntRow.lsNewRow The n
Con sole.WriteL in e("当前行为新追加行。"
Else
Co nsole.WriteLi ne(”当前行不是新追加行。"
End IfByVal e As DataGridViewRowCa ncelEve ntArgs _
5、DataGridView行的用户删除操作的自定义:
1无条件的限制行删除操作。
默认时QataGridView是允许用户进行行的删除操作的。如果设置
DataGridView对象的AllowUserToDeleteRows属性为False时,用户的行删除操作就 被禁止了。
[]
'禁止DataGridView1的行删除操作。
DataGridView1.AllowUserToDeleteRows = False
但是,通过DataGridViewRowCollection.Remove还是可以进行行的删除。
补足:如果 DataGridView 绑定的是 DataView 的话通过 DataView.AllowDelete
也可以控制行的删除。
2行删除时的条件判断处理。
用户在删除行的时候,将会引发erDeletingRow事件。在这个
事件里,可以判断条件并取消删除操作。
[]
'DataGridView1 的 UserDeletingRow 事件
Private Sub DataGridView1_UserDelet in gRow(ByVal sen der As Object, _
Han dles erDelet in gRow'删除前的用户确认。
If MessageBox.ShowC确认要删除该行数据吗?","删除确认",_ MessageBoxButt on
s.OKCa ncel, MessageBoxIc on. Questio n <> _ Win dows.Forms.DialogResult.OK
The n
'如果不是OK,则取消。
e.Ca ncel = True
End If
End Sub
6、DataGridView行、列的隐藏和删除:
1行、列的隐藏
[]
'DataGridView1的第一列隐藏
DataGridView1.Colu mn s(O.Visible = False
'DataGridView1的第一行隐藏
DataGridView1.Rows(0.Visible = False
2行头、列头的隐藏 []
'列头隐藏
DataGridViewl.Colum nH eadersVisible = False
'行头隐藏
DataGridViewI.RowHeadersVisible = False
3行和列的删除
[]
'删除名为"Column1"的列
DataGridView1.Colum ns.R emove("Colum n1"
'删除第一列
DataGridView1.Colum ns. RemoveAt(0
'删除第一行
DataGridView1.Rows.RemoveAt(0
4删除选中行
[]
For Each r As DataGridViewRow In DataGridView1.SelectedRows
If Not r.IsNewRow The n
DataGridView1.Rows.Remove(r
End If