gjvdkamp
gjvdkamp

Reputation: 10516

WPF Datagrid binding to DataTable with complex type

I have a class that contains data from some model. This class has metadata along with the actual value.

class ServerValue {

    public int SomeId {get;}
    public int SomeOtherId {get;}
    public DateTime LastChanged {get;}

    public object Value {get;set;}

    // this lets me show the value, but how do i update it from the grid?
    public override string ToString(){
        return Value.ToString();
     }
}

Now I also have a class MyDataTable that derives from DataTable that has all kind of logic. It calls the server, gets a bunch of ServerValues and puts them into Rows and Columns.

Finally I have a WPF DataGrid that I bind to the MyDataTable and the data are displayed, because the DataGrid calls ToString on each ServerValue and gets back the value for display. Hurray so far.

Now, I want to have two way databinding, so input on the grid is written back to the ServerValue. So I want to bind the grid cells to the Value property of the ServerValue instead of the ServerValue itself.

Right now the ServerValue of the DataGrid cell is just replaced with a string. I could work around this and all but I'd to try the elegant route first.

So I have a datatable with a complex type in cells and i want two-way databinding to a specific property of that type.

Is this possible? I've been googling on this and i can't anything on this.

Thanks in advance,

John

Upvotes: 1

Views: 1796

Answers (1)

Johan Buret
Johan Buret

Reputation: 2634

What you want is a way to convert back and forth from your object to their text reprenstations.

Define a Converter for your Binding

http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

Upvotes: 1

Related Questions