如何循环遍历WPF工具包DataGrid的行

3

我有下面的代码,定义了一个名为dgQuery的WPF工具包datagrid控件;我使用数据集填充了它,然后在dgQuery中插入了一个新的复选框列,以便选择/取消某些行,以下是我的部分C#代码:

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的所有行和条件,以让我知道哪些行已选中/未选中复选框?请给我提供一个示例。

谢谢!

4个回答

11

这将返回您数据表格中的一行

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 DataGrid 中,行是 ItemSource.items... 没有 Rows 属性!

希望这可以帮到你...


1
谢谢Tony!请问我该如何评估我插入的复选框列是选中还是未选中,以及如何检索每个DataGridRow的所有单元格? - Tristan
1
在循环每一行时,我们如何获取行索引? - Gaurav123

1
 var row = GetDataGridRows(dataGrid1);
 /// go through each row in the datagrid
            foreach (Microsoft.Windows.Controls.DataGridRow r in row)
            {
                DataRowView rv = (DataRowView)r.Item;

                // Get the state of what's in column 1 of the current row (in my case a string)
                string t = rv.Row[1].ToString();


            }

Tony,再次感谢。还有一个问题,请问我该如何评估定义了复选框的“opc”列的内容?在DataRowView中,我只能看到数据集列,而看不到复选框。谢谢。 - Tristan
为什么不尝试一下MarkB的解决方案来访问你数据表格中的复选框呢? - Tony The Lion
我已经在论坛上为您再次提出了这个问题,因为我自己找不到如何解决它,所以这里是带有答案的主题讨论:http://stackoverflow.com/questions/1936732/datagridtemplatecolumn-get-value-of-cell使用我接受的答案!虽然我自己还没有尝试过,但它看起来应该能够工作!祝你好运!请告诉我发生了什么事吧? - Tony The Lion
Tony,再次感谢您的帮助。我阅读了1936732,但在应用到我的情况时有很多疑问,因此我决定使用您的原始解决方案。首先,我向数据集添加了一个新的布尔类型列,以模拟复选框(虽然我是通过双击来勾选的 :-)),我认为这是目前最简单和干净的解决方案。致以最好的问候。 - Tristan

1

不确定这是否有帮助,因为它采用了与您开始的方法不同的方法,但是与其直接使用网格进行操作,您可以将其绑定到一个ObservableCollection对象,该对象具有每个列的属性。如果在您的对象中添加一个名为“Selected”的bool属性,并将复选框列绑定到它,那么您随时可以查询当前选择的内容,如下所示:

 List<MemberEntity> selectedItems = 
            new List<MemberEntity>(from memberEntity in _memberEntities 
                                   where memberEntity.Selected == true 
                                   select memberEntity);

        //now save selectedItems to the database...

MemberEntity是一个类,它拥有网格中每一列的属性,包括一个名为Selected的bool类型属性,用于复选框列。_memberEntities是一个MemberEntity实例的ObservableCollection。网格的ItemSource属性绑定到_memberEntities,每个列的Binding属性都绑定到MemberEntity中的一个属性,例如假设Selected和Name是MemberEntity中的属性:

<tk:DataGrid ItemsSource="{Binding _memberEntities}">
        <tk:DataGrid.Columns>
            <tk:DataGridCheckBoxColumn Binding="{Binding Path=Selected}" />
            <tk:DataGridTextColumn Binding="{Binding Path=Name}" />
        </tk:DataGrid.Columns>
</tk:DataGrid>

马克,感谢你的时间和帮助。在应用到我的情况之前,我需要了解一下ObservableCollection对象。 - Tristan
听起来不错,特里斯坦。使用这种方法的主要优点是将代码与呈现分离。因此,您的代码专注于集合而不是UI控件。这意味着,如果稍后更改UI控件,则您的代码可能不必更改。此外,单元测试更容易,因为您可以通过操作集合来编程设置所选项目,而无需尝试操作UI。 - Mark Bostleman

0
//Looping thought datagrid rows & loop though cells and alert cell values

var row = GetDataGridRows(DataGrid_Standard);
/// go through each row in the datagrid 
foreach (Microsoft.Windows.Controls.DataGridRow r in row)
{
    DataRowView rv = (DataRowView)r.Item;
    foreach (DataGridColumn column in DataGrid_Standard.Columns)
    {
        if (column.GetCellContent(r) is TextBlock)
        {
            TextBlock cellContent = column.GetCellContent(r) as TextBlock;
            MessageBox.Show(cellContent.Text);
        }
        else if (column.GetCellContent(r) is CheckBox)
        {
            CheckBox chk = column.GetCellContent(r) as CheckBox;
            MessageBox .Show (chk.IsChecked.ToString());
        }                      
    }
} 

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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接