views:

298

answers:

3

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?

A: 

Re-bind the data array.

George
Thank you, George. But I think it's too expensive operation in case only one value in the string[][] array changed. Is there any way to implement something like PropertyChanged? Or any suggestion to implement a miniExcel application (what components should I use for better productivity)?
Nike Skover
The only problem is that I have to bind DataGrid with data with arbitrary quantity of columns, so I can't implement Properties in my data object.
Nike Skover
Oh sorry, it's a comment for Aran Mulholland
Nike Skover
+1  A: 

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.

slugster
I can't understand how to realize INotifyPropertyChanged for array. For example, if I have a Customer class I just call NotifyPropertyChanged("Name") in a set function. What should I pass into NotifyPropertyChanged instead of "Name" if I set a one value in string[][] array?
Nike Skover
Thats kind of what i was implying - move away from the strin[][], instead use a List<some object> or ObservableCollection<some object>, and implement INotifyPropertyChange on the 'some object'. I don't know exactly what your data looks like, and it may seem like a bit of work for no real reason, but when using databinding and any sort of standard pattern such as MVVM/MVC/MVP then it really makes your task (and maintenance) a whole lot easier.
slugster
If however you are stuck with the string[][] approach, then i would look at how you delete and reinsert a particular row in the grid, as most grids i've seen don't allow you to call refresh at a row level (only the grid level).
slugster
I've changed string[][] with ObservableCollection<ObservableCollection<string>>, so now it works rather good. The only thing that bothering me is that I think when I change one element in my data object, ObservableCollection updates all cells in DataGrid.
Nike Skover
>>then i would look at how you delete and reinsert a particular row in the grid<< -- I don't know how to solve it without re-binding all columns.
Nike Skover
Try changing the outer ObservableCollection to a normal List, so you have List<ObservableCollection<string>> - List<> doesn't implement INotifyPropertyChanged.
slugster
A: 

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.

Aran Mulholland