使用 C# .NET 4.5、MS Visual Studio 2012、WPF。
嗨,刚刚让这段代码终于工作了,它基本上通过行迭代数据网格。
请注意,他可能会冒犯所有 WPF 巫师!
public IEnumerable<System.Windows.Controls.DataGridRow> GetDataGridRow(System.Windows.Controls.DataGrid grid)
{
var itemsource = grid.ItemsSource as System.Collections.IEnumerable;
paretogrid.UpdateLayout();
if (null == itemsource) yield return null;
foreach (var item in itemsource)
{
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow; // null?
if (null != row) yield return row;
}
}
private void ShowArrows()
{
var rows = GetDataGridRow(paretogrid); // fetching null?
foreach (DataGridRow r in rows)
{
DataRowView rv = (DataRowView)r.Item;
}
}
正如您所看到的,我遍历了行。现在我想要做的是将列索引 2 处的单元格值与每个“行”上的列索引 4 进行比较,然后如果 true 或 false 则相应地更新列 5。我可以迭代列,这让我可以逐个单元格地进行操作,但这不是我想要的,例如
foreach (DataGridColumn column in paretogrid.Columns)
是不是这样:
paretogrid.Columns[2].GetCellContent(something here) as textblock;
最佳答案
请参阅下面的说明,它将帮助您弄清楚为什么您会看到您所看到的内容
好吧,让我让您更容易理解 TextBox,例如,如果我这样做的话:
var myInt = (int)textBox1.text;
例如,这会编译,因为我暗示我希望 myInt
存储 Int32
值,但是:
var myInt = (int)textBox1;
会失败,因为您无法将Object
(即TextBox
)转换为值,这是否可以更轻松地查看您最初的错误所在以及原因得到它。
这也与 WinForms
与 WPF
无关,如果基本上是一些简单的事情,我相信我们很多人以前都做过,因为我们忘记附加 。当我们想要转换对象保存的
添加到对象的末尾Value
而不是对象本身时,将文本
关于c# - 比较数据网格中同一行的两个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15813266/