export from datagrid to excel in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Narz1986
    New Member
    • Dec 2008
    • 10

    #1

    export from datagrid to excel in c#

    hi...i m trying to export data from the datagrid to excel...here is my code

    Code:
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + exportfile.FileName + "");
    HttpContext.Current.Response.Charset = "";
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    HttpContext.Current.Response.ContentType = "application/vnd.xls";
    
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    //DatagridImport.
    DatagridImport.RenderControl(htmlWrite);
    HttpContext.Current.Response.Write(stringWrite.ToString());
    HttpContext.Current.Response.End();
    but im getting an error datagridview does not contain a definition for RenderControl.. ..any help wil be very useful..
    thank you
    Regards
    Last edited by Frinavale; Jan 21 '09, 04:05 PM. Reason: Moved to ASP.NET Answers from C# and added [code] tags
  • sangam56
    New Member
    • Nov 2007
    • 68

    #2
    export from datagrid to excel in C#

    All GridView, DataGrid, DataList support RenderControl method. Since you are trying to export datagrid, why don't you use DataGrid control. Hope this helps.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Have you bound the DataGrid to a DataSource?

      I don't know why you would be getting such a message because, like sangam56 has stated, the DataGrid has a RenderControl method.

      Comment

      Working...