Creating .Excel File from a DatagridVIew

In order to fetch all the data from the grid view and create a .xls file of the data, use the following code .

private void CreateXLS()
{
DataTable dt = (DataTable)dgSearch .DataSource;
dt.WriteXml(@"C:\ABC.xls",System.Data.XmlWriteMode.IgnoreSchema);
dt.Dispose();
}


In case you get an error while converting the dgsearch.datasource to Datatable, convert the dgsearch.datasource to dataview and by using the property of dataview ie "Totable" convert it to datatable

Comments

Popular posts from this blog

SharePoint 2010 Migration Woes: Importing and exporting lists template between different SP2010 Servers

Per-user throttling in SharePoint Online: a Recipe for getting blocked in SharePoint Online and way to Avoid it

Powershell: Filling up an Existing Excel Sheet with data from SQL Server