开发者

how to update a cell in DataGrid which is binded with string[][] array

I have a DataGrid and a string[][] dataSource array as a source data for this DataGrid. I use following code to set binding:

dataGrid.ItemsSource = dataSource;

for (int i = 0; i < columns; i++)
{
    dataGrid.Columns.Add(new DataGridTextColumn
    {                        
        Binding = new Binding(string.Format("[{0}]", i))
     });
}

How can I 开发者_运维问答update an information in a cell of DataGrid when value in string[i][j] was changed?


If the data item that you are binding to implemented INotifyPropertyChanged then the update would happen automatically, as the data item/collection would broadcast that a property had been changed, and the datagrid would automatically be updated without you having to do a thing (manual grid rebinds can be slow depending on the amount of data).

So if you change the data structure that you are binding to from a string to something that implements INotifyPropertyChanged (even if you code up that data object yourself, which is easy to do), and set the grid to automatically generate columns, then all you will need to do is set the dataGrid.ItemSource property and update the individual data objects as necessary.


Re-bind the data array.


Heres a Hack.

If you know which column has been updated, you can set the binding on the column to a new binding (with the same path) this will force the binding to re-evaluate for the whole column. thats expensive i know. i wouldn't do this i would be binding to objects that have properties, just as slugster suggested.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜