1

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?

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
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.
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).
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.
>>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.
|
0

Re-bind the data array.

3 Comments

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)?
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.
Oh sorry, it's a comment for Aran Mulholland
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.