Solved

Export GridView contents to XML file or other format

Posted on 2011-09-07
5
626 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

15 Experts available now in Live!

Get 1:1 Help Now