Using Microsoft's DataGrid to Export Data Instead of Native WebGrid

1 reply. Last post: November 20, 2011 4:07 PM by Yudi
Tags :
  • (None)
  • New Discussion
  • New Question
  • New Product Feedback

My company wishes to try to intercept the native Export to Excel on the WebGrid. They want to use Microsoft's DataGrid. I created a custom postback action.

When I take the datatable from the WebGrid, it only has the rows on the current page. Is there a way I can get all the rows? Here's a sample of the code. (It still doesn't work becuase when Response.End() occurs, I get a security error. Maybe you can help with this too. Even if I didn't get any rows, how can I trigger the web browser to recognize that I'm trying to send an excel file during  the WebGrid FlyPostBack?)

        void ctlGrid_InitializePostBack(object sender, PostbackEventArgs e)
        {
            if (e.Action == ISNet.WebUI.WebGrid.PostBackAction.Custom)
            {
                // double-check this is the GetCheckedRows button
                if (!string.IsNullOrEmpty( System.Web.HttpContext.Current.Request.Form["PrintToExcel"]))
                {
                    PrintToExcel(sender, e);
                }
            }
        }

        private void PrintToExcel(object sender, PostbackEventArgs e)
        {
            WebGrid grid = (WebGrid)sender;
            grid.LayoutSettings.PagingExportMode = ClassicPagingExportMode.ExportAllData;

            DataTable dt = ((System.Data.DataView)e.DataSource).Table;
            DataGrid dg = new DataGrid();
            dg.DataSource = dt;
            dg.DataBind();
            System.Web.HttpContext.Current.Response.ClearContent();
            string wgTableName = "Excel Export";
            System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + wgTableName);
            System.Web.HttpContext.Current.Response.ContentType = "application/excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            dg.RenderControl(htw);
            System.Web.HttpContext.Current.Response.Write(sw.ToString());
            System.Web.HttpContext.Current.Response.End();
            dg = null;
            dg.Dispose();   
            
        }

The reason we are trying to do this is it takes too long to create the excel file and when it does, it's currently limited to 65000 rows, which is not acceptable for our larger clients.

We are using WebGrid from WebStudioUI 2009 R2 SP1

All times are GMT -5. The time now is 4:54 PM.
Previous Next