Datagrid - renaming cells with usefull data

yogiwales

Newcomer
Joined
Jan 23, 2003
Messages
21
Location
Cardiff, Wales - UK
Hello again!

Rite, I have many classes such as orders and customers each assigned with their own primary key to keep the program modular. When I bind a collection to a datagrid it currently shows the Primary Keys (ID) in the boxes and I want them to be replaced by the actual text name for instance:

CURRENTLY
--------------

Order ID Member ID Invoice Date
1 1 22/4/03

WANTED
-----------
Order ID Member Invoice Date
1 Dave 22/4/3

I have a member collection which has dave stored in it as Member ID = 1.

Thanks
Yogi
 
You can't do this (that I know of) directly. The easiest solution would be to add an expression column to your Order table (I would assume that's the one that has the MemberID and you want to display Member Name). The expression column would get the Member Name from the Member table. Assuming you have a relationship set up between the two tables in your DataSet, the expression would be very simple, something like "Child(OrderMember.MemberName)", depending on your column and relationship names.

There are trickier options of course. One involves developing your own (or buying) a combobox type of control. You can bind it to the MemberID and have it show the text of the Member Name from the Members table.

Another option is to use Microsoft's unsupported JoinView class that basically creates a SQL Server like "view" of data. Similar to a DataView, only it joins multiple tables into one bindable class. Search MSDN for JoinView for the VB.NET sourcecode.

-Ner
 
I should have asked - are you using custom collection objects to bind to your DataGrid or a DataSet? If a DataSet, how are you creating and filling it? If it's something else, I'll need to see the code.

If you are using a DataSet and want to start, just add a new column to your table. For example:
C#:
DataColumn c = dataset1.Tables["Table1"].Columns.Add("MemberName", typeof(string), "Child(OrderMember.MemberName)");

I don't know what you relationship or column names are, so I faked them above.

-Ner
 
Ah, well then - if you control the collections, I think you'll have to manually add a new field to the "parent" object type. So if the Order object currently has a field MemberID, add a field called MemberName and manually fill it. Then hide the MemberID column in the grid.

The other solution, which might be better if you have a lot of tables with these foreign key relationships, would be to buy or create a custom control that can bind to the ID and display the text from another table. This is usually a combobox of some kind.

Here's are some links to look at:
Link 1
Link 2

They have code you can use to create your own ComboBox control that fits in a grid.

-Ner
 
Back
Top