如何遍历 WPF 工具包 Datagrid 的行 [英] How to loop over the rows of a WPF toolkit Datagrid

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

问题描述

我在下一个代码中定义了一个名为 dgQuery 的 WPF 工具包数据网格控件;我用数据集的信息填充了这个,然后我在 dgQuery 中插入了一个新的复选框列来检查/取消选中某些行,我显示了我的 C# 代码的一部分:

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.

谢谢!!

推荐答案

这将在您的数据网格中返回一个行"

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...没有 Rows 属性!

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

希望这有帮助...

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

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