Solved

Export GridView contents to XML file or other format

Posted on 2011-09-07
5
622 Views
Last Modified: 2012-08-13
Hi,

I am using a component one as.net gridview. This will be used to display data from a variety of sources. I would like to be able to use the component one ReportViewer control to allow the user to view, print and export the contents of the GridView. This is so they can use the in-built features of the GridView control to filter, group, etc.

My thinking at the moment is I will need to somehow create an XML file based upon the format and data of the GridView and then use this XML as the source for the ReportViewer.

Does anyone have any suggestions on the best way to approach this or is my thinking the best way? Any suggestions appreciated.
0
Comment
Question by:MooksUK
  • 3
  • 2
5 Comments
 

Author Comment

by:MooksUK
ID: 36497280
In addition to the above. I believe I will have to create an RDL file which will be used to format the report and then save the data in an XML file. These can then be used by the ReportViewer. This seems a lengthy approach and would be very interested in any quicker way of achieving the same.
0
 
LVL 12

Expert Comment

by:starlite551
ID: 36505974
Try this code :
public static string GetXMLGridView(GridView gridview)
{
      DataTable dt = gridview.DataSource as DataTable;
      DataSet ds = new DataSet();
      ds.Tables.Add(dt);      
      string xmlContent = ds.GetXml();      
      return xmlContent;
}

Open in new window

0
 
LVL 12

Expert Comment

by:starlite551
ID: 36505997
The above Method returns the content of a GridView in XML format string. Without starting tags which specify the version of Xml used.
0
 
LVL 12

Accepted Solution

by:
starlite551 earned 500 total points
ID: 36506112
You can create a physical XML File using this code :
ds.WriteXml(@"C:\MyXMLFile.xml");

Open in new window

public static void GetXMLGridView(GridView gridview,string filePath)
{
      DataTable dt = gridview.DataSource as DataTable;
      DataSet ds = new DataSet();
      ds.Tables.Add(dt);      
      ds.WriteXml(filePath);            
}

Open in new window


Method Usage :
 
GetXMLGridView(gridview,@"C:\MyXML.xml");

Open in new window

0
 

Author Closing Comment

by:MooksUK
ID: 36530523
Thanks, this answered part of the problem. I will look to produce the XSD file from it as well so that this can be used by the report viewer component.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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