473,769 Members | 3,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(@"\u sr\tmp\sample.x ml");
dataGridView.Da taSource = ds;
dataGridView.Da taMember = "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 7822
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 parentBindingSo urce and childBindingSou rce, and two
DataGridViews, i.e parentDataGridV iew and childDataGridVi ew onto your form.

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

this.parentBind ingSource.DataS ource = this.dataset1;
this.parentBind ingSource.DataM ember = "parentTabl e";

this.childBindi ngSource.DataSo urce = this.parentBind ingSource;
this.childBindi ngSource.DataMe mber = "relation1" ;

this.parentData GridView.DataSo urce = this.parentBind ingSource;
this.childDataG ridView.DataSou rce = this.childBindi ngSource;

Thus, when the program is running, the parentDataGridV iew shows the data in
the parentTable and the childDataGridVi ew 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/
vbwlkWalkthroug hAccessingXMLDa ta.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 ="computedcolum n" type ="xs:int" msdata:Expressi on =
"len(au_fna me)"/>

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

dsAuthors.ReadX mlSchema("Compl ete 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.as p

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
3629
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, and rebind the lstMyPersonnes to my DataGridView.dataSource, it shows the new record in the DataGridView as it should be. But when I click on a cell of that row, I suddenly got this error: "Index -1
10
39514
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 some DataGridViewTextBoxCell, DataGridViewComboBoxCell etc, i want to export all that to XML.. Any help is greatly appreciated.
3
32273
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 datatable without having to loop through each column in the datagridview? Or is there a way to retrieve the rows from the original datatable filtered by the dataview into a separate table? I only want to copy the rows from the main table that...
2
24558
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 you update after closing the grid/form, etc.? Also, can you tell me the best book to buy that fully explains the DataGridView control? Thanks.
7
12631
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 the background for my question... Before I switched my application over to the Fx 2.0, I used a DataGrid to display my data. I would store different DataGridTableStyles (each one with a custom set of columns) in the DataGrid.TableStyles property...
7
15658
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 there an equivalent property for the DataGridView? I have searched, but have not found one. I would like the user to be able to see all the columns of the table on one screen - thus eliminating the need to use the horizontal scroll bar to view...
0
2506
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 my.
3
5744
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 few celltemplate property getters and combobox/datecombo constructor calls without database access which does not take a lot of time. Debug output (below) shows lot of messages Stepping over non-user code. Running in release mode from...
0
5657
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 blank unless they are filled by the user. 2) As soon as the user enters a cell, the dtp control should appear as the editing control of that cell. If there's a value in the cell beforehand, that value is set as the value of the dtp editing control...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.