Solved

Export DataGrid to XML

Posted on 2006-11-29
12
659 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now