如何通過使用單元格列和行索引將值插入到特定的datagrid單元格中。我將行和列索引保存爲整數。如何使用列和行索引值設置datagrid單元格的值?
我得到了如下的索引。我基本上採取單元格值,列索引和行索引,並作爲序列化的XML發送到Java發送回來,並需要把它放在同一個單元格。
int column = dataGrid2.CurrentCell.Column.DisplayIndex;
int row = dataGrid2.SelectedIndex;
感謝,
如何通過使用單元格列和行索引將值插入到特定的datagrid單元格中。我將行和列索引保存爲整數。如何使用列和行索引值設置datagrid單元格的值?
我得到了如下的索引。我基本上採取單元格值,列索引和行索引,並作爲序列化的XML發送到Java發送回來,並需要把它放在同一個單元格。
int column = dataGrid2.CurrentCell.Column.DisplayIndex;
int row = dataGrid2.SelectedIndex;
感謝,
對於您通過項目屬性訪問行的數據網格。單元格是項目上的集合。
dataGrid2.Items[row].Cells[column].Text = "text";
只要數據在當前頁面生命週期中已經綁定到數據網格,它就會工作。如果情況並非如此,那麼我相信你會陷入控制。
以編程方式更新一個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
中的某些行爲更新其內容值...我覺得這很麻煩。
如果你確實需要,請告訴我。
我使用基於變化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;
}