Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why error is giving int the line gvdetails.RenderControl(htw);

My cs code is given below

C#
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
//Change the Header Row back to white color
gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < gvdetails.HeaderRow.Cells.Count; i++)
   {
   gvdetails.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
   }
int j = 1;
//This loop is used to apply stlye to cells based on particular row
foreach (GridViewRow gvrow in gvdetails.Rows)
   {
   //gvrow.BackColor = Color.White;
   if (j <= gvdetails.Rows.Count)
      {
      if (j % 2 != 0)
         {
         for (int k = 0; k < gvrow.Cells.Count; k++)
            {
            gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
            }
         }
      }
   j++;
   }
gvdetails.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}



[edit]Code block added, "Treat my content as plain text..." option disabled, code formatted - OriginalGriff[/edit]
Posted
Updated 30-Dec-11 21:33pm
v2
Comments
OriginalGriff 31-Dec-11 3:34am    
What error are you getting?
Janardan Pandey 31-Dec-11 3:40am    
I am getting error gvdetails of GridView must be place inside the form tag.But My grid View already placed in Form tag with runnat = server

See my HTML CODE

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Export Data from Grid to Word and Excel.aspx.cs" Inherits="Export_Data_from_Grid_to_Word_and_Excel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">

<br />
<br />
<asp:ImageButton ID="btWord" runat="server" Width="98px"
ImageUrl="~/word_icon[1].png" Height="93px" onclick="ImageButton1_Click" />
     
<asp:ImageButton ID="btExcel" runat="server" Width="119px"
ImageUrl="~/excel[1].gif" Height="108px" onclick="ImageButton2_Click" />
<asp:GridView ID="gvdetails" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="qid"
DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#E7E7FF"
BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal"
Width="260px">
<alternatingrowstyle backcolor="#F7F7F7">
<columns>
<asp:BoundField DataField="qid" HeaderText="qid" SortExpression="qid"
ReadOnly="True" />
<asp:BoundField DataField="qname" HeaderText="qname"
SortExpression="qname" />
<asp:BoundField DataField="sid" HeaderText="sid" SortExpression="sid" />
<asp:BoundField DataField="aid" HeaderText="aid" SortExpression="aid" />
<asp:BoundField DataField="cid" HeaderText="cid" SortExpression="cid" />
<asp:BoundField DataField="class" HeaderText="class" SortExpression="class" />

<footerstyle backcolor="#B5C7DE" forecolor="#4A3C8C">
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<rowstyle backcolor="#E7E7FF" forecolor="#4A3C8C">
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<sortedascendingcellstyle backcolor="#F4F4FD">
<sortedascendingheaderstyle backcolor="#5A4C9D">
<sorteddescendingcellstyle backcolor="#D8D8F0">
<sorteddescendingheaderstyle backcolor="#3E3277">

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:tuitionConnectionString %>"
SelectCommand="SELECT * FROM [question]">
<br />

</div>
</form>
</body>
</html>


AND CS CODE IS GIVEN BELOW

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Export_Data_from_Grid_to_Word_and_Excel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
gvdetails.AllowPaging = false;

gvdetails.DataBind();

Response.ClearContent();

Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.doc"));

Response.Charset = "";

Response.ContentType = "application/ms-word";

StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);

gvdetails.RenderControl(htw);

Response.Write(sw.ToString());

Response.End();
Janardan Pandey 31-Dec-11 5:27am    
Unable to understand and do wt u saying

1 solution

Answered to remove from unanswered list - question is duplicated with more information.

Please do not delete - I want the OP to see the difference between his code formatted and unformatted.
 
Share this answer
 
Comments
Janardan Pandey 31-Dec-11 5:27am    
ok

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900