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