Click here to Skip to main content
15,905,229 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Frnds,

In my project i have a gridview and Print button.I need to print the Data from Gridview. Already i have Displayed the Data in Gridview its Fine. But some Errors.

Please can you advice me. How to print the data perfectly from gridview.

This is my code.

C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.IO;
using System.Text;


protected void BtnCurrentPage_Click(object sender, EventArgs e)
   {

        GridView1.PagerSettings.Visible = false;
        GridView1.DataBind();

        StringWriter sw = new StringWriter();
        
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        
        GridView1.RenderControl(hw);
        string gridHtml = sw.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
        StringBuilder sb = new StringBuilder();
        sb.Append("");
        ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
        GridView1.PagerSettings.Visible = true;
        GridView1.DataBind();
    }


This is the ERROR what am getting after clicking on Printbutton after Execution.
C#
Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.


Please anybody have idea about this. please find me a solution.

Thanks,
Posted
Updated 29-Apr-12 20:49pm
v3
Comments
Prasad_Kulkarni 30-Apr-12 2:49am    
can you post you .aspx code snippets?
Ranjith Reddy CSE 30-Apr-12 2:50am    
ok
Ranjith Reddy CSE 30-Apr-12 2:52am    
This is my DESIGN PAGE
========================

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>:: Grid Printing ::</title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table class="style1">
<tr>
<td style="text-align: center">
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="Id" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" HorizontalAlign="Center"
Width="900px" AllowPaging="True" AllowSorting="True">
<rowstyle backcolor="#EFF3FB">
<columns>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"
ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="Department" HeaderText="Department"
SortExpression="Department" />
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" />
<asp:BoundField DataField="Password" HeaderText="Password"
SortExpression="Password" />
<asp:BoundField DataField="IssueDate" HeaderText="IssueDate"
SortExpression="IssueDate" />
<asp:BoundField DataField="ExpiryDate" HeaderText="ExpiryDate"
SortExpression="ExpiryDate" />
<asp:BoundField DataField="UserType" HeaderText="UserType"
SortExpression="UserType" />
<asp:BoundField DataField="ProgramAccess" HeaderText="ProgramAccess"
SortExpression="ProgramAccess" />

<footerstyle backcolor="#507CD1" font-bold="True" forecolor="White">
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<editrowstyle backcolor="#2461BF">
<alternatingrowstyle backcolor="White">

<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:KKIA_RUHConnectionString %>"
SelectCommand="SELECT * FROM [UsersInformation]">
<asp:Button ID="BtnCurrentPAge" runat="server" BackColor="#0066FF"
ForeColor="White" Text="Print Current Page"
onclick="BtnCurrentPAge_Click" />
 
</td>
</tr>
<tr>
<td>
 </td>
</tr>
</table>
</form>
</body>
</html>

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