Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF在datagrid中迭代_C#_Wpf_Datagrid - Fatal编程技术网

C# WPF在datagrid中迭代

C# WPF在datagrid中迭代,c#,wpf,datagrid,C#,Wpf,Datagrid,使用visual studio 2012 ulti使用WPF C#.NET4.5 旧winforms代码: foreach (DataGridViewRow paretoRow in ParetoGrid.Rows) { if ((Convert.ToInt32(paretoRow.Cells["CurrentPareto"].Value) < (Convert.ToInt32(paretoRow.Cells["NewPareto"].Value)))) {

使用visual studio 2012 ulti使用WPF C#.NET4.5

旧winforms代码:

foreach (DataGridViewRow paretoRow in ParetoGrid.Rows)
{
       if ((Convert.ToInt32(paretoRow.Cells["CurrentPareto"].Value) < (Convert.ToInt32(paretoRow.Cells["NewPareto"].Value))))
       {
              paretoRow.Cells["pNew"].Value = downArrow
       }
}
但是我仍然不知道现在谁能拿到手机

所以我的问题是,是否有语法更改要执行,如果有,在哪里?或者,我开始相信WPF中的数据网格比winforms更能使用对象,因此不需要使用名为“row”的属性,如果是这种情况,我应该知道在这个示例中使用什么逻辑/语法


谢谢你们的耐心,伙计们,想想当我回家去银行度假时,我会做一点WPF挖掘,看看它到底有多不同。

我想你们首先要做的是获取你的
数据网格的所有行。

public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid)
{
    var itemsSource = grid.ItemsSource as IEnumerable;
    if (null == itemsSource) yield return null;
    foreach (var item in itemsSource)
    {
        var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow;
        if (null != row) yield return row;
    }
}

在WPF中,您可以更加动态和面向对象。您可以将列“pNew”绑定到放入
DataGrid
的元素的属性上,该属性返回向下箭头。 如果值更改,您可以引发事件
PropertyChanged
(Interface
INotifyPropertyChanged
),并重新计算绑定属性

从WPF开始,同样有趣的是
数据模板
控制模板
转换器
。 当调用属性时,转换器将属性值更改为WPF的可用值(例如BoolToVisibility)。
DataTemplate
ControlTemplate
可用于更改控件的外观


有几个关于WPF的好教程。我还建议您研究MVVM模式,将其用作Businessobject和WPF控件的中间层,尤其是处理您在这里尝试执行的操作。

是的,您是对的。WPF
DataGrid
是围绕更好地支持对象的使用而构建的

可以使用类似于以下内容的ViewModel。将它们全部构建到一个集合中,然后将该集合设置为您的
ItemsSource
。如果要显示和显示图像,而不是选中pNew为真/假,则还需要使用ValueConverter

public class FooViewModel : INotifyPropertyChanged
{
    private int currentPareto;
    public int CurrentPareto 
    {
        get
        {
           return currentPareto;
        }
        set
        { 
            if (currentPareto == value)
                return;

            currentPareto = value;
            OnPropertyChanged("CurrentPareto");
            OnPropertyChanged("pNew");
        }
    }

    private int newPareto;
    public int NewPareto 
    {
        get
        {
           return newPareto;
        }
        set
        { 
            if (newPareto == value)
                return;

            newPareto = value;
            OnPropertyChanged("NewPareto");
            OnPropertyChanged("pNew");
        }
    }

    public bool pNew
    {
        get
        {
            return CurrentPareto < NewPareto;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

为什么不能使用这个属性获取行数,然后使用For循环进行迭代

dataGridView1.Rows.Count

人们似乎把这件事搞得太复杂了,这对我来说很有用:

foreach (System.Data.DataRowView dr in yourDataGrid.ItemsSource)
{
     MessageBox.Show(dr[0].ToString());
}

我甚至不明白为什么在数据网格中获取行及其值如此复杂。想知道怎么做感觉很痛苦。api甚至给出了一些有趣的事件名称,这些名称也不是那么直接。为什么人们不能把注意力集中在基线上,给出准确的需求,而不是各种各样的没有用处和意义的选择呢。我的意思是你只需要一把勺子和叉子就可以吃了。从10万年前开始就没有改变过。这是我的代码,感谢那个家伙,他提到有些人只是想把事情复杂化,浪费你的时间

    private void dtaResultGrid_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        ActivateTestDatagridAccess();
    }

    public async void ActivateTestDatagridAccess()
    {
        try
        {
            await Task.Delay(500);
            foreach (System.Data.DataRowView dr in dtaResultGrid.ItemsSource)
            {
                for (int j = 0; j < dtaResultGrid.Columns.Count; j++)
                {
                    Console.WriteLine(dr[j].ToString());
                }
                Console.Write(Environment.NewLine);
            }
        }
        catch (Exception exrr)
        {
            Console.WriteLine(exrr.ToString());
        }
    }
private void dtaResultGrid\u DataContextChanged(对象发送方,dependencPropertyChangedEventArgs e)
{
ActivateTestDatagridAccess();
}
公共异步void ActivateTestDatagridAccess()
{
尝试
{
等待任务。延迟(500);
foreach(dtaResultGrid.ItemsSource中的System.Data.DataRowView dr)
{
对于(int j=0;j
如果使用类的实例(如struct_类)填充datagridview行 这将是拥有foreach循环的最快方式

foreach (struct_class row in dgv.Items)
{
    MessageBox.Show(row.name);
}

这应该会有帮助:那么如何获取所有行?看看我的foreach循环,我曾经使用datgridviewrow hwoever,但它对WPF不起作用。您如何获得所有行?感谢代码,我来试一试。不过,一开始它看起来比winforms冗长得多,一个简单的3行程序变得复杂得多。但我认为这是对我目前方法的修正,应该是改变。我这样想对吗?在var itemsource=grid.itemsource下有一个红色的语法行作为ienumerable。错误是别名“IEnumerable”说“需要1个类型参数”“您需要确保网格中有itemsource”这是什么意思?请耐心等待我,因为silverlight在2009年3月发布的工具包将名称空间microsoft.windows.controls更改为system.windows.controls:代码将稍有更改,您需要:using system.Collections.Generic;使用系统集合;为了修正“IEnumerable”说“需要1个类型参数”的错误,谢谢你的建议,这肯定是我这个周末要考虑的。目前,我正在寻找一种解决方案的“黑客”,只是想看看如何使用winforms风格的方法来实现。我认为人们喜欢让事情变得更加复杂。我建议使用yourDataGrid.Items而不是yourDataGrid.ItemsSource。这将根据已排序的列维护任何用户应用的排序方向。不起作用,它显示错误无法将对象强制转换为System.Data.DataRowView您的答案是正确的(因此我给了它一个向上投票)。也许大多数选民可能想要更多的示例代码?(我希望downvoting需要一个注释。)这个答案不正确,因为WPF DataGrid没有名为Rows的成员。这实际上是前面在这个线程中提到的。但我也希望否决投票需要评论。
foreach (System.Data.DataRowView dr in yourDataGrid.ItemsSource)
{
     MessageBox.Show(dr[0].ToString());
}
    private void dtaResultGrid_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        ActivateTestDatagridAccess();
    }

    public async void ActivateTestDatagridAccess()
    {
        try
        {
            await Task.Delay(500);
            foreach (System.Data.DataRowView dr in dtaResultGrid.ItemsSource)
            {
                for (int j = 0; j < dtaResultGrid.Columns.Count; j++)
                {
                    Console.WriteLine(dr[j].ToString());
                }
                Console.Write(Environment.NewLine);
            }
        }
        catch (Exception exrr)
        {
            Console.WriteLine(exrr.ToString());
        }
    }
foreach (struct_class row in dgv.Items)
{
    MessageBox.Show(row.name);
}