gpt4 book ai didi

c# - 遍历数据网格 WPF 的行

转载 作者:行者123 更新时间:2023-11-30 15:20:42 31 4
gpt4 key购买 nike

如何在 WPF 中遍历数据 GridView 的每一行

foreach (System.Data.DataRowView dr in grid.Items)
{
string a = dr[0].ToString();
MessageBox.show(a);
}

这里我尝试读取第一列的所有值!

它给出了错误:

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'.

我该如何解决这个问题。请任何人帮助我。

谢谢!

最佳答案

Grid.Items是用于生成ItemsControl 内容的集合,转换为DataRowView 无效。在这种情况下会导致异常。

你需要的是...

foreach (DataRowView dr in grid.ItemsSource)
{
MessageBox.Show(dr[0]);
}

关于c# - 遍历数据网格 WPF 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443793/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com