比较datagrid中同一行的两个单元格 [英] compare two cells on same row in datagrid

查看:102
本文介绍了比较datagrid中同一行的两个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#.NET 4.5,MS Visual Studio 2012,WPF。

Using C# .NET 4.5, MS visual studio 2012, WPFs.

只需让这段代码终于可以工作,它基本上遍历一个datgrid通过行。

Hi, just got this code to finally work, it basically iterates through a datgrid via row.

被警告,他可能会冒犯所有的WPF向导!

be warned his may offend all the WPF wizards out there!

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。我可以遍历列,让我去单元格单元格,但不是我之后例如

So as you can see I iterate through the rows. Now what I want to do is compare a cell value at column index 2 with columns index 4 on every "row", then if true or false update column 5 accordingly. I can iterate through the columns which lets me go cell per cell but isn't what I am after e.g

foreach (DataGridColumn column in paretogrid.Columns)

所以是这样的:

paretogrid.Columns[2].GetCellContent(something here) as textblock;


推荐答案

请看下面的解释,这将有助于清除为什么你看到你看到的东西

please see the following explanation below it will help clear up why you are seeing what you are seeing

好的,让我更容易理解TextBox,例如,如果我这样做:

Ok let me make it easier for you to understand TextBox for example if I did:

var myInt = (int)textBox1.text;

例如,这将编译,因为我暗示我想要 myInt 存储 Int32 值:

for example this would compile because I am implying that I want myInt to store an Int32 value, however :

var myInt = (int)textBox1;

将失败,因为您无法转换 Object ,这是一个价值的 TextBox ,这样可以更容易地看到你的初始错误是你获得它的原因。

would fail because you can not convert an Object, which is the TextBox to a Value does this make it easier to see where your initial error is an why you were getting it.

这与$ code> WinForms vs WPF 无关,如果基本上有些简单,我相信我们中许多人以前做过,因为当我们想要转换 Value .Text 追加到对象的末尾$ c>对象持有而不是对象本身

This also has nothing to do with WinForms vs WPF if basically something simple that I am sure many of us have done before because we forget to Append .Text to the end of the object when we are wanting to convert the Value that the object holds rather that the object itself

这篇关于比较datagrid中同一行的两个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆