DataGridView.CellMouseClick DataGridView.CellMouseClick DataGridView.CellMouseClick DataGridView.CellMouseClick Event

定义

在用户用鼠标单击单元格中的任何位置时发生。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. 在此示例中, 事件处理程序报告CellMouseClick事件的发生。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. 若要报告多个事件或频繁发生的事件, 请考虑MessageBox.ShowConsole.WriteLine消息替换为多行或将TextBox其追加到多行。To 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.

若要运行示例代码, 请将其粘贴到包含名为DataGridView DataGridView1的类型的实例的项目中。To run the example code, paste it into a project that contains an instance of type DataGridView named DataGridView1. 然后, 确保事件处理程序与CellMouseClick事件相关联。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单击的, 此事件发生在复选框更改值之前, 因此, 如果您不希望基于当前值计算预期值, 则通常会CellValueChanged处理事件。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. 由于该事件仅在提交用户指定的值时才发生, 这通常发生在焦点离开单元格时, 您还必须处理CurrentCellDirtyStateChanged事件。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.

适用于

另请参阅