如何:根据 Windows 窗体 DataGridView 控件的单元格中的更改执行自定义操作
DataGridView 控件包含许多可用于检测 DataGridView 单元格状态更改的事件。 最常用的两个事件是 CellValueChanged 和 CellStateChanged 事件。
检测 DataGridView 单元格的值更改
为 CellValueChanged 事件编写处理程序。
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { string msg = String.Format( "Cell at row {0}, column {1} value changed", e.RowIndex, e.ColumnIndex); MessageBox.Show(msg, "Cell Value Changed"); }
Private Sub dataGridView1_CellValueChanged(ByVal sender As Object, _ ByVal e As DataGridViewCellEventArgs) _ Handles dataGridView1.CellValueChanged Dim msg As String = String.Format( _ "Cell at row {0}, column {1} value changed", _ e.RowIndex, e.ColumnIndex) MessageBox.Show(msg, "Cell Value Changed") End Sub
检测 DataGridView 单元格的状态更改
为 CellStateChanged 事件编写处理程序。
private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e) { DataGridViewElementStates state = e.StateChanged; string msg = String.Format("Row {0}, Column {1}, {2}", e.Cell.RowIndex, e.Cell.ColumnIndex, e.StateChanged); MessageBox.Show(msg, "Cell State Changed"); }
Private Sub dataGridView1_CellStateChanged(ByVal sender As Object, _ ByVal e As DataGridViewCellStateChangedEventArgs) _ Handles dataGridView1.CellStateChanged Dim state As DataGridViewElementStates = e.StateChanged Dim msg As String = String.Format( _ "Row {0}, Column {1}, {2}", _ e.Cell.RowIndex, e.Cell.ColumnIndex, e.StateChanged) MessageBox.Show(msg, "Cell State Changed") End Sub
编译代码
此示例需要:
名为
dataGridView1
的 DataGridView 控件。 对于 C#,事件处理程序必须连接到相应的事件。对 System 和 System.Windows.Forms 程序集的引用。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈