【问题标题】:How can i set the value of a datagrid cell using its Column and row index values?如何使用其列和行索引值设置数据网格单元格的值?
【发布时间】:2011-10-12 08:41:00
【问题描述】:

如何使用单元格列和行索引将值插入特定的数据网格单元格。我将行和列索引保存为整数。

我得到的索引如下。我基本上采用单元格值、列索引和行索引并将其作为序列化 XML 发送到 java,后者将其发回并需要将其放在同一个单元格中。

        int column = dataGrid2.CurrentCell.Column.DisplayIndex;
        int row = dataGrid2.SelectedIndex;

谢谢,

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    对于数据网格,您可以通过 Items 属性访问行。单元格是项目的集合。

    dataGrid2.Items[row].Cells[column].Text = "text";
    

    只要在当前页面生命周期内数据已绑定到数据网格,这将起作用。如果不是这种情况,那么我相信你会被卡住。

    【讨论】:

      【解决方案2】:

      要以编程方式更新 WPF DataGridCell,可能有多种方法...

      其中一种方法是更新绑定数据项本身的值。这样,属性更改通知将为所有订阅的视觉对象触发,包括 DataGridCell 本身...

      反思方法

       var boundItem = dataGrid2.CurrentCell.Item;
      
       //// If the column is datagrid text or checkbox column
       var binding = ((DataGridTextColumn)dataGrid2.CurrentCell.Column).Binding;
      
       var propertyName = binding.Path.Path;
       var propInfo = boundItem.GetType().GetProperty(propertyName);
       propInfo.SetValue(boundItem, yourValue, new object[] {});
      

      对于DataGridComboBoxColumn,您必须提取SelectedValuePath 并使用它来代替propertyName

      其他方法包括将单元格置于编辑模式并使用EditingElementStyle 中的某些行为更新其内容值...我觉得这很麻烦。

      如果你真的需要,请告诉我。

      【讨论】:

        【解决方案3】:

        我使用了一个基于 WPF 的变体——它的示例来完成整行,并且成功了!:

        (sender as DataGrid).RowEditEnding -= DataGrid_RowEditEnding;
        
        foreach (var textColumn in dataGrid2.Columns.OfType<DataGridTextColumn>())
                    {
                        var binding = textColumn.Binding as Binding;
                        if (binding != null)
                        {
                            var boundItem = dataGrid2.CurrentCell.Item;
                            var propertyName = binding.Path.Path;
                            var propInfo = boundItem.GetType().GetProperty(propertyName);
                            propInfo.SetValue(boundItem, NEWVALUE, new object[] { });
                        }
                    }
        
        (sender as DataGrid).RowEditEnding += DataGrid_RowEditEnding;
        

        PS:确保使用对列有效的值类型(可能通过 switch 语句)。

        例如: 打开 propertyName 或 propInfo... propInfo.SetValue(boundItem, (type) NEWVALUE, new object[] {});

                            switch (propertyName)
                            {
                                case "ColumnName":
                                    propInfo.SetValue(boundItem, ("ColumnName"'s type) NEWVALUE, new object[] { });
                                    break;
                                default:
                                    break;
                            }
        

        【讨论】:

          【解决方案4】:

          如果你使用 DataGrid 你可以试试

          DataRowView rowView = (dtGrid.Items[rows] as DataRowView); //Get RowView
          rowView.BeginEdit();
          rowView[col] = "Change cell here";
          rowView.EndEdit();
          dtGrid.Items.Refresh(); // Refresh table
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-03-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-24
            • 1970-01-01
            • 2017-02-05
            相关资源
            最近更新 更多