[英]How to get the selected Row values in TextBoxes for DataGrid in WPF C#
I want to display a selected row out of the datagrid into some textboxes.我想将数据网格中的选定行显示到一些文本框中。 The problem is that it becomes null at line 4 when converting into a DataRowView.
问题是它在第 4 行转换为 DataRowView 时变为 null。 Why is this?
为什么是这样?
1 private void dataGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
2 {
3 DataGrid grid = (DataGrid)sender;
4 DataRowView selected_row = grid.SelectedItem as DataRowView;
5
6 if (selected_row != null)
7 {
8 comboBoxCategory.Text = selected_row["Category"].ToString();
9 textBoxBrand.Text = selected_row["Brand"].ToString();
10 textBoxName.Text = selected_row["Name"].ToString();
11 textBoxCount.Text = selected_row["Count"].ToString();
12 textBoxPrice.Text = selected_row["Price"].ToString();
13 }
14 }
Apparantly the SelectedItem
property doesn't return a DataRowView
.显然
SelectedItem
属性不返回DataRowView
。
Either cast to the appropriate type assuming you have define one, or use the dynamic
keyword:假设您已经定义了一个类型,则转换为适当的类型,或者使用
dynamic
关键字:
private void dataGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid grid = (DataGrid)sender;
dynamic selected_row = grid.SelectedItem;
comboBoxCategory.Text = selected_row.Categorie.ToString();
textBoxBrand.Text = selected_row.Merk.ToString();
textBoxName.Text = selected_row.Naam.ToString();
textBoxCount.Text = selected_row.Aantal.ToString();
textBoxPrice.Text = selected_row.Prijs.ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.