c# - 如何在 WPF C# 中的 DataGrid 的 TextBoxes 中获取选定的行值
问题描述
我想将数据网格中的选定行显示到一些文本框中。问题是它在转换为 DataRowView 时在第 4 行变为空。为什么是这样?
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 }
解决方案
显然,该SelectedItem
属性不返回 a DataRowView
。
假设您已定义一个类型,则转换为适当的类型,或使用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();
}
推荐阅读
- c# - 一组工作人员处理队列中的项目
- git - 如何在同一个拉取请求中识别不同的作者?
- javascript - 我需要澄清一下使用展开运算符时会发生什么
- amazon-web-services - 我应该如何使用分段上传将数据附加到 s3 的单个对象中?
- tableau-api - 深度链接-Tableau
- angular - 角度缓存依赖是否通过构造函数注入?
- c# - 在 C# UWP 中获取快捷方式目标路径
- c++ - UDP 客户端不在 esp32 上广播消息
- powershell - 无法获取 New-F5Item cmdlet
- checkbox - 如何以正式的angular7检测复选框选中事件?