473,320 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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();
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
2 7796
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DraguVaso | last post by:
Hi, I'm using the DataGridView in VB.NET 2.0. The DataSource is a Generic List of a custom class0: lstMyPersonnes = New List(Of clsPersonne). When I add a new clsPersonne to lstMyPersonnes,...
10
by: Henok Girma | last post by:
Hello Gurus, I want to save the state of an unbound DataGridView on my Windows Form application to an XML file so i can later load it back.. Basically, on my form I have a DataGridView, it's got...
3
by: Rich | last post by:
Hello, I am populating a datagridview from a datatable and filtering the number of rows with a dataview object. Is there a way to retrieve the rows displayed by the datagridview into a separate...
2
by: bob | last post by:
Can anyone tell me the best way to update a dataset while it is being edited/viewed in the DataGridView control? Is this something that should be inserted into one of the grid's events? or should...
7
by: Mitchell S. Honnert | last post by:
Is there an equivalent of the DataGrid's DataGridTableStyle for the DataGridView? If not, is there an easy way to duplicate the DataGridTableStyle's functionality for the DataGridView? Here's...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
0
by: jeastman - Hotmail | last post by:
Hello world Excuse, not to be written English and it helps me with a translator. I am new programming in C#. I made a control inheriting the DataGridView to be able to add controls done by...
3
by: Andrus | last post by:
I have DataGridView in virtual mode containing 3500 rows. In code below, assigning to RowCount value to 3500 takes 8 seconds. CPU usage goes high at this time. Stepping by F11 into user code shows...
0
by: priyamtheone | last post by:
I'm trying to make a datagridview column to act like a datetimepicker column (C#.Net 2005). These are the behaviours that the dgv should have: 1) Initially all the cells of the dtp column should be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.