如何循环一个WPF工具包DataGrid

如何循环一个WPF工具包DataGrid

本文介绍了如何循环一个WPF工具包DataGrid的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我定义了名为dgQuery一个WPF工具包DataGrid控件下一个code;我充满了数据信息的这一个,然后我插入一个新的复选框列dgQuery检查/取消某些行,我告诉我的C#code的一部分:

I have the next code where I defined a WPF toolkit datagrid control called dgQuery; I filled this one with information of a dataset, then I inserted a new checkbox column in dgQuery to check/uncheck some of the rows, I show part of my C# code:

dgQuery.DataContext = dS.Tables[0];

DataGridTemplateColumn cbCol = new DataGridTemplateColumn();
cbCol.Header = "Opc";
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(CheckBox));
Binding bind = new Binding("IsSelected");
bind.Mode = BindingMode.TwoWay;
factory.SetValue(CheckBox.IsCheckedProperty, bind);
DataTemplate cellTemplate = new DataTemplate();
cellTemplate.VisualTree = factory;
cbCol.CellTemplate = cellTemplate;
dgQuery.Columns.Insert(0, cbCol);

选中/取消选中进入dgQuery行的新复选框列后,我会点击一个按钮来保存到数据库中只有我检查了行。现在的问题是,我怎么能培养阅读dgQuery的所有行,这将让我知道这行已复选框选中/取消该条件的循环?帮我举个例子,谢谢。

After checking/unchecking into the new checkbox column of the dgQuery rows I will click a button to save into a database only the rows I checked. The question is, how can I develop the loop for reading all the rows of dgQuery and the condition that will let me know which rows have the checkbox checked/unchecked? Help me with an example, please.

谢谢!

推荐答案

这会在你的DataGrid中返回一个行

this will return a 'row' in your datagrid

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数据网格中,行ItemSource.items ......没有行属性!

in wpf datagrid, rows are ItemSource.items... no Rows property!

希望这有助于...

这篇关于如何循环一个WPF工具包DataGrid的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:11