How do I display data from 2 tables in a datagrid?

  • Thread starter Thread starter Jon Bosker
  • Start date Start date
J

Jon Bosker

Hi

My data is held in XML. I have 2 tables one with some time entry data
and the other with some project related data - here is the much
simplified version:

TimeEntries (table)
- StartTime (column)
- ProjectID (column)

Projects (table)
- ProjectID (column)
- ProjectName (column)

The two tables are related on ProjectID.

What I am trying to do is display my time entries with the ProjectName
in a datagrid (winforms) - spreadsheet style - like this:

StartTime | ProjectName
-------------------------
09:00 | MyProject

What is the best way to achieve this? I know I could create a new
dataset with a new datatable and brute-force the data in but that is
so ugly. I have tried creating a dataview and adding a new column but
when I saved the data back the new column was also there!

Surely there must be an elegant way to do this?!? Can anyone put me
out of my misery?

Regards, Jon
 
Thanks for the reply. I now believe that I need to use a
DataViewManager... something like this:

mySchema.ReadXml("MyFileName.xsd");
mySchema.Relations.Add("TimeProjects",
mySchema.Tables["Projects"].Columns["ProjectID"],
mySchema.Tables["TimeEntries"].Columns["ProjectID"]);
DataViewManager dvManager = new DataViewManager(mySchema);

But I still cannot figure out how to get the column into the datagrid -
doing the following would not include the ProjectName column:

this.dataGrid1.SetDataBinding(dvManager, "TimeEntries");

Can anyone offer any more advice?

Regards, Jon
 
Jon,

The most simple way is first doing after setting the relations to the
dataset.

datagrid1.DataSource = YourDataset;

After that you can start playing with the dataviewmanager
 
I am only starting to mess with the dataviews and dataviewmanagers
myself, but maybe this will help. There is a method of the
dataviewmanager that you can call the will return an instance of a
dataview based on the dataviewmanager. I think you have to bind the
dataview that is created from this method rather than trying to bind it
to the dataviewmanager object itself.

DataView myDV = dvManager.CreateDataView(???);
this.dataGrid1.DataSource = myDV;
From the samples I have played with so far, the ??? is a dataset. From
your code listed I don't what should go here.Anyway,maybe this will
help you move in the right direction.

- Andrew
 
Back
Top