By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

Manipulating XML and a DataGridView

P: n/a
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();
ds.ReadXml(@"\usr\tmp\sample.xml");
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
http://msdn2.microsoft.com/en-us/library/ms171628.aspx

Binding XML to a DataGridView
http://msdn.microsoft.com/library/de...ingXMLData.asp

Binding Objects to a DataGridView
http://www.codeproject.com/useritems/datagridview.asp

Binding to a DataGrid
http://www.codeproject.com/soap/AgConfig.asp
Aug 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
DataGridViews.

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
respectively.

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.

http://msdn.microsoft.com/library/de...us/vbcon/html/
vbwlkWalkthroughAccessingXMLData.asp

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 =
"len(au_fname)"/>

In the btnReadXML's click event handler, you add the following code before
"dsAuthors.ReadXml(filePath);":

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

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:

http://msdn.microsoft.com/library/de...us/dndive/html
/data05312002.asp

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

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

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
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

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

Aug 22 '06 #2

P: n/a
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.