• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 677
  • Last Modified:

Export DataGrid to XML

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
Cwinde01
Asked:
Cwinde01
  • 6
  • 5
1 Solution
 
DimandjaCommented:
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
 
Cwinde01Author Commented:
I am using Vb.Net 2003.  I don't think RenderControl is a member of the datagrid in 2003.  Any other suggestions?
0
 
Bob LearnedCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Cwinde01Author Commented:
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
 
Bob LearnedCommented:
Saving a DataSet to XML can be as easy as using DataSet.WriteXml, but the ArrayList is a different story all together.

Bob
0
 
Cwinde01Author Commented:
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
 
Bob LearnedCommented:
I need to find out the reason for mixing DataSet/DataTable and ArrayList as a binding source.

Bob
0
 
Cwinde01Author Commented:
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
 
Bob LearnedCommented:
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
 
Cwinde01Author Commented:
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
 
Bob LearnedCommented:
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
 
Cwinde01Author Commented:
It looks like that should work.

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now