470,647 Members | 1,328 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

Manipulating XML and a DataGridView

I have been trying to figure out how to use DataSets, BindingSources,
DataGridViews, and XML together, but it is a challenge. I understand how
to populate a DataGridView with XML basically as:

DataSet ds = new DataSet();
dataGridView.DataSource = ds;
dataGridView.DataMember = "targetElement";

What I found through experimentation is that the DataMember may specify
any element in the XML, and the DataGridView will then display any of its
children that are text nodes.

Question 1: Is there a code sample that emulates the hierarchical behavior
handled by the older DataGrid component? I understand that a single
DataGridView cannot do this, but I am sure someone must have modeled it
with two DataGridViews.

Question 2: In my DataGridView, the XML elements that I want to display as
columns are at different hierarchical levels. What is the "best practice"
for building a custom DataSet?

Question 3 (extending question 2): How would I also included new, computed
columns based on other fields in the XML?

For those interested, here are some useful references I have uncovered in
my explorations of this topic:

DataGrid vs DataGrid View

Binding XML to a DataGridView

Binding Objects to a DataGridView

Binding to a DataGrid
Aug 21 '06 #1
2 7568
Hi Michael,

Question 1:

Yes, you are right. DataGridView doesn't support the function to show
hierarchical data in one DataGridView control. But we could do this with
two DataGridViews. One DataGridView shows data in the parent DataTable and
the other shows the corresponding data in the child DataTable.

Below is a walkthrough.

1. Set up a WinForms application. Ceate a DataSet in the project, add two
DataTables into the DataSet,i.e parentTable and childTable and set up a
relationship between the DataTables, i.e relation1.

2. Add an instance of the DataSet, i.e dataset1, two BindingSource
instances , i.e parentBindingSource and childBindingSource, and two
DataGridViews, i.e parentDataGridView and childDataGridView onto your form.

3. Use the following code to bind the two BindingSources and two

this.parentBindingSource.DataSource = this.dataset1;
this.parentBindingSource.DataMember = "parentTable";

this.childBindingSource.DataSource = this.parentBindingSource;
this.childBindingSource.DataMember = "relation1";

this.parentDataGridView.DataSource = this.parentBindingSource;
this.childDataGridView.DataSource = this.childBindingSource;

Thus, when the program is running, the parentDataGridView shows the data in
the parentTable and the childDataGridView shows the corresponding data in
the childTable.

Question 2:

To display the XML elements that are at different hierarchical levels as
columns, I recommend you to separate these XML elements into two DataTables
and use two DataGridViews to display the data in the two DataTables

Question 3:

I recommend you to add a XML schema file into the project to define the
computed column in the schema file. I will take the project in the
following link for example to demonstrate how to do this.


When you running the sample program in the above link and click on the
"show schema" button, the XML schema information is displayed in the
textbox1. To add a computed element in the XML file, you could add an XML
Schema file into the project, clear the content in this schema file and
copy the content in the textbox1 to this file. Then you could add a
computed element in the schema file like below.

<xs:element name ="computedcolumn" type ="xs:int" msdata:Expression =

In the btnReadXML's click event handler, you add the following code before

dsAuthors.ReadXmlSchema("Complete path where you saved the XML Schema

Thus, the computed element will displayed in the DataGridView when the
program is running.

For more information on the expression of computed column, you may refer to
" The Syntax of Expressions" section in the following link:


Hope this helps.
If you have anything unclear, please feel free to let me know.
Linda Liu
Microsoft Online Community Support

Get notification to my posts through email? Please refer to

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 22 '06 #2
Thank you for the in-depth commentary. Some of that I was aware of, some
not. I will see what I can apply to my problem at hand.
Aug 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by DraguVaso | last post: by
10 posts views Thread by Henok Girma | last post: by
7 posts views Thread by Mitchell S. Honnert | last post: by
7 posts views Thread by =?Utf-8?B?TG9zdEluTUQ=?= | last post: by
reply views Thread by jeastman - Hotmail | last post: by
3 posts views Thread by Andrus | last post: by
1 post views Thread by Korara | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.