ralphtrent
Programmer
Hello
I have a datagrid that I want to export to an excel file when the user clicks an image button. This works great, execept when I click the button, I need to rebind the datagrid. How do I avoid that. The datagrid is already bound and display, I just want to take that and save it to a file. I should say that this simple task works fine even though I need to rebind it, but I want to add an enhancement that will allow the users to click a checkbox on the datagrid header (one check box per column) to select the columns they want to export. I have teh checkbox there, but like i said when I click my image button, the datagrid is null'd as are the check box options.
Here is some code
I have a datagrid that I want to export to an excel file when the user clicks an image button. This works great, execept when I click the button, I need to rebind the datagrid. How do I avoid that. The datagrid is already bound and display, I just want to take that and save it to a file. I should say that this simple task works fine even though I need to rebind it, but I want to add an enhancement that will allow the users to click a checkbox on the datagrid header (one check box per column) to select the columns they want to export. I have teh checkbox there, but like i said when I click my image button, the datagrid is null'd as are the check box options.
Here is some code
Code:
protected void imgbtnExportToExcel_Click(object sender, ImageClickEventArgs e)
{
[tab]exportData(sender);
}
Code:
private void exportData(object sender)
{
[tab]if (sender.GetType().Name.EndsWith("ImageButton"))
[tab]{
[tab][tab]DataTable dt = new DataTable();
[tab][tab]TimeSpan ltsTimeToSpin, timeToDataBind, timeToCheckSLGs;
[tab][tab]if (Session["lastSerializedDataFile"] != null)
[tab][tab]{
[tab][tab][tab]dt.ReadXml(Convert.ToString(Session["lastSerializedDataFile"]));
[tab][tab][tab]displayResults(dt, "", out ltsTimeToSpin, out timeToDataBind, out timeToCheckSLGs);
[tab][tab][tab]dt = null;
[tab][tab][tab]if (((ImageButton)sender).ID.IndexOf("excel", StringComparison.CurrentCultureIgnoreCase) > -1)
[tab][tab][tab]{
[tab][tab][tab][tab]displayData.exportDataGridResults(dgResults, displayData.ExportType.Excel);
[tab][tab][tab]}
[tab][tab][tab]if (((ImageButton)sender).ID.IndexOf("word", StringComparison.CurrentCultureIgnoreCase) > -1)
[tab][tab][tab]{
[tab][tab][tab][tab]displayData.exportDataGridResults(dgResults, displayData.ExportType.Word);
[tab][tab][tab]}
[tab][tab]}
[tab]}
}