I am doing a custom export to excel where I load the grid, say 10 items on the first page, 3 pages long. The user then filters the grid using the built in filter column headers. That limits the grid to 14 items in 2 pages. The user clicks my export button where the following gets executed:
I compare my full custom datatable with the items in the grid to decide which items in my datatable I will include in the export. This works great on a grid with no paging. The problem arises when I have more than one page. It appears that the count in
rgdProjectWorksheets.MasterTableView.Items only holds the items on the current page. How do I iterate through all the items in all pages?
I've tried running the GetVisibleIDList method in the PreRender event (as this post suggests), but that does not seem to be working.
Any help would be much appreciated.
Thanks
Kris
/// <summary> |
/// Exports data in grid to excel |
/// </summary> |
public void Export() |
{ |
//rgdProjectWorksheets.ExportSettings.ExportOnlyData = true; |
//rgdProjectWorksheets.ExportSettings.IgnorePaging = true; |
//rgdProjectWorksheets.MasterTableView.ExportToExcel(); |
PWDetailAccess pwa = new PWDetailAccess(); |
DataTable dt = pwa.GetFlatExportData(Convert.ToInt32(Session[ApplicationConstants.SessionVariables.OrganizationID].ToString())); |
List<int> idlist = GetVisibleIDList(); |
HttpContext context = HttpContext.Current; |
context.Response.Clear(); |
foreach (DataColumn column in dt.Columns) |
{ |
context.Response.Write(column.ColumnName + ","); |
} |
context.Response.Write(Environment.NewLine); |
foreach (DataRow row in dt.Rows) |
{ |
if (!idlist.Contains(Convert.ToInt32(row[PWDetailsSchema.ID].ToString()))) |
continue; |
for (int i = 0; i < dt.Columns.Count; i++) |
{ |
context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ","); |
} |
context.Response.Write(Environment.NewLine); |
} |
context.Response.ContentType = "text/csv"; |
context.Response.AppendHeader("Content-Disposition", "attachment; filename=SA_FlatExport.csv"); |
context.Response.End(); |
} |
private List<int> GetVisibleIDList() |
{ |
List<int> idlist = new List<int>(); |
for (int i = 0; i < rgdProjectWorksheets.MasterTableView.Items.Count; i++) |
{ |
if (rgdProjectWorksheets.Items[i].Visible == true) |
idlist.Add(Convert.ToInt32(rgdProjectWorksheets.Items[i][PWDetailsSchema.ID].Text)); |
} |
return idlist; |
} |
I compare my full custom datatable with the items in the grid to decide which items in my datatable I will include in the export. This works great on a grid with no paging. The problem arises when I have more than one page. It appears that the count in
rgdProjectWorksheets.MasterTableView.Items only holds the items on the current page. How do I iterate through all the items in all pages?
I've tried running the GetVisibleIDList method in the PreRender event (as this post suggests), but that does not seem to be working.
Any help would be much appreciated.
Thanks
Kris