Pagination and detailed usage of the GridView

Source: Internet
Author: User

Pagination and detailed usage of the gridview

1. Drag and Drop the gridview control to the interface.

2. Use the code to bind the data source to the gridview control.

Oledbcommand command = new oledbcommand ();

String SQL = "select * from admin"; // bind to the data table admin

Command. commandtext = SQL;

Command. connection = shyconnection. conn;

Shyconnection. conn. open (); // open the data connection

Oledbdataadapter adapter = new oledbdataadapter (command );

Dataset ds = new dataset ();

Adapter. fill (ds );

Gridviewx. datasource = ds;

Gridviewx. datakeynames = new string [] {"id"}; primary key array of the control

Gridviewx. databind ();

Shyconnection. conn. close (); // close the data connection

3. Add the following code to the gridview control on the source interface:

<Columns>

<Asp Tutorial: boundfield headertext = "student ID" datafield = "xuehao"/> // bind the xuehao column of the Data Source

<Asp: boundfield headertext = "name" datafield = "name"/> // name column bound to the data source

<Asp: boundfield headertext = "password" datafield = "pasword"/> // pasword column bound to the data source

<Asp: templatefield headertext = "delete">

<Itemtemplate>

<Asp: checkbox id = "checkbox1" runat = "server"/>

</Itemtemplate>

</Asp: templatefield>

<Asp: templatefield headertext = "edit">

<Itemtemplate>

<Asp: linkbutton id = "linkbutton1" runat = "server" causesvalidation = "false" commandname = "zuoza" commandargument = "<% # gridviewx. rows. count %> "text =" edit "> </asp: linkbutton>

</Itemtemplate>

</Asp: templatefield>

</Columns>

4. Check whether the checkbox control of each column in The gridview control is in progress.

Protected void button#click (object sender, eventargs e)

{

For (int I = 0; I <= gridviewx. rows. count-1; I ++)

{

Checkbox cbox = (checkbox) gridviewx. rows [I]. findcontrol ("checkbox1"); // find the checkbox control with the checkbox1 name in each column, and then force the instance to be converted to the object of the checkbox

If (cbox. checked = true)

{

Response. write (int) gridviewx. datakeys [I]. value + "<br>"); // output the primary key value of the current column

}

Else

{

Response. write ("shy" + "</br> ");

}

}

}

5. How to trigger the function gridviewx_rowcommand when clicking EDIT:

Protected void gridviewx_rowcommand (object sender, gridviewcommandeventargs e)

{

If (e. commandname = "edit ")

{

String id = "";

Int index = convert. toint32 (string) e. commandargument );

Id = convert. tostring (gridviewx. datakeys [index]. value. tostring ());

// Now the primary key id of the selected record is obtained

}

}

6. trigger this event function when the control is loaded: gridviewx_rowdatabound

Protected void gridviewx_rowdatabound (object sender, gridviewroweventargs e)

{

This. lblcurrentpage. text = string. format ("Current page {0}/total page {1}", this. gridviewx. pageindex + 1, this. gridviewx. pagecount );

// Set the border style for traversing all rows

Foreach (tablecell tc in e. row. cells)

{

Tc. attributes ["style"] = "border-color: black ";

}

// Obtain the number using the index

If (e. row. rowindex! =-1)

{

Int id = gridviewx. pageindex * gridviewx. pagesize + e. row. rowindex + 1;

// E. row. cells [0]. text = id. tostring ();

}

}

Paging code

Grdview page function.
The procedure is as follows:
1. Change the allowpaging attribute of the grdview control to true.
2. Change the pagesize attribute of the grdview control to any value (10 by default)
3. Change pagesetting-> mode of the grdview control to numeric (default: numeric). This attribute is a paging style.
After the gridview property is set, the page style is displayed on the page.

Now we start to implement the paging function:
1. After <asp: gridview id =...>, add onpageindexchanging = "gridview1_pageindexchanging"
2. Add the following in the corresponding aspx. cs:
Protected void gridview1_pageindexchanging (object sender, gridviewpageeventargs e)
{
Gridview1.pageindex = e. newpageindex;
Initpage (); // rebind the function of the gridview data
}
3,
Gridview1.pageindex = e. newpageindex;

 

Page creation:

1. Drag the gridview (gridviewx) control to the interface and bind the data (bind (). At the same time, perform the following operations on the dropdownlist: this. ddlcurrentpage. items. clear ();

For (int I = 1; I <= this. gridviewx. pagecount; I ++)

{

This. ddlcurrentpage. items. add (I. tostring ());

}

1. this. ddlcurrentpage. selectedindex = this. gridviewx. pageindex;

2. Set the gridview attribute allowpaging = "true" pagesize = "n"

3. Drag and Drop several linkbuttons and set them as follows:

<Asp: linkbutton id = "lnkbtnfrist" runat = "server" onclick = "lnkbtnfrist_click"> homepage </asp: linkbutton>

<Asp: linkbutton id = "lnkbtnpre" runat = "server" onclick = "lnkbtnpre_click"> previous page </asp: linkbutton>

<Asp: linkbutton id = "lnkbtnnext" runat = "server" onclick = "lnkbtnnext_click"> next page </asp: linkbutton>

<Asp: linkbutton id = "lnkbtnlast" runat = "server" onclick = "lnkbtnlast_click"> last page </asp: linkbutton>

4. Set the event handler for several text buttons:

Protected void lnkbtnpre_click (object sender, eventargs e)

{

If (this. gridviewx. pageindex> 0)

{

This. gridviewx. pageindex = this. gridviewx. pageindex-1;

Bind ();

}

}

Protected void lnkbtnnext_click (object sender, eventargs e)

{

If (this. gridviewx. pageindex <this. gridviewx. pagecount)

{

This. gridviewx. pageindex = this. gridviewx. pageindex + 1;

Bind ();

}

}

Protected void lnkbtnlast_click (object sender, eventargs e)

{

This. gridviewx. pageindex = this. gridviewx. pagecount;

Bind ();

}

5. dropdownlist event handler:

Protected void dropdownlistincluselectedindexchanged (object sender, eventargs e)

{

This. gridviewx. pageindex = this. ddlcurrentpage. selectedindex;

Bind ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.