发布时间 : 星期一 文章超全经典DataGridView+编程36计更新完毕开始阅读a4ccc203eff9aef8951e0605
Handles DataGridView1.RowContextMenuStripNeeded Dim dgv As DataGridView = CType(sender, DataGridView)
' 当\列是Bool型且为True时、设定其的ContextMenuStrip Dim boolVal As Object = dgv(\ Console.WriteLine(boolVal)
If TypeOf boolVal Is Boolean AndAlso CBool(boolVal) Then e.ContextMenuStrip = Me.ContextMenuStrip1 End If End Sub [C#]
// RowContextMenuStripNeeded事件处理方法
private void DataGridView1_RowContextMenuStripNeeded(object sender, DataGridViewRowContextMenuStripNeededEventArgs e) {
DataGridView dgv = (DataGridView)sender;
// 当\列是Bool型且为True时、设定其的ContextMenuStrip object boolVal = dgv[\ Console.WriteLine(boolVal);
if (boolVal is bool && (bool)boolVal) {
e.ContextMenuStrip = this.ContextMenuStrip1; } }
CellContextMenuStripNeeded 事件处理方法的参数中、「e.ColumnIndex=-1」表示行头、「e.RowIndex=-1」表示列头。RowContextMenuStripNeeded则不存在「e.RowIndex=-1」的情况。
? DataGridView 的单元格的边框、 网格线样式的设定
GO TO TOP
1) DataGridView 的边框线样式的设定
DataGridView 的边框线的样式是通过 DataGridView.BorderStyle 属性来设定的。 BorderStyle 属性设定值是一个
BorderStyle 枚举: FixedSingle(单线,默认)、Fixed3D、None。
2) 单元格的边框线样式的设定
单元格的边框线的样式是通过 DataGridView.CellBorderStyle 属性来设定的。 CellBorderStyle 属性设定值是
DataGridViewCellBorderStyle 枚举。(详细参见 MSDN) 另外,通过 DataGridView.ColumnHeadersBorderStyle 和
RowHeadersBorderStyle 属性可以修改 DataGridView 的头部的单元格边框线样式。 属性设定值是 DataGridViewHeaderBorderStyle 枚举。(详细参见 MSDN)
3) 单元格的边框颜色的设定
单元格的边框线的颜色可以通过 DataGridView.GridColor 属性来设定的。默认是 ControlDarkDark 。但是只有在 CellBorderStyle 被设定为 Single、SingleHorizontal、SingleVertical 的条件下才能改变其边框线的颜色。同样,ColumnHeadersBorderStyle 以及 RowHeadersBorderStyle 只有在被设定为 Single 时,才能改变颜色。
4) 单元格的上下左右的边框线式样的单独设定
CellBorderStyle只能设定单元格全部边框线的式样。要单独改变单元格某一边边框式样的话,需要用到DataGridView.AdvancedCellBorderStyle属性。如示例:
[VB.NET]
' 单元格的上边和左边线设为二重线 ' 单元格的下边和右边线设为单重线
DataGridView1.AdvancedCellBorderStyle.Top = _
DataGridViewAdvancedCellBorderStyle.InsetDouble DataGridView1.AdvancedCellBorderStyle.Right = _ DataGridViewAdvancedCellBorderStyle.Inset DataGridView1.AdvancedCellBorderStyle.Bottom = _ DataGridViewAdvancedCellBorderStyle.Inset DataGridView1.AdvancedCellBorderStyle.Left = _
DataGridViewAdvancedCellBorderStyle.InsetDouble
同样,设定行头单元格的属性是: AdvancedRowHeadersBorderStyle, 设定列头单元格属性是:AdvancedColumnHeadersBorderStyle。
? DataGridView 单元格表示值的自定义
GO TO TOP
通过CellFormatting事件,可以自定义单元格的表示值。(比如:值为Error的时候,单元格被设定为红色)
下面的示例:将“Colmn1”列的值改为大写。 [VB.NET]
'CellFormatting 事件处理方法
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, _ ByVal e As DataGridViewCellFormattingEventArgs) _ Handles DataGridView1.CellFormatting
Dim dgv As DataGridView = CType(sender, DataGridView)
' 如果单元格是“Column1”列的单元格
If dgv.Columns(e.ColumnIndex).Name = \AndAlso _ TypeOf e.Value Is String Then ' 将单元格值改为大写
Dim str As String = e.Value.ToString()
e.Value = str.ToUpper()
' 应用该Format,Format完毕。 e.FormattingApplied = True End If End Sub [C#]
//CellFormatting 事件处理方法
private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
DataGridView dgv = (DataGridView)sender;
// 如果单元格是“Column1”列的单元格
if (dgv.Columns[e.ColumnIndex].Name == \is string) {
// 将单元格值改为大写
string str = e.Value.ToString(); e.Value = str.ToUpper();
// 应用该Format,Format完毕。 e.FormattingApplied = true; } }
CellFormatting事件的DataGridViewCellFormattingEventArgs对象的Value属性一开始保存着未被格式化的值。当Value属性被设定表示用的文本之后,把FormattingApplied属性做为True,告知DataGridView文本已经格式化完毕。如果不这样做的话,DataGridView会根据已经设定的Format,NullValue,DataSourceNullValue,FormatProvider属性会将Value属性会被重新格式化一遍。
? DataGridView 用户输入时,单元格输入值的设定
GO TO TOP
通过 DataGridView.CellParsing 事件可以设定用户输入的值。下面的示例:当输入英文文本内容的时候,立即被改变为大写。 [VB.NET]
'CellParsing 事件处理方法
Private Sub DataGridView1_CellParsing(ByVal sender As Object, _ ByVal e As DataGridViewCellParsingEventArgs) _ Handles DataGridView1.CellParsing
Dim dgv As DataGridView = CType(sender, DataGridView)
' 单元格列为“Column1”时
If dgv.Columns(e.ColumnIndex).Name = \AndAlso _ e.DesiredType Is GetType(String) Then ' 将单元格值设为大写
e.Value = e.Value.ToString().ToUpper() ' 解析完毕
e.ParsingApplied = True End If End Sub [C#]
//CellParsing 事件处理方法
private void DataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
DataGridView dgv = (DataGridView)sender;
//单元格列为“Column1”时
if (dgv.Columns[e.ColumnIndex].Name == \ e.DesiredType == typeof(string)) {
//将单元格值设为大写
e.Value = e.Value.ToString().ToUpper(); //解析完毕
e.ParsingApplied = true; } }
? DataGridView 新加行的默认值的设定
GO TO TOP
需要指定新加行的默认值的时候,可以在DataGridView.DefaultValuesNeeded事件里处理。在该事件中处理除了可以设定默认值以外,还可以指定某些特定的单元格的ReadOnly属性等。 [VB.NET]
' DefaultValuesNeeded 事件处理方法
Private Sub DataGridView1_DefaultValuesNeeded(ByVal sender As Object, _
ByVal e As DataGridViewRowEventArgs) _ Handles DataGridView1.DefaultValuesNeeded ' 设定单元格默认值
e.Row.Cells(\
e.Row.Cells(\End Sub [C#]
// DefaultValuesNeeded 事件处理方法
private void DataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e) {
// 设定单元格的默认值
e.Row.Cells[\ e.Row.Cells[\}