【问题标题】:How to loop over the rows of a WPF toolkit Datagrid如何遍历 WPF 工具包 Datagrid 的行
【发布时间】:2010-12-28 10:01:46
【问题描述】:

我有下一个代码,我在其中定义了一个名为 dgQuery 的 WPF 工具包数据网格控件;我用数据集的信息填充了这个,然后我在 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 的所有行以及让我知道哪些行选中/取消选中复选框的条件?请帮我举个例子。

谢谢!!

【问题讨论】:

    标签: c# wpf datagrid wpfdatagrid wpftoolkit


    【解决方案1】:

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

    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 属性!

    希望这会有所帮助...

    【讨论】:

    • Tnxs 托尼!让我问你如何评估我插入的复选框列是否被选中/未选中,一般来说,我如何检索每个 DataGridRow 的所有单元格?
    • 我们如何在为每一行循环时获取行索引
    【解决方案2】:
     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();
    
    
                }
    

    【讨论】:

    • 托尼,再次感谢您。请再问一个问题,如何评估定义复选框的“opc”列的内容?在 DataRowView 我只能看到数据集列,而不是复选框。问候。
    • 为什么不试试 MarkB 的解决方案来访问数据网格中的复选框?
    • 我已经在这个论坛上再次为你问了这个问题,因为我自己找不到如何做,所以这里有一个答案:stackoverflow.com/questions/1936732/…使用我的答案公认!我自己没有尝试过,但它看起来应该可以工作!祝你好运!请告诉我上面发生了什么?
    • 托尼,再次感谢您的帮助。我读了 1936732 并且在申请我的案例时有很多疑问,所以我决定使用你原来的解决方案,首先,我在数据集中添加了一个 Bool 类型的新列,所以它现在模拟一个复选框(虽然我检查了双击: -)),我认为这是目前简单而干净的解决方案。我最好的问候。
    【解决方案3】:

    不确定这是否有用,因为它采用了与您开始时不同的方法,但您可以将其绑定到具有每列属性的对象的 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 的布尔值,用于复选框列。 _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 对象的信息。
    • 听起来不错,特里斯坦。使用这种方法的主要优点是您的代码与演示文稿的分离。因此,您的代码专注于集合而不是 UI 控件。这意味着如果您稍后更改 UI 控件,您的代码可能不必更改。此外,单元测试更容易,因为您可以通过操作集合而不是尝试操作 UI 以编程方式设置选择项。
    【解决方案4】:
    //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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 2010-12-19
      • 1970-01-01
      • 2010-12-01
      • 2011-02-13
      • 1970-01-01
      • 2010-12-17
      相关资源
      最近更新 更多