遍历数据网格 WPF 的行 [英] Iterating through rows of a data grid WPF
问题描述
如何遍历WPF中数据网格视图的每一行
how to iterate through every row of data grid view in WPF
foreach (System.Data.DataRowView dr in grid.Items)
{
string a = dr[0].ToString();
MessageBox.show(a);
}
这里我尝试读取第一列的所有值!
here I try to read all the values of first column!
它给出了错误:
Test.exe 中发生类型为System.InvalidCastException"的未处理异常
附加信息:无法将<>f__AnonymousType0`1[System.String]"类型的对象转换为System.Data.DataRowView"类型.
An unhandled exception of type 'System.InvalidCastException' occurred in Test.exe
Additional information: Unable to cast object of type '<>f__AnonymousType0`1[System.String]' to type 'System.Data.DataRowView'.
我该如何解决这个问题.请任何人帮助我.
How can I resolve this. please any one help me.
谢谢!
推荐答案
Grid.Items
是一个集合,用于生成ItemsControl
的内容,转换为DataRowView
无效.在这种情况下,这会导致异常.
Grid.Items
is a collection used to generate the content of the ItemsControl
, cast to DataRowView
is invalid. Which is cauing an exception in this case.
你需要的是...
foreach (DataRowView dr in grid.ItemsSource)
{
MessageBox.Show(dr[0]);
}
这篇关于遍历数据网格 WPF 的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!