我在datagrid中使用RowEditEnding事件来获取更新的行并更新我的数据库
private void OnUpdateOrInsertItem(object sender,DataGridRowEditEndingEventArgs e)
{
DataGridRow row1 = e.Row;
int row_index = ((DataGrid)sender).ItemContainerGenerator.IndexFromContainer(row1);
int numberOfColumns = ((DataGrid)sender).Columns.Count;
DataGridCell RowColumn = ((DataGrid)sender).Columns[1].GetCellContent(row1).Parent as DataGridCell;
string CellValue = ((TextBlock)RowColumn.Content).Text;
MessageBox.Show(CellValue);
}
但是当我更改我在messageBox中打印它的单元格时,它会在messageBox上返回一个空值。 我认为问题与线程有关 这是我的xml代码
<Grid>
<DataGrid x:Name="datagrid1" AutoGenerateColumns="true" HorizontalAlignment="Left" Margin="0,55,0,0" VerticalAlignment="Top" Height="248" Width="672" RowEditEnding="OnUpdateOrInsertItem" PreviewKeyDown="OnDeleteItem"/>
<Button Content="Show Database" HorizontalAlignment="Left" Margin="54,10,0,0" VerticalAlignment="Top" Width="98" RenderTransformOrigin="-0.193,-13.506" Height="29" Click="Button_Click_1"/>
<ComboBox x:Name="comboBox1" SelectedIndex="0" HorizontalAlignment="Left" Margin="469,17,0,0" VerticalAlignment="Top" Width="120" Loaded="ComboBox_Loaded"
SelectionChanged="ComboBox_SelectionChanged"/>
</Grid>
如何解决此问题或者是否有其他方法可以在RowEditEnding事件中获取更新的行?
答案 0 :(得分:0)
ItemContainerGenerator
。如果需要访问UI项,请将相关属性绑定到基础视图模型。