DataGridViewCellContextMenuStripNeededEventHandler 委托
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示将处理 CellContextMenuStripNeeded 的 DataGridView 事件的方法。
public delegate void DataGridViewCellContextMenuStripNeededEventHandler(System::Object ^ sender, DataGridViewCellContextMenuStripNeededEventArgs ^ e);
public delegate void DataGridViewCellContextMenuStripNeededEventHandler(object sender, DataGridViewCellContextMenuStripNeededEventArgs e);
public delegate void DataGridViewCellContextMenuStripNeededEventHandler(object? sender, DataGridViewCellContextMenuStripNeededEventArgs e);
type DataGridViewCellContextMenuStripNeededEventHandler = delegate of obj * DataGridViewCellContextMenuStripNeededEventArgs -> unit
Public Delegate Sub DataGridViewCellContextMenuStripNeededEventHandler(sender As Object, e As DataGridViewCellContextMenuStripNeededEventArgs)
参数
- sender
- Object
事件源。
示例
下面的代码示例使用 DataGridViewCellContextMenuStripNeededEventArgs 设置上下文菜单,而不取消共享行。
ToolStripMenuItem^ wholeTable;
ToolStripMenuItem^ lookUp;
System::Windows::Forms::ContextMenuStrip^ strip;
String^ cellErrorText;
void dataGridView1_CellContextMenuStripNeeded( Object^ /*sender*/,
DataGridViewCellContextMenuStripNeededEventArgs^ e )
{
cellErrorText = String::Empty;
if ( strip == nullptr )
{
strip = gcnew System::Windows::Forms::ContextMenuStrip;
lookUp->Text = L"Look Up";
wholeTable->Text = L"See Whole Table";
strip->Items->Add( lookUp );
strip->Items->Add( wholeTable );
}
e->ContextMenuStrip = strip;
}
void wholeTable_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
dataGridView1->DataSource = Populate( L"Select * from employees", true );
}
DataGridViewCellEventArgs^ theCellImHoveringOver;
void dataGridView1_CellMouseEnter( Object^ /*sender*/, DataGridViewCellEventArgs^ e )
{
theCellImHoveringOver = e;
}
DataGridViewCellEventArgs^ cellErrorLocation;
void lookUp_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
try
{
dataGridView1->DataSource = Populate( String::Format( L"Select * from employees where {0} = '{1}'", dataGridView1->Columns[ theCellImHoveringOver->ColumnIndex ]->Name, dataGridView1->Rows[ theCellImHoveringOver->RowIndex ]->Cells[ theCellImHoveringOver->ColumnIndex ]->Value ), true );
}
catch ( ... )
{
cellErrorText = L"Can't look this cell up";
cellErrorLocation = theCellImHoveringOver;
}
}
void dataGridView1_CellErrorTextNeeded( Object^ /*sender*/, DataGridViewCellErrorTextNeededEventArgs^ e )
{
if ( cellErrorLocation != nullptr )
{
if ( e->ColumnIndex == cellErrorLocation->ColumnIndex && e->RowIndex == cellErrorLocation->RowIndex )
{
e->ErrorText = cellErrorText;
}
}
}
DataTable^ Populate( String^ query, bool resetUnsharedCounter )
{
if ( resetUnsharedCounter )
{
ResetCounter();
}
// Alter the data source as necessary
SqlDataAdapter^ adapter = gcnew SqlDataAdapter( query,
gcnew SqlConnection( L"Integrated Security=SSPI;Persist Security Info=False;"
L"Initial Catalog=Northwind;Data Source= localhost" ) );
DataTable^ table = gcnew DataTable;
adapter->Fill( table );
return table;
}
Label^ count;
int unsharedRowCounter;
void ResetCounter()
{
unsharedRowCounter = 0;
count->Text = unsharedRowCounter.ToString();
}
private ToolStripMenuItem wholeTable = new ToolStripMenuItem();
private ToolStripMenuItem lookUp = new ToolStripMenuItem();
private ContextMenuStrip strip;
private string cellErrorText;
private void dataGridView1_CellContextMenuStripNeeded(object sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
cellErrorText = String.Empty;
if (strip == null)
{
strip = new ContextMenuStrip();
lookUp.Text = "Look Up";
wholeTable.Text = "See Whole Table";
strip.Items.Add(lookUp);
strip.Items.Add(wholeTable);
}
e.ContextMenuStrip = strip;
}
private void wholeTable_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = Populate("Select * from employees", true);
}
private DataGridViewCellEventArgs theCellImHoveringOver;
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
theCellImHoveringOver = e;
}
private DataGridViewCellEventArgs cellErrorLocation;
private void lookUp_Click(object sender, EventArgs e)
{
try
{
dataGridView1.DataSource = Populate("Select * from employees where " +
dataGridView1.Columns[theCellImHoveringOver.ColumnIndex].Name + " = '" +
dataGridView1.Rows[theCellImHoveringOver.RowIndex].
Cells[theCellImHoveringOver.ColumnIndex].Value + "'",
true);
}
catch (SqlException)
{
cellErrorText = "Can't look this cell up";
cellErrorLocation = theCellImHoveringOver;
}
}
private void dataGridView1_CellErrorTextNeeded(object sender,
DataGridViewCellErrorTextNeededEventArgs e)
{
if (cellErrorLocation != null)
{
if (e.ColumnIndex == cellErrorLocation.ColumnIndex &&
e.RowIndex == cellErrorLocation.RowIndex)
{
e.ErrorText = cellErrorText;
}
}
}
private DataTable Populate(string query, bool resetUnsharedCounter)
{
if (resetUnsharedCounter)
{
ResetCounter();
}
// Alter the data source as necessary
SqlDataAdapter adapter = new SqlDataAdapter(query,
new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=localhost"));
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);
return table;
}
private Label count = new Label();
private int unsharedRowCounter;
private void ResetCounter()
{
unsharedRowCounter = 0;
count.Text = unsharedRowCounter.ToString();
}
Private WithEvents wholeTable As New ToolStripMenuItem()
Private WithEvents lookUp As New ToolStripMenuItem()
Private strip As ContextMenuStrip
Private cellErrorText As String
Private Sub dataGridView1_CellContextMenuStripNeeded(ByVal sender As Object, _
ByVal e As DataGridViewCellContextMenuStripNeededEventArgs) _
Handles dataGridView1.CellContextMenuStripNeeded
cellErrorText = String.Empty
If strip Is Nothing Then
strip = New ContextMenuStrip()
lookUp.Text = "Look Up"
wholeTable.Text = "See Whole Table"
strip.Items.Add(lookUp)
strip.Items.Add(wholeTable)
End If
e.ContextMenuStrip = strip
End Sub
Private Sub wholeTable_Click(ByVal sender As Object, ByVal e As EventArgs) Handles wholeTable.Click
dataGridView1.DataSource = Populate("Select * from employees", True)
End Sub
Private theCellImHoveringOver As DataGridViewCellEventArgs
Private Sub dataGridView1_CellMouseEnter(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles dataGridView1.CellMouseEnter
theCellImHoveringOver = e
End Sub
Private cellErrorLocation As DataGridViewCellEventArgs
Private Sub lookUp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lookUp.Click
Try
dataGridView1.DataSource = Populate("Select * from employees where " & _
dataGridView1.Columns(theCellImHoveringOver.ColumnIndex).Name & " = '" & _
dataGridView1.Rows(theCellImHoveringOver.RowIndex).Cells(theCellImHoveringOver.ColumnIndex).Value.ToString() & _
"'", True)
Catch ex As SqlException
cellErrorText = "Can't look this cell up"
cellErrorLocation = theCellImHoveringOver
End Try
End Sub
Private Sub dataGridView1_CellErrorTextNeeded(ByVal sender As Object, _
ByVal e As DataGridViewCellErrorTextNeededEventArgs) _
Handles dataGridView1.CellErrorTextNeeded
If (Not cellErrorLocation Is Nothing) Then
If e.ColumnIndex = cellErrorLocation.ColumnIndex AndAlso _
e.RowIndex = cellErrorLocation.RowIndex Then
e.ErrorText = cellErrorText
End If
End If
End Sub
Private Function Populate(ByVal query As String, ByVal resetUnsharedCounter As Boolean) As DataTable
If resetUnsharedCounter Then
ResetCounter()
End If
' Alter the data source as necessary
Dim adapter As New SqlDataAdapter(query, _
New SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" & _
"Initial Catalog=Northwind;Data Source=localhost"))
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
adapter.Fill(table)
Return table
End Function
Private count As New Label()
Private unsharedRowCounter As Integer
Private Sub ResetCounter()
unsharedRowCounter = 0
count.Text = unsharedRowCounter.ToString()
End Sub
注解
CellContextMenuStripNeeded仅当控件属性已设置或其VirtualMode属性为 true
时DataGridViewDataSource,才会发生该事件。
处理 CellContextMenuStripNeeded 事件时,每当用户右键单击单元格时,都将显示处理程序中指定的快捷菜单。 如果要显示由单元格的当前状态或值确定的快捷菜单,这非常有用。
每当 CellContextMenuStripNeeded 以编程方式检索属性的值 DataGridViewCell.ContextMenuStrip 时,或者当用户右键单击单元格时,也会发生该事件。
可以使用 ColumnIndex 和 RowIndex 属性来确定单元格的状态或值,并使用此信息来设置 ContextMenuStrip 属性。 此属性使用事件值替代的单元格 ContextMenuStrip 属性的值进行初始化。
CellContextMenuStripNeeded处理大量数据时处理 事件,以避免为多个单元格设置单元格ContextMenuStrip值而造成性能损失。 有关详细信息,请参阅 缩放 Windows 窗体 DataGridView 控件的最佳做法。
还可以通过设置行属性或处理DataGridView控件RowContextMenuStripNeeded的事件,为单个行ContextMenuStrip(而不是单个单元格)指定快捷菜单。 单元格 ContextMenuStrip 属性设置将覆盖行 ContextMenuStrip 属性设置,事件 CellContextMenuStripNeeded 将 RowContextMenuStripNeeded 同时覆盖事件和行 ContextMenuStrip 属性设置。 但是,可以为单元格快捷菜单指定 null
,以防止覆盖行快捷菜单。
有关如何处理事件的详细信息,请参阅 处理和引发事件。
创建 DataGridViewCellContextMenuStripNeededEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件。
扩展方法
GetMethodInfo(Delegate) |
获取指示指定委托表示的方法的对象。 |