WPF Datagrid读取一个单元格值 [英] WPF Datagrid read a cell value
问题描述
我正在尝试找出如何读取WPF数据网格单元格的值。
I am trying to find out how to read the value of my WPF datagrid cells.
类似
String myString = myDataGrid.Cells[1][2].ToString();
已经在XAML中创建了datagrid,并且我已经通过使用
the datagrid has been created in XAML and i have populated the datagrid with row data by using
reportGrid.Items.Add(new cbResultRow() { ... });
现在我想返回并读取数据网格中的单元格值。
now I want to go back and read cell values in my datagrid.
我已经看到了一些从选定的行或单元格读取数据的示例,因为我没有任何选择(用户没有与数据网格交互)。
I have seen some examples of reading data from a selected row or cell, by I don't have any selection (the user doesnt interact with the datagrid).
我也看到过这样的代码
foreach(DataGridRow myrow in myDataGrid.Rows)
但是编译器说Rows不是datagrid的成员。
however the compiler says Rows is not a member of datagrid.
我搜索了几个小时,试图找出如何做我认为很简单的事情!
I have searched for several hours to try to find out how to do what I would have thought was a very simple thing!
请帮助,
谢谢,
会。
推荐答案
WPF数据网格旨在绑定到类似于DataTable。在大多数情况下,您将修改DataTable,以及DataTable中绑定到DataGrid的行/列。
The WPF datagrid was built to bind to something like a DataTable. The majority of the time, you will modify the DataTable, and the Rows/Columns within the DataTable that is bound to the DataGrid.
DataGrid本身是可视元素数据表。 这是一个很好的有关如何执行此操作的教程。在循环中修改数据看起来像这样。
The DataGrid itself is the Visual Element for the DataTable. Here is a pretty good tutorial on how to do this. To modify data within a loop would look something like this.
foreach(DataRow row in myTable.Rows)
{
row["ColumnTitle"] = 1;
}
这只会使 ColumnTitle列中的所有值都等于1。要访问单个单元,它看起来应该像这样。
This would simply make all the values in Column "ColumnTitle" equal to 1. To access a single cell it would look something like this.
myTable.Rows[0][0] = 1;
这会将DataTable中的第一个单元格设置为1。
This would set the first cell in your DataTable to 1.
这篇关于WPF Datagrid读取一个单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!