DataGridView.CellMouseClick 事件

定义

在用户用鼠标单击单元格中的任何位置时发生。Occurs whenever the user clicks anywhere on a cell with the mouse.

public:
 event System::Windows::Forms::DataGridViewCellMouseEventHandler ^ CellMouseClick;
public event System.Windows.Forms.DataGridViewCellMouseEventHandler CellMouseClick;
member this.CellMouseClick : System.Windows.Forms.DataGridViewCellMouseEventHandler 
Public Custom Event CellMouseClick As DataGridViewCellMouseEventHandler 

示例

下面的代码示例演示如何使用此成员。The following code example demonstrates the use of this member. 在此示例中,事件处理程序报告 @no__t 的事件发生。In the example, an event handler reports on the occurrence of the CellMouseClick event. 此报表可帮助你了解何时发生事件,并可帮助你进行调试。This report helps you to learn when the event occurs and can assist you in debugging. 若要报告多个事件或频繁发生的事件,请考虑将 @no__t 0 替换为 Console.WriteLine,或将消息附加到多行 TextBoxTo report on multiple events or on events that occur frequently, consider replacing MessageBox.Show with Console.WriteLine or appending the message to a multiline TextBox.

若要运行示例代码,请将其粘贴到包含名为 @no__t DataGridView 类型实例的项目中。To run the example code, paste it into a project that contains an instance of type DataGridView named DataGridView1. 然后,确保事件处理程序与 @no__t 0 事件关联。Then ensure that the event handler is associated with the CellMouseClick event.

private void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "ColumnIndex", e.ColumnIndex );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Button", e.Button );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Clicks", e.Clicks );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "X", e.X );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Y", e.Y );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Delta", e.Delta );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Location", e.Location );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "CellMouseClick Event" );
}
Private Sub DataGridView1_CellMouseClick(sender as Object, e as DataGridViewCellMouseEventArgs) _ 
     Handles DataGridView1.CellMouseClick

    Dim messageBoxVB as New System.Text.StringBuilder()
    messageBoxVB.AppendFormat("{0} = {1}", "ColumnIndex", e.ColumnIndex)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Button", e.Button)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Clicks", e.Clicks)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "X", e.X)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Y", e.Y)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Delta", e.Delta)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Location", e.Location)
    messageBoxVB.AppendLine()
    MessageBox.Show(messageBoxVB.ToString(),"CellMouseClick Event")

End Sub

注解

当用户单击鼠标按钮时,将发生此事件。This event occurs when the user clicks using any of the mouse buttons. 使用 MouseEventArgs.Button 属性可确定单击了哪个按钮。Use the MouseEventArgs.Button property to determine which button was clicked.

若要检测某个单元格的内容(如单元中的按钮或链接),请使用 CellContentClick 事件。To detect clicks on the contents of a cell, such as a button or link within a cell, use the CellContentClick event.

此事件的处理程序接收鼠标指针位置的列索引和行索引。The handler for this event receives the column index and row index of the mouse pointer position.

若要在 DataGridViewCheckBoxCell 中单击,此事件发生在复选框更改值之前,因此,如果不希望基于当前值计算预期值,则通常会改为处理 @no__t 事件。For clicks in a DataGridViewCheckBoxCell, this event occurs before the check box changes value, so if you do not want to calculate the expected value based on the current value, you will typically handle the CellValueChanged event instead. 由于该事件仅在提交用户指定的值时才发生,这通常发生在焦点离开单元格时,您还必须处理 @no__t 0 事件。Because that event occurs only when the user-specified value is committed, which typically occurs when focus leaves the cell, you must also handle the CurrentCellDirtyStateChanged event. 在该处理程序中,如果当前单元格为复选框单元,请调用 CommitEdit 方法并传入 Commit 值。In that handler, if the current cell is a check box cell, call the CommitEdit method and pass in the Commit value.

有关如何处理事件的详细信息,请参阅处理和引发事件For more information about how to handle events, see Handling and Raising Events.

适用于

另请参阅