Related data in datagrid _without_ drill-down?

Related data in datagrid _without_ drill-down?

Post by Jeppe Jesp » Wed, 02 Nov 2005 17:59:12


I have two tables: Customers and Orders.
Orders rows are related to Customer row through FK_CustomerID in the Orders
table.

I want to display orders in a dataGrid, but i want to show the Customers
NAME, not ID in the grids rows.

So an order looks like this in my grid:

OrderID Customer OrderDate OrderTotal
----------------------------------------------------------
1 Alfreds F.. May 7 15.00


....instead of....

OrderID Customer OrderDate OrderTotal
----------------------------------------------------------
1 231 May 7 15.00


What's the best way? Can individual cells in a row be bound to data from two
different dataTables which ofcourse are related?

Thanks,
J.Jespersen
Denmark
 
 
 

Related data in datagrid _without_ drill-down?

Post by Bajo » Wed, 02 Nov 2005 18:33:33

Dear Jespersen,
you can do this but its a bit complex and
you may have to create 2-3 clolumns yourself. In my option make a class
just put 4-5 properties(which columns you need) in that class.fill this
class with data from the tables for each row and add it to the list.
Assign that list to the DataGrid as dataSource.

I hope it helps.

Regards,
Naveed Ahmad Bajwa

 
 
 

Related data in datagrid _without_ drill-down?

Post by Jeppe Jesp » Wed, 02 Nov 2005 21:12:11

Can you elaborate on this. Still kinda lost on both of those...
 
 
 

Related data in datagrid _without_ drill-down?

Post by Bajo » Wed, 02 Nov 2005 21:38:31

Suppose you need OrderID,Customer, OrderDate,OrderTotal in the
DataGrid. Now make a class with these properties
class foo
{
// declear variables
int orderID;
string customer;
...so on for each column you need in grid
// properties
public int OrderID
{
get
{
return orderID;
}
set
{
orderID=value;
}
}
.. so on for each column you need in grid
}

write the code which contains the dataGrid

ArraList list = new ArrayList();
Foo foo = new Foo();
while (not end of data)
{
foo = new Foo();
foo.OrderID = data.GetInt(0);
.. so on for each column
list.add(foo);
}
dataGrid.DataSource = list;
}

I hope its clear now.

Regards,
Naveed Ahmad Bajwa
 
 
 

Related data in datagrid _without_ drill-down?

Post by Bart Mermu » Wed, 02 Nov 2005 22:02:56

Hi,




Are the DataTable's inside a DataSet and did you add a relation between them
? Customer should be the parent and Orders the child.
ds.Relations.Add( "CustomerOrders",
ds.Tables["Customers"].Columns["CustomerID"],
ds.Tables["Orders"].Column["FK_CustomerID"] );

Then you can add an expression column to the Orrders so it gets the name
from its parent Customer:
ds.Tables["Orders"].Columns.Add(
"Customer", typeof(string), "Parent(CustomerOrders).Name");

Then you can hide the foreign key column, see following link on how to hide
columns:
http://www.yqcomputer.com/ ;en-us;317951

There is one problem, DataTable with expression columns may give error when
updating, so you need to remove them before update and re-add them after
update.

HTH,
Greetings