<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function printpage ()
{
VaR newwin = Window. Open ('printer ','','');
VaR titlehtml = Document. getelementbyid ("divshow"). innerhtml;
Newwin.doc ument. Write (titlehtml );
Newwin.doc ument. Location. Reload ();
Newwin. Print ();
Newwin. Close ();
}
</SCRIPT>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: button id = "button1" runat = "server" text = "generate gridview" onclick = "button#click"/> <br/>
<Asp: gridview id = "gdshow" runat = "server" Height = "295px" width = "561px">
</ASP: gridview> <br/>
<% -- <Asp: checkboxlist id = "checkboxlist1" runat = "server" repeatdirection = "horizontal"
Width = "518px">
<Asp: listitem value = "ID card number"> </ASP: listitem>
<Asp: listitem> name </ASP: listitem>
<Asp: listitem> monthly salary </ASP: listitem>
<Asp: listitem> quarterly salary </ASP: listitem>
<Asp: listitem> bonus </ASP: listitem>
<Asp: listitem> Annual Salary </ASP: listitem>
<Asp: listitem> Department </ASP: listitem>
</ASP: checkboxlist> -- %>
<Div id = "divshow" runat = "server">
<% -- Place the dynamically generated gridview here -- %> </div>
<A href = "javascript:;" onclick = "printpage ()"> Print </a>
</Div>
</Form>
</Body>
</Html>
Background code:
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class gridviewdemo: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
/**/
/// <Summary>
/// Bind to generate the gridview
/// </Summary>
/// <Param name = "gdv"> the gridview to bind </param>
/// <Param name = "dtbldatasource"> data source of the gridview </param>
/// <Param name = "strdatakey"> datakeynames of the gridview </param>
Public void gridviewbind ()
{
Dataset dt = getdatatable (); // obtain the data source
Datatable DTS = DT. Tables [0];
Gdshow = new gridview ();
Gdshow. width = unit. pixel (700 );
Gdshow. autogeneratecolumns = false;
// Gvshow. rowediting + = new gridviewediteventhandler (gvshow_rowediting); // Add an editing event
Gdshow. datasource = DT;
Boundfield bf4 = new boundfield ();
Bf4.headertext = "emp_id ";
Bf4.datafield = "emp_id ";
Gdshow. Columns. Add (bf4 );
// Gdshow. Controls. Add (gdshow );
Gdshow. databind ();
// For (INT I = 0; I <DTS. Columns. Count; I ++)
//{
// Boundfield BC = new boundfield ();
// BC. datafield = DTS. Columns [I]. columnname. tostring ();
// BC. headertext = DTS. Columns [I]. Caption. tostring ();
// Gdshow. Columns. Add (BC );
// Gdshow. databind ();
//}
// Commandfield cf = new commandfield ();
/// Cf. buttontype = buttontype. Link;
/// Cf. showeditbutton = true;
/// Cf. causesvalidation = false;
// Gdshow. Columns. Add (CF );
// Gdshow. databind ();
Divshow. Controls. Add (gdshow );
}
Protected void button#click (Object sender, eventargs E)
{
Gridviewbind ();
}
// Protected void gvshow_rowediting (Object sender, gridviewediteventargs E)
//{
// Int I = E. neweditindex;
// String id = gvshow. Rows [I]. cells [0]. Text. tostring ();
// Response. Write ("You want to edit" + id );
//}
Public dataset getdatatable ()
{
String SQL = "select emp_id, bankno, workmode, ipay, imeal, iearning, itaxbase, ideduct, iamount, ideal from tbl_employe_pay ";
Dataset dt = Dal. dbhelpersql. Query (SQL );
Return DT;
}
}
A simple example can also be optimized. I will try again later.