Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Reading from XML and bound to DatagridView : Adding rows dynamically
 

Reading from XML and bound to DatagridView : Adding rows dynamically

Hi All,

.NET 2.0, Win Forms, C#.NET,DataGridView control, XML datasource.

I am using XML as a Data Source and try to bind the source into the DataGridView control in WIN forms. I have the XML source which has something like this...

<EventsGroups>
<
Event>
<
EventName>Event</EventName>
<
EventDescription>Event description</EventDescription>
</
Event>
</EventsGroups>

The description should bound to the Description column which i defined in the datagridview control...how to do that ...?? moreover i want to CREATE ROWS with description reading from theXMLdynamically ....

It would be great ...if anyone help me in this regard....you please share the code to do the same??

Thanks in advance.

Thanks,
Kathir

Kathirvel  Friday, March 02, 2007 4:42 PM

Simply like this( without XMLSchema).

System.Xml.XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument();
xmlDoc.DataSet.ReadXml("fullpath\\filename");
dataGridView1.DataSource = xmlDoc.DataSet;
dataGridView1.DataMember = "Event";

If you want to edit columnsof datagridview( you defined the column ?), you should make XMLSchema and need to add XMLSchema to your project.

ke1  Saturday, March 03, 2007 11:36 AM

Simply like this( without XMLSchema).

System.Xml.XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument();
xmlDoc.DataSet.ReadXml("fullpath\\filename");
dataGridView1.DataSource = xmlDoc.DataSet;
dataGridView1.DataMember = "Event";

If you want to edit columnsof datagridview( you defined the column ?), you should make XMLSchema and need to add XMLSchema to your project.

ke1  Saturday, March 03, 2007 11:36 AM

Thanks a lot. I have did something in different way ..read the xml and put the decription in the arraylist and created the columns and rows on the fly and push all the description with the for loop.....

any how i will try your code ..must be simple also..i think....cooool.

Kathirvel  Saturday, March 03, 2007 12:52 PM

But if i want to display all columns of xml file in the datagridview? In my example there areone element nested in another,andlast nests these two mentioned. Every element contains attributes, that needs to be displayed in datadridview.I want to display every elements attributes in datagridview.

I want to do that for testing puposes, to comparesuch methodwith retrieving data from relational tables.

There can be a lot of instances of complexes, objects and estimates.

<complex complexid="1" complexname="pavadin" complexUsercode="1" customer="klientas1" contractDate="2007.02.02" contractCode="852432211" contractNumber="821364">

<object objectname="1" objectid="1" objectUsercode="852432211" objectNumber="2">

<estimate estimateID="5" estimateName="pavad" priceLevel="2" estimateUserCode="5646" estimateNumber="5646" />

<estimate estimateID="5" estimateName="pavad" priceLevel="2" estimateUserCode="5646" estimateNumber="5646" />

<estimate estimateID="5" estimateName="pavad" priceLevel="2" estimateUserCode="5646" estimateNumber="5646" />

krepsininkas  Saturday, April 28, 2007 3:17 PM

Hi.

If you want to display every elements attributes in one datagridview, you can not do so with using dataset of XmlDocument or XmlDataDocument, I think.

In nested xml element, dataset create some relational tables.

for example

<Sample>

<elem1>aaa</elem1>

<elem2>bbb</elem2>

<elem2>ccc</elem2>

<elem3>

<elem4>ddd</elem4>

<elem5>eee</elem5>

</elem3>

</Sample>

In this case, dataset creates 3 tables which name is "Sample", "elem2", "elem3".

And datagridview is binding to one table, so needs 3 datagridviews for displaying 3 tables.

like this

  XmlDataDocument myxml = new XmlDataDocument();
   myxml.DataSet.ReadXml("path\\failename");
   dataGridView1.DataSource = myxml.DataSet;
   dataGridView2.DataSource = myxml.DataSet;
   dataGridView3.DataSource = myxml.DataSet;
   dataGridView1.DataMember = "Sample";
   dataGridView2.DataMember = "Sample.Sample_elem2";
   dataGridView3.DataMember = "Sample.Sample_elem3";

If element have some attributes, it will display with no coding.

See more info

http://msdn2.microsoft.com/en-us/library/dz35xd55(VS.80).aspx

http://msdn2.microsoft.com/en-us/library/3b4194wc(VS.80).aspx

ke1  Sunday, April 29, 2007 6:16 PM

You can use google to search for other answers

Custom Search

More Threads

• Supress drawing the the datagrid row header
• "copy if newer" database property not working!!!!!!!!!
• How to know the parent row index of a cell in Data Grid View?
• Search form dynamic sql
• how to update Datagridview after editing details in edit form?
• Detecting changes in data
• Combobox selected value does not show correctly with master/detail combo binding..
• datagrid
• Help convert Control.ControlCollection => List<Control>
• datagridview delete rows problem