DataGrid, Relations and Binding

H

Harry

Hello,
I have a DataSet built from a .XSD that contains relations. For example
consider the XML fragment below:

<Apples>
<Orange ID="A"/>
<Orange ID="R"/>
</Apples>
<Apples>
<Orange ID="QQ"/>
<Apples>

An apple row would have a relation called GetOrangeRows that returns a
strongly typed array of OrangeRows that are the child nodes of a specific
Apples row. My question is how can I get a DataGrid to only display Orange
rows that are children of a specific Apples row. If I simply set the
datagrids datasource to myDataSet.Apples[0].GetOrangeRows() I'm unable to
add or delete rows and the column headers are not what I specified them to
be. Any advice would be much appreciated.
 
G

Guest

Assuming you have a relation (I'll call mine "Cust2Orders"), you can use the
GetChildRows method of the Row object to get the parent's child rows. This
yeilds a DataRow array, which is not directly bindable (at lest not how you
want it to be) to a DataGrid.

But you can use the DataRowView's CreateChildView method and create a new
DataView out of it. Check out the following code snippet which gets the child
rows (orders) for a specific parent row (customers). This should do it for
you.

// Assuming you already have a DataSet with
// 2 DataTable objects in it, craete the relation
ds.Relations.Add("Custs2Orders", ds.Tables["Customers"].Columns["CustomerID"],
ds.Tables["Orders"].Columns["CustomerID"]);

// Create a DataView from the parent table
DataView vw = new DataView(ds.Tables["Customers"]);

// Create a DataRowView from the parent's first row
DataRowView drv = vw[0];

// Create a DataView using the selected parent row's children.
DataView vw2 = drv.CreateChildView("Custs2Orders");

// Now bind it
DataGrid1.DataSource = vw2;
DataGrid1.DataBind();


// John Papa
// http://codebetter.com/blogs/john.papa
 
H

Harry

John,

Thank you very much for your quick reply. Looking at your advice and code
example this seems to be exactly what I was looking for. I was clued into
using a DataView, however, I was uncertain how to use this class to obtain
my desired end result. Thanks again for your help!

John Papa said:
Assuming you have a relation (I'll call mine "Cust2Orders"), you can use the
GetChildRows method of the Row object to get the parent's child rows. This
yeilds a DataRow array, which is not directly bindable (at lest not how you
want it to be) to a DataGrid.

But you can use the DataRowView's CreateChildView method and create a new
DataView out of it. Check out the following code snippet which gets the child
rows (orders) for a specific parent row (customers). This should do it for
you.

// Assuming you already have a DataSet with
// 2 DataTable objects in it, craete the relation
ds.Relations.Add("Custs2Orders", ds.Tables["Customers"].Columns["CustomerID"],
ds.Tables["Orders"].Columns["CustomerID"]);

// Create a DataView from the parent table
DataView vw = new DataView(ds.Tables["Customers"]);

// Create a DataRowView from the parent's first row
DataRowView drv = vw[0];

// Create a DataView using the selected parent row's children.
DataView vw2 = drv.CreateChildView("Custs2Orders");

// Now bind it
DataGrid1.DataSource = vw2;
DataGrid1.DataBind();


// John Papa
// http://codebetter.com/blogs/john.papa


Harry said:
Hello,
I have a DataSet built from a .XSD that contains relations. For example
consider the XML fragment below:

<Apples>
<Orange ID="A"/>
<Orange ID="R"/>
</Apples>
<Apples>
<Orange ID="QQ"/>
<Apples>

An apple row would have a relation called GetOrangeRows that returns a
strongly typed array of OrangeRows that are the child nodes of a specific
Apples row. My question is how can I get a DataGrid to only display Orange
rows that are children of a specific Apples row. If I simply set the
datagrids datasource to myDataSet.Apples[0].GetOrangeRows() I'm unable to
add or delete rows and the column headers are not what I specified them to
be. Any advice would be much appreciated.
 
G

Guest

FYI, I have had this question a lot recently so I threw together an example
on my blog. You can find it here:
http://codebetter.com/blogs/john.papa/archive/2005/04/08/61697.aspx


// John Papa
// http://codebetter.com/blogs/john.papa


Harry said:
John,

Thank you very much for your quick reply. Looking at your advice and code
example this seems to be exactly what I was looking for. I was clued into
using a DataView, however, I was uncertain how to use this class to obtain
my desired end result. Thanks again for your help!

John Papa said:
Assuming you have a relation (I'll call mine "Cust2Orders"), you can use the
GetChildRows method of the Row object to get the parent's child rows. This
yeilds a DataRow array, which is not directly bindable (at lest not how you
want it to be) to a DataGrid.

But you can use the DataRowView's CreateChildView method and create a new
DataView out of it. Check out the following code snippet which gets the child
rows (orders) for a specific parent row (customers). This should do it for
you.

// Assuming you already have a DataSet with
// 2 DataTable objects in it, craete the relation
ds.Relations.Add("Custs2Orders", ds.Tables["Customers"].Columns["CustomerID"],
ds.Tables["Orders"].Columns["CustomerID"]);

// Create a DataView from the parent table
DataView vw = new DataView(ds.Tables["Customers"]);

// Create a DataRowView from the parent's first row
DataRowView drv = vw[0];

// Create a DataView using the selected parent row's children.
DataView vw2 = drv.CreateChildView("Custs2Orders");

// Now bind it
DataGrid1.DataSource = vw2;
DataGrid1.DataBind();


// John Papa
// http://codebetter.com/blogs/john.papa


Harry said:
Hello,
I have a DataSet built from a .XSD that contains relations. For example
consider the XML fragment below:

<Apples>
<Orange ID="A"/>
<Orange ID="R"/>
</Apples>
<Apples>
<Orange ID="QQ"/>
<Apples>

An apple row would have a relation called GetOrangeRows that returns a
strongly typed array of OrangeRows that are the child nodes of a specific
Apples row. My question is how can I get a DataGrid to only display Orange
rows that are children of a specific Apples row. If I simply set the
datagrids datasource to myDataSet.Apples[0].GetOrangeRows() I'm unable to
add or delete rows and the column headers are not what I specified them to
be. Any advice would be much appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top