Tutorial :wpf datagrid observablecollection



Question:

I'm working on a large database application using WPF. So far, I have been able to create a DataContext using Link to Sql class, store it in an ObservableCollection, and feed it to my DataGrid as DataContext.

The main table in SQL I'm trying to read/write looks like this

Work_Table    WorkID[pk int] | frn_CustomerID[fk int] | frn_UserID[fk int] | DateCreated[datetime] | etc...    Customers_Table    CustomerID[pk int] | CustomerName[varchar] | etc...    Users_Table    UserID[pk int] | UserName[varchar] | etc...  

I want to represent these columns in my DataGrid by using DataTemplates and Inline editing

WorkID | CustomerName | UserName | DateCreated  

Do I have to use a Proxy Class that contains all these columns and ids? Should I modify my observablecollection, or should I handle everything in the code behind based on events? I like the fact that the datagrid does basic CRUD which is in fact great since I'm on a time constraint at the moment.

I am fairly new to WPF and particularly to the DataGrid control. I also have read about IValueConverters but I'm not exactly sure if this is the way to go and what performance issue I might encounter.

If anyone can point me in the right direction, so far, all the searches I found are simple tables without the use of foreign keys, etc.


UPDATE
I found this code so far and had some progress but now the issue is sorting.

<toolkit:DataGridTemplateColumn Header="Customer Name">       <toolkit:DataGridTemplateColumn.CellTemplate>           <DataTemplate>               <ComboBox ItemsSource="{Binding Customers, Source={StaticResource MyDataContext}}" DisplayMemberPath="CustomerName" SelectedValuePath="Customer_ID" SelectedValue="{Binding Path=Customer_ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  />           </DataTemplate>       </toolkit:DataGridTemplateColumn.CellTemplate>   </toolkit:DataGridTemplateColumn>`  

This took care of being able to display the "Customer Name" instead of just the Customer_ID. Of course, I want to keep the underlying data as an integer but display a User Readable description. Now the problem is sorting. I'm just wondering if there's a way to do this very cleanly. I assume this is a common scenario but I haven't been successful finding a solution. I guess maybe I'm looking for the wrong search terms.

To simplify things, here's what I have so far:

1.) I have a table called Work_Table that contains Work_ID, Customer_ID, Employee_ID etc..
2.) I have another lookup table called Customers_Table that contains Customer_ID, CustomerName, etc..
3.) I have to display these columns in a DataGrid - Work_ID, CustomerName, etc...

I have an ObservableCollection that my DataGrid is bound to. Before I tried the combobox approach, it displayed Work_ID, Customer_ID, Employee_ID, etc.. basically the whole table and its columns.

The question is what is the best way for me to do item 3.)? Should I bind my DataGrid to a custom ObservableCollection that only contains columns I needed? Is IValueConverter another feasible solution so when the DataGrid is about to display the Customer_ID it will display CustomerName instead?

I did not have this problem before until I normalized the database.


Solution:1

I think this is probably what you are looking for.

http://blogs.msdn.com/vinsibal/archive/2008/08/19/wpf-datagrid-stock-and-template-columns.aspx


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »