【发布时间】:2019-07-15 12:41:03
【问题描述】:
在初始化带有 DataGridView 的 Form 时,当放置列标题文本时触发该事件。因此,处理事件的函数在启动时运行的次数与列的数量一样多。我怎样才能停止它并仅在用户对数据网格进行一些更改后才触发它?
添加: 似乎从 CellValueChanged 更改为 CellEndEdit 可以解决问题。
【问题讨论】:
标签: c#
在初始化带有 DataGridView 的 Form 时,当放置列标题文本时触发该事件。因此,处理事件的函数在启动时运行的次数与列的数量一样多。我怎样才能停止它并仅在用户对数据网格进行一些更改后才触发它?
添加: 似乎从 CellValueChanged 更改为 CellEndEdit 可以解决问题。
【问题讨论】:
标签: c#
您是否尝试过使用数据网格的 SelectionChanged 属性,在内容更改时触发事件?
<Grid>
<DataGrid> Name="TestDataGrid" SelectionChanged="DataGrid_EventName" />
</Grid>
在代码隐藏中,事件将被捕获:
private void DataGrid_EventName(object sender, SelectionChangedEventArgs e)
{
//Conduct work here
}
编辑:我很确定您仍然需要初始化 DataGrid。
例如,您可以使用此事件从 DataGrid 中提取特定于行的信息:
private void DataGrid_EventName(object sender, SelectionChangedEventArgs e)
{
DataGrid _dataGrid = (DataGrid)sender;
DataRowView selectedRow = _dataGrid.SelectedItem as DataRowView;
if (selectedRow != null)
{
RowItemList = new List<string>(); //List declared outside of this method...
//add row information to the list
RowItemList.Add(selectedRow.ToString());
//or, get column-specific information
string ColmnEntry = selectedRow[ColumnName].ToString();
}
}
【讨论】: