2011-10-12 78 views
4

如何通过使用单元格列和行索引将值插入到特定的datagrid单元格中。我将行和列索引保存为整数。如何使用列和行索引值设置datagrid单元格的值?

我得到了如下的索引。我基本上采取单元格值,列索引和行索引,并作为序列化的XML发送到Java发送回来,并需要把它放在同一个单元格。

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

感谢,

回答

1

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

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

只要数据在当前页面生命周期中已经绑定到数据网格,它就会工作。如果情况并非如此,那么我相信你会陷入控制。

1

以编程方式更新一个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中的某些行为更新其内容值...我觉得这很麻烦。

如果你确实需要,请告诉我。

1

我使用基于变化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语句)。

例如: switch on propertyName or propInfo ... propInfo.SetValue(boundItem,(type)NEWVALUE,new object [] {});

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