19.DataGridView中的ContextMenuStrip属性
[VB.NET]
DataGridView1.ContextMenuStrip = Me.ContextMenuStrip1
DataGridView1.Columns(0).ContextMenuStrip = Me.ContextMenuStrip2
DataGridView1.Columns(0).HeaderCell.ContextMenuStrip = Me.ContextMenuStrip2
DataGridView1.Rows(0).ContextMenuStrip = Me.ContextMenuStrip3
DataGridView1(1, 0).ContextMenuStrip = Me.ContextMenuStrip4
[C#]
DataGridView1.ContextMenuStrip = this.ContextMenuStrip1;
DataGridView1.Columns[0].ContextMenuStrip = this.ContextMenuStrip2;
DataGridView1.Columns[0].HeaderCell.ContextMenuStrip = this.ContextMenuStrip2;
DataGridView1.Rows[0].ContextMenuStrip = this.ContextMenuStrip3;
DataGridView1[0, 1].ContextMenuStrip = this.ContextMenuStrip4;
也可以用CellContextMenuStripNeeded、RowContextMenuStripNeeded属性进行定义
[VB.NET]
Private Sub DataGridView1_CellContextMenuStripNeeded( _
ByVal sender As Object, _
ByVal e As DataGridViewCellContextMenuStripNeededEventArgs) _
Handles DataGridView1.CellContextMenuStripNeeded
Dim dgv As DataGridView = CType(sender, DataGridView)
If e.RowIndex < 0 Then
e.ContextMenuStrip = Me.ContextMenuStrip1
ElseIf e.ColumnIndex < 0 Then
e.ContextMenuStrip = Me.ContextMenuStrip2
ElseIf TypeOf (dgv(e.ColumnIndex, e.RowIndex).Value) Is Integer Then
e.ContextMenuStrip = Me.ContextMenuStrip3
End If
End Sub
[C#]
private void DataGridView1_CellContextMenuStripNeeded(object sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (e.RowIndex < 0)
{
e.ContextMenuStrip = this.ContextMenuStrip1;
}
else if (e.ColumnIndex < 0)
{
e.ContextMenuStrip = this.ContextMenuStrip2;
}
else if (dgv[e.ColumnIndex, e.RowIndex].Value is int)
{
e.ContextMenuStrip = this.ContextMenuStrip3;
}
}
20. DataGridView指定滚动框位置
[VB.NET]
DataGridView1.FirstDisplayedScrollingRowIndex = 0
DataGridView1.FirstDisplayedScrollingColumnIndex = 0
[C#]
DataGridView1.FirstDisplayedScrollingRowIndex = 0;
DataGridView1.FirstDisplayedScrollingColumnIndex = 0;
21. DataGridView手动追加列
[VB.NET]
DataGridView1.AutoGenerateColumns = False
DataGridView1.DataSource = BindingSource1
Dim textColumn As New DataGridViewTextBoxColumn()
textColumn.DataPropertyName = "Column1"
textColumn.Name = "Column1"
textColumn.HeaderText = "Column1"
DataGridView1.Columns.Add(textColumn)
[C#]
DataGridView1.AutoGenerateColumns = false;
DataGridView1.DataSource = BindingSource1;
DataGridViewTextBoxColumn textColumn = new DataGridViewTextBoxColumn();
textColumn.DataPropertyName = "Column1";
textColumn.Name = "Column1";
textColumn.HeaderText = "Column1";
DataGridView1.Columns.Add(textColumn);
22. DataGridView全体分界线样式设置
[VB.NET]
DataGridView1.BorderStyle = BorderStyle.Fixed3D
[C#]
DataGridView1.BorderStyle = BorderStyle.Fixed3D;
单元格上下左右分界线样式设置
[VB.NET]
DataGridView1.AdvancedCellBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.InsetDouble
DataGridView1.AdvancedCellBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Inset
DataGridView1.AdvancedCellBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Inset
DataGridView1.AdvancedCellBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.InsetDouble
[C#]
DataGridView1.AdvancedCellBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.InsetDouble;
DataGridView1.AdvancedCellBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Inset;
DataGridView1.AdvancedCellBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Inset;
DataGridView1.AdvancedCellBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.InsetDouble;
23. DataGridView根据单元格属性更改显示内容
如下例,当该列是字符串时,自动转换文字大小写
[VB.NET]
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, _
ByVal e As DataGridViewCellFormattingEventArgs) _
Handles DataGridView1.CellFormatting
Dim dgv As DataGridView = CType(sender, DataGridView)
If dgv.Columns(e.ColumnIndex).Name = "Column1" AndAlso _
TypeOf e.Value Is String Then
Dim str As String = e.Value.ToString()
e.Value = str.ToUpper()
e.FormattingApplied = True
End If
End Sub
[C#]
private void DataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns[e.ColumnIndex].Name == "Column1" && e.Value is string)
{
string str = e.Value.ToString();
e.Value = str.ToUpper();
e.FormattingApplied = true;
}
}
24. DataGridView新追加行的行高样式设置
行高设置
[VB.NET]
DataGridView1.RowTemplate.Height = 50
DataGridView1.RowTemplate.MinimumHeight = 50
[C#]
DataGridView1.RowTemplate.Height = 50;
DataGridView1.RowTemplate.MinimumHeight = 50;
样式设置
[VB.NET]
DataGridView1.DefaultCellStyle.BackColor = Color.Yellow
[C#]
DataGridView1.DefaultCellStyle.BackColor = Color.Yellow;
25. DataGridView新追加行单元格默认值设置
[VB.NET]
Private Sub DataGridView1_DefaultValuesNeeded(ByVal sender As Object, _
ByVal e As DataGridViewRowEventArgs) _
Handles DataGridView1.DefaultValuesNeeded
e.Row.Cells("Column1").Value = 0
e.Row.Cells("Column2").Value = "-"
End Sub
[C#]
private void DataGridView1_DefaultValuesNeeded(object sender,
DataGridViewRowEventArgs e)
{
e.Row.Cells["Column1"].Value = 0;
e.Row.Cells["Column2"].Value = "-";
}
欢迎微信技术交流: