DataGridView 的右键菜单(ContextMenuStrip)
DataGridView, DataGridViewColumn, DataGridViewRow, DataGridViewCell 有 ContextMenuStrip 属性。可以通过设定 ContextMenuStrip 对象来控制 DataGridView 的右键菜单的显示。 DataGridViewColumn 的 ContextMenuStrip 属性设定了 除了列头以外的单元格的右键菜单。 DataGridViewRow 的 ContextMenuStrip 属性设定了除了行头以外的单元格的右键菜单。DataGridViewCell 的 ContextMenuStrip 属性设定了指定单元格的右键菜单。
[VB.NET]
' DataGridView 的 ContextMenuStrip 设定
DataGridView1.ContextMenuStrip = Me.ContextMenuStrip1
' 列的 ContextMenuStrip 设定
DataGridView1.Columns(0).ContextMenuStrip = Me.ContextMenuStrip2
' 列头的 ContextMenuStrip 设定
DataGridView1.Columns(0).HeaderCell.ContextMenuStrip = Me.ContextMenuStrip2
' 行的 ContextMenuStrip 设定
DataGridView1.Rows(0).ContextMenuStrip = Me.ContextMenuStrip3
' 单元格的 ContextMenuStrip 设定
DataGridView1(0, 0).ContextMenuStrip = Me.ContextMenuStrip4
' DataGridView 的 ContextMenuStrip 设定
DataGridView1.ContextMenuStrip = Me.ContextMenuStrip1
' 列的 ContextMenuStrip 设定
DataGridView1.Columns(0).ContextMenuStrip = Me.ContextMenuStrip2
' 列头的 ContextMenuStrip 设定
DataGridView1.Columns(0).HeaderCell.ContextMenuStrip = Me.ContextMenuStrip2
' 行的 ContextMenuStrip 设定
DataGridView1.Rows(0).ContextMenuStrip = Me.ContextMenuStrip3
' 单元格的 ContextMenuStrip 设定
DataGridView1(0, 0).ContextMenuStrip = Me.ContextMenuStrip4
[C#]
// DataGridView 的 ContextMenuStrip 设定
DataGridView1.ContextMenuStrip = this.ContextMenuStrip1;
// 列的 ContextMenuStrip 设定
DataGridView1.Columns[0].ContextMenuStrip = this.ContextMenuStrip2;
// 列头的 ContextMenuStrip 设定
DataGridView1.Columns[0].HeaderCell.ContextMenuStrip = this.ContextMenuStrip2;
// 行的 ContextMenuStrip 设定
DataGridView1.Rows[0].ContextMenuStrip = this.ContextMenuStrip3;
// 单元格的 ContextMenuStrip 设定
DataGridView1[0, 0].ContextMenuStrip = this.ContextMenuStrip4;
// DataGridView 的 ContextMenuStrip 设定
DataGridView1.ContextMenuStrip = this.ContextMenuStrip1;
// 列的 ContextMenuStrip 设定
DataGridView1.Columns[0].ContextMenuStrip = this.ContextMenuStrip2;
// 列头的 ContextMenuStrip 设定
DataGridView1.Columns[0].HeaderCell.ContextMenuStrip = this.ContextMenuStrip2;
// 行的 ContextMenuStrip 设定
DataGridView1.Rows[0].ContextMenuStrip = this.ContextMenuStrip3;
// 单元格的 ContextMenuStrip 设定
DataGridView1[0, 0].ContextMenuStrip = this.ContextMenuStrip4;
对于单元格上的右键菜单的设定,优先顺序是: Cell > Row > Column > DataGridView
⇒ CellContextMenuStripNeeded、RowContextMenuStripNeeded 事件
利用 CellContextMenuStripNeeded 事件可以设定单元格的右键菜单,尤其但需要右键菜单根据单元格值的变化而变化的时候。比起使用循环遍历,使用该事件来设定右键菜单的效率更高。但是,在DataGridView使用了DataSource绑定而且是VirtualMode的时候,该事件将不被引发。
[VB.NET]
' CellContextMenuStripNeeded事件处理方法
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
' 列头的ContextMenuStrip设定
e.ContextMenuStrip = Me.ContextMenuStrip1
ElseIf e.ColumnIndex < 0 Then
' 行头的ContextMenuStrip设定
e.ContextMenuStrip = Me.ContextMenuStrip2
ElseIf TypeOf (dgv(e.ColumnIndex, e.RowIndex).Value) Is Integer Then
' 如果单元格值是整数时
e.ContextMenuStrip = Me.ContextMenuStrip3
End If
End Sub
' CellContextMenuStripNeeded事件处理方法
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
' 列头的ContextMenuStrip设定
e.ContextMenuStrip = Me.ContextMenuStrip1
ElseIf e.ColumnIndex < 0 Then
' 行头的ContextMenuStrip设定
e.ContextMenuStrip = Me.ContextMenuStrip2
ElseIf TypeOf (dgv(e.ColumnIndex, e.RowIndex).Value) Is Integer Then
' 如果单元格值是整数时
e.ContextMenuStrip = Me.ContextMenuStrip3
End If
End Sub
[C#]
// CellContextMenuStripNeeded事件处理方法
private void DataGridView1_CellContextMenuStripNeeded(object sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (e.RowIndex < 0)
{
// 列头的ContextMenuStrip设定
e.ContextMenuStrip = this.ContextMenuStrip1;
}
else if (e.ColumnIndex < 0)
{
// 行头的ContextMenuStrip设定
e.ContextMenuStrip = this.ContextMenuStrip2;
}
else if (dgv[e.ColumnIndex, e.RowIndex].Value is int)
{
// 如果单元格值是整数时
e.ContextMenuStrip = this.ContextMenuStrip3;
}
}
// CellContextMenuStripNeeded事件处理方法
private void DataGridView1_CellContextMenuStripNeeded(object sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (e.RowIndex < 0)
{
// 列头的ContextMenuStrip设定
e.ContextMenuStrip = this.ContextMenuStrip1;
}
else if (e.ColumnIndex < 0)
{
// 行头的ContextMenuStrip设定
e.ContextMenuStrip = this.ContextMenuStrip2;
}
else if (dgv[e.ColumnIndex, e.RowIndex].Value is int)
{
// 如果单元格值是整数时
e.ContextMenuStrip = this.ContextMenuStrip3;
}
}
同样,可以通过 RowContextMenuStripNeeded 事件来设定行的右键菜单。
[VB.NET]
' RowContextMenuStripNeeded事件处理方法
Private Sub DataGridView1_RowContextMenuStripNeeded( _
ByVal sender As Object, _
ByVal e As DataGridViewRowContextMenuStripNeededEventArgs) _
Handles DataGridView1.RowContextMenuStripNeeded
Dim dgv As DataGridView = CType(sender, DataGridView)
' 当"Column1"列是Bool型且为True时、设定其的ContextMenuStrip
Dim boolVal As Object = dgv("Column1", e.RowIndex).Value
Console.WriteLine(boolVal)
If TypeOf boolVal Is Boolean AndAlso CBool(boolVal) Then
e.ContextMenuStrip = Me.ContextMenuStrip1
End If
End Sub
' RowContextMenuStripNeeded事件处理方法
Private Sub DataGridView1_RowContextMenuStripNeeded( _
ByVal sender As Object, _
ByVal e As DataGridViewRowContextMenuStripNeededEventArgs) _
Handles DataGridView1.RowContextMenuStripNeeded
Dim dgv As DataGridView = CType(sender, DataGridView)
' 当"Column1"列是Bool型且为True时、设定其的ContextMenuStrip
Dim boolVal As Object = dgv("Column1", e.RowIndex).Value
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;
// 当"Column1"列是Bool型且为True时、设定其的ContextMenuStrip
object boolVal = dgv["Column1", e.RowIndex].Value;
Console.WriteLine(boolVal);
if (boolVal is bool && (bool)boolVal)
{
e.ContextMenuStrip = this.ContextMenuStrip1;
}
}
// RowContextMenuStripNeeded事件处理方法
private void DataGridView1_RowContextMenuStripNeeded(object sender,
DataGridViewRowContextMenuStripNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
// 当"Column1"列是Bool型且为True时、设定其的ContextMenuStrip
object boolVal = dgv["Column1", e.RowIndex].Value;
Console.WriteLine(boolVal);
if (boolVal is bool && (bool)boolVal)
{
e.ContextMenuStrip = this.ContextMenuStrip1;
}
}
CellContextMenuStripNeeded 事件处理方法的参数中、「e.ColumnIndex=-1」表示行头、「e.RowIndex=-1」表示列头。RowContextMenuStripNeeded则不存在「e.RowIndex=-1」的情况。