c# - 比较数据网格中同一行的两个单元格

标签 c# wpf datagrid

使用 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)转换为值,这是否可以更轻松地查看您最初的错误所在以及原因得到它。

这也与 WinFormsWPF 无关,如果基本上是一些简单的事情,我相信我们很多人以前都做过,因为我们忘记附加 。当我们想要转换对象保存的Value而不是对象本身时,将文本添加到对象的末尾

关于c# - 比较数据网格中同一行的两个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15813266/

相关文章:

c# - 数据 GridView C# 背后的正确逻辑

Wpf Datagrid 删除行问题

c# - Windows 窗体和 C# 中控件的动态自上而下列表?

c# - 在 ASP.NET Core 2 日志记录更新后将 ILogger 映射到 Serilog 的记录器

c# - 在 Azure Web 角色中禁用 IIS 空闲超时

c# - 如何在 ASP.NET Core 中设置正确的 AttachDbFilename 相对路径?

c# - 带有文本的 WPF DataGrid 进度条

c# - 组合框的 WPF 不同 ItemsSource

c# - 如何在 MVVM 中单击时清除文本框

wpf - 在 WPF XAML 中缩放复杂的 SVG 路径