Solved

Export DataGrid to XML

Posted on 2006-11-29
12
664 Views
Last Modified: 2008-02-01
I am looking for a way to export a datagrid to xml regardless of its source type.  I have several datagrids where their source types are of type DataTable and ArrayList.  I was attempting to write a class where the datagrid would be passed and the class would handle the export.  Any suggestions?
0
Comment
Question by:Cwinde01
  • 6
  • 5
12 Comments
 
LVL 7

Expert Comment

by:Dimandja
ID: 18040150
Try http://tinyurl.com/ufc87

In VB.NET, the code would be:

         Dim sw as new StringWriter()
         Dim hw as new  HtmlTextWriter(sw)
         DataGrid1.RenderControl(hw)

         With hw
            .Indentation = 4
            .WriteEndDocumnet()
         End with

         hw.Close()
         MsgBox(sw.ToString())


 
0
 

Author Comment

by:Cwinde01
ID: 18040211
I am using Vb.Net 2003.  I don't think RenderControl is a member of the datagrid in 2003.  Any other suggestions?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18044841
There was an assumption made--that you were using the DataGrid web control with an ASP.NET application.  If you are using the WinForms DataGrid, then how are you binding the DataGrid?  DataTable?  DataSet?

Bob
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Cwinde01
ID: 18045569
There are several grids that are loaded.  Most of the time we will set datagrid.datasource = dataset.tables("TableName") and other times we will set datagrid.datasource = ArrayList.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18045598
Saving a DataSet to XML can be as easy as using DataSet.WriteXml, but the ArrayList is a different story all together.

Bob
0
 

Author Comment

by:Cwinde01
ID: 18045712
That is what I was afraid of.  I have used the DataSet.WriteXml before but I was trying to find a way to extract the data from the datagrid.datasource.  Maybe another solution might be to set the datagrid.datasource to a dataset somehow.  But I have not been able to do that either.  I tried CType(Me.DataGrid.DataSource, DataTable), but when the arraylist is the datasource CType is unable to convert it into a DataTable.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18046788
I need to find out the reason for mixing DataSet/DataTable and ArrayList as a binding source.

Bob
0
 

Author Comment

by:Cwinde01
ID: 18047183
Sorry for the confusion.  I have an application that has several datagrids.  

* Some of the datagrids are loaded using a DataSet.  

* Then there are other datagrids that are loaded using a dataview because I need to filter the dataset based on the selection made from another datagrid.  

* Then I have one instance where I have a datagrid that has two different datasources.  I query the first datasource and load the results into a arraylist then I query the second source and load the results in the same arraylist.  Then assign the arraylist to the datagrid.datasource so that it appears as one source.  I realize now that the data should have been loaded manualy using a datatable.  I was hoping that I would not have to rewrite the piece that loads the arraylist.

I have one scenario where I want to show all the different grids on one xml page and show a summary for each grid.  I would have a style sheet to handle the summary but  I need to get the different grids into the same doc.

Hope that helps.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18047758
Hmmm, maybe you could something like this:

  Dim dv1 As New DataView(ds.Tables("Table1"))
  Dim dv2 As New DataView(ds.Tables("Table2"))

  dv1.RowFilter = "filter goes here"
  dv2.RowFilter = "filter goes here"

  Me.DataGrid1.DataSource = dv1
  Me.DataGrid2.DataSource = dv2

Then, you could create an XML file from the DataView.  You would only need to create one method to create the file.

Bob
0
 

Author Comment

by:Cwinde01
ID: 18047865
It appears that I will need to convert everthing to a dataview or a datatable, is that correct?

I guess there is no way to extrapolate the datagrid.datasource to some other type of datasource? (arraylist, datatable, dataview)
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 18047955
1) You don't need an ArrayList

2) The DataView constructor takes a DataTable as an argument

3) With the DataView you can apply different filters for each separate instance

4) If you bind all DataGrid controls to a DataView, then you can retrieve the DataSource this way:

  Dim dv As DataView = CType(CType(source, DataGrid).Datasource, DataView)

Bob
0
 

Author Comment

by:Cwinde01
ID: 18047975
It looks like that should work.

Thank you for your help and explanations.  Your patience has been greatly appreciated.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question