如何:响应 Windows 窗体 DataGrid 控件中的单击
更新:2007 年 11 月
说明: |
---|
DataGridView 控件替换了 DataGrid 控件并添加了功能;但是也可选择保留 DataGrid 控件以备向后兼容和将来使用。有关更多信息,请参见 Windows 窗体 DataGridView 控件和 DataGrid 控件之间的区别。 |
当 Windows 窗体 DataGrid 连接到数据库后,您可以监视用户单击的单元格。
检测 DataGrid 的用户何时选择另一个单元格
在 CurrentCellChanged 事件处理程序中,编写代码进行适当地响应。
Private Sub myDataGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myDataGrid.CurrentCellChanged MessageBox.Show("Col is " & myDataGrid.CurrentCell.ColumnNumber _ & ", Row is " & myDataGrid.CurrentCell.RowNumber _ & ", Value is " & myDataGrid.Item(myDataGrid.CurrentCell)) End Sub
private void myDataGrid_CurrentCellChanged(object sender, System.EventArgs e) { MessageBox.Show ("Col is " + myDataGrid.CurrentCell.ColumnNumber + ", Row is " + myDataGrid.CurrentCell.RowNumber + ", Value is " + myDataGrid[myDataGrid.CurrentCell] ); }
(Visual C#) 在窗体的构造函数中放置以下代码以注册事件处理程序。
this.myDataGrid.CurrentCellChanged += new System.EventHandler(this.myDataGrid_CurrentCellChanged);
确定用户所单击的 DataGrid 部分
在适当的事件处理程序(例如 MouseDown 或 Click 事件处理程序)中调用 HitTest 方法。
HitTest 方法返回包含所单击区域的行和列的 DataGrid.HitTestInfo 对象。
Private Sub myDataGrid_MouseDown(ByVal sender As Object, _ ByVal e As MouseEventArgs) Handles myDataGrid.MouseDown Dim myGrid As DataGrid = CType(sender, DataGrid) Dim hti As System.Windows.Forms.DataGrid.HitTestInfo hti = myGrid.HitTest(e.X, e.Y) Dim message As String = "You clicked " Select Case hti.Type Case System.Windows.Forms.DataGrid.HitTestType.None message &= "the background." Case System.Windows.Forms.DataGrid.HitTestType.Cell message &= "cell at row " & hti.Row & ", col " & hti.Column Case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader message &= "the column header for column " & hti.Column Case System.Windows.Forms.DataGrid.HitTestType.RowHeader message &= "the row header for row " & hti.Row Case System.Windows.Forms.DataGrid.HitTestType.ColumnResize message &= "the column resizer for column " & hti.Column Case System.Windows.Forms.DataGrid.HitTestType.RowResize message &= "the row resizer for row " & hti.Row Case System.Windows.Forms.DataGrid.HitTestType.Caption message &= "the caption" Case System.Windows.Forms.DataGrid.HitTestType.ParentRows message &= "the parent row" End Select Console.WriteLine(message) End Sub
private void myDataGrid_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { DataGrid myGrid = (DataGrid) sender; System.Windows.Forms.DataGrid.HitTestInfo hti; hti = myGrid.HitTest(e.X, e.Y); string message = "You clicked "; switch (hti.Type) { case System.Windows.Forms.DataGrid.HitTestType.None : message += "the background."; break; case System.Windows.Forms.DataGrid.HitTestType.Cell : message += "cell at row " + hti.Row + ", col " + hti.Column; break; case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader : message += "the column header for column " + hti.Column; break; case System.Windows.Forms.DataGrid.HitTestType.RowHeader : message += "the row header for row " + hti.Row; break; case System.Windows.Forms.DataGrid.HitTestType.ColumnResize : message += "the column resizer for column " + hti.Column; break; case System.Windows.Forms.DataGrid.HitTestType.RowResize : message += "the row resizer for row " + hti.Row; break; case System.Windows.Forms.DataGrid.HitTestType.Caption : message += "the caption"; break; case System.Windows.Forms.DataGrid.HitTestType.ParentRows : message += "the parent row"; break; } Console.WriteLine(message); }
(Visual C#) 在窗体的构造函数中放置以下代码以注册事件处理程序。
this.myDataGrid.MouseDown += new System.Windows.Forms.MouseEventHandler (this.myDataGrid_MouseDown);
请参见
任务
如何:在运行时更改 Windows 窗体 DataGrid 控件中显示的数据