sandeep kumar

sandeep kumar

  • NA
  • 58
  • 12k

GridView Export to Excel

May 15 2018 3:22 PM
Hi,
 
I have a grid view and want to export gridview data to excel. I am using SQLdatasource connection.
 
Here is my .CS code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.IO;  
  10. using System.Drawing;  
  11. namespace Camden_Homes  
  12. {  
  13. public partial class LLC : System.Web.UI.Page  
  14. {  
  15. static string previouspage = string.Empty;  
  16. protected void Page_Load(object sender, EventArgs e)  
  17. {  
  18. if (Session["username"] == null)  
  19. Response.Redirect("default.aspx");  
  20. Label1.Text = "username: " + Session["username"];  
  21. }  
  22. protected void LinkButton1_Click(object sender, EventArgs e)  
  23. {  
  24. Response.Redirect(previouspage);  
  25. }  
  26. protected void LinkButton2_Click(object sender, EventArgs e)  
  27. {  
  28. Response.Redirect("default.aspx");  
  29. }  
  30. protected void Button1_Click(object sender, EventArgs e)  
  31. {  
  32. Response.Clear();  
  33. Response.Buffer = true;  
  34. Response.AddHeader("content-disposition""attachment;filename=camdenexport.xls");  
  35. Response.Charset = "";  
  36. Response.ContentType = "application/vnd.ms-excel";  
  37. using (StringWriter sw = new StringWriter())  
  38. {  
  39. HtmlTextWriter hw = new HtmlTextWriter(sw);  
  40. GridView1.HeaderRow.BackColor = Color.White;  
  41. foreach (TableCell cell in GridView1.HeaderRow.Cells)  
  42. {  
  43. cell.BackColor = GridView1.HeaderStyle.BackColor;  
  44. }  
  45. foreach (GridViewRow row in GridView1.Rows)  
  46. {  
  47. row.BackColor = Color.White;  
  48. foreach(TableCell cell in row.Cells)  
  49. {  
  50. if (row.RowIndex % 2 ==0)  
  51. {  
  52. cell.BackColor = GridView1.AlternatingRowStyle.BackColor;  
  53. }  
  54. else  
  55. {  
  56. cell.BackColor = GridView1.RowStyle.BackColor;  
  57. }  
  58. cell.CssClass = "textmode";  
  59. }  
  60. }  
  61. GridView1.RenderControl(hw);  
  62. string style = @" .textmode { } ";  
  63. Response.Write(style);  
  64. Response.Output.Write(sw.ToString());  
  65. Response.Flush();  
  66. Response.End();  
  67. }  
  68. }  
  69. public override void VerifyRenderingInServerForm(Control control)  
  70. {  
  71. /*verifies that the control is rendered*/  
  72. }  
  73. }  
  74. }  
And I am getting error is
 
RegisterForEventValidation can only be called during Render();
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: RegisterForEventValidation can only be called during Render();
Source Error:
Line 63: }
Line 64: }
Line 65: GridView1.RenderControl(hw);
Line 66: string style = @" .textmode { } ";
Line 67: Response.Write(style);

Answers (1)