The DataGrid has both paging and sorting capabilities and attention points

Source: Internet
Author: User
Tags count sort tostring
datagrid| pagination | Sorting when the DataGrid has both paging and sorting capabilities, you should be aware that when you rebind the data source, mydatagrid.currentpageindex=0;
The following to achieve the above function of the original code, it is not a lot of suffix
ASPX contains a DataGrid and DropDownList that control its data source changes
DataGrid Code
<asp:datagrid id= "Mydatagrid" runat= "Server" bordercolor= "#CCCCCC" font-size= "100%" horizontalalign= "Center"
autogeneratecolumns= "False" ondeletecommand= "Mydatagrid_delete" onsortcommand= "Sort_grid" OnPageIndexChanged= " Mydatagrid_pageindexchanged "
Datakeyfield= "Acc_no" pagerstyle-position= "Bottom" pagerstyle-horizontalalign= "Center" pagerstyle-mode= "NextPrev"
Pagesize= "allowsorting=" "true" allowpaging= "true" cellpadding= "4" width= "100%" >
<alternatingitemstyle backcolor= "#E9E9E6" ></AlternatingItemStyle>
<Columns>
<asp:buttoncolumn text= "Kou" commandname= "Delete" ></asp:ButtonColumn>
<asp:boundcolumn datafield= "No" sortexpression= "no" readonly= "True" headertext= "Serial number" ></asp:BoundColumn>
<asp:boundcolumn datafield= "id" sortexpression= "id" headertext= "id" ></asp:BoundColumn>
<asp:boundcolumn datafield= ' name ' sortexpression= ' name ' headertext= ' names ' ></asp:BoundColumn>
<asp:boundcolumn datafield= "C_name" sortexpression= "C_name" headertext= "section name" ></asp:BoundColumn>
<asp:boundcolumn datafield= "FLG" sortexpression= "FLG" headertext= "project" ></asp:BoundColumn>
</Columns>
<pagerstyle nextpagetext= "Next 10" prevpagetext= "Back" horizontalalign= "Center" ></PagerStyle>
</asp:datagrid>


DropDownList code
<asp:dropdownlist id= "DDLWK" runat= "Server" autopostback= "True" enabled= "False" >
<asp:listitem value= "0" > Tokyo </asp:ListItem>
<asp:listitem value= "3" > Kyushu </asp:ListItem>
<asp:listitem value= "8" > Hokkaido </asp:ListItem>
<asp:listitem value= "9" > Four countries </asp:ListItem>
</asp:dropdownlist>



The Aspx.cs file code core is as follows:
private void Page_Load (object sender, System.EventArgs e)
{
if (! IsPostBack)
{
session["WP"] = "0";
Ddlwk_gets ();
Bindgrid ();
}
}
private void Ddlwk_gets ()
{
Switch (session["WP"]. ToString ())
{
Case "0":d dlwk.selectedindex=0;
Break
Case "3":d dlwk.selectedindex=1;
Break
Case "8":d dlwk.selectedindex=2;
Break
Case "9":d dlwk.selectedindex=3;
Break
default:ddlwk.selectedindex=0;
Break
}
}
protected void Bindgrid ()
{
Mydatagrid.datasource=getdata (). tables["VCO"]. DefaultView;
Mydatagrid.databind ();
COUNT. Text=mydatagrid.columns.count.tostring ();
}

<summary>
Return data
</summary>
<returns></returns>
Private DataSet GetData ()
{
String strconn= (String) (NameValueCollection) context.getconfig ("System.web/database")) ["strconn"];
using (SqlConnection conn = new SqlConnection (strconn))
{
SqlCommand cmd = new SqlCommand ("Sp_c", conn);
Cmd.commandtype=commandtype.storedprocedure;
Cmd. Parameters.Add ("@place", sqldbtype.varchar,2);
Cmd. parameters["@place"]. value=session["WP"]. ToString ();
Conn. Open ();

SqlDataAdapter da = new SqlDataAdapter ();
Da. Selectcommand=cmd;
DataSet ds=new DataSet ();
Da. Fill (ds, "VCO");
count.text= "ヒット:" +ds. tables["VCO"]. Rows.Count.ToString () + "pieces";
return DS;
}

}
<summary>
From the dataset, except for a
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Mydatagrid_delete (Object sender, DataGridCommandEventArgs E)
{
String strid=mydatagrid.datakeys[(int) E.item.itemindex]. ToString ();
Delete operation
}
<summary>
Paging operations
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Mydatagrid_pageindexchanged (object source, DataGridPageChangedEventArgs e)
{
Mydatagrid.currentpageindex=e.newpageindex;
Bindgrid ();
}
<summary>
Sort
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Sort_grid (object sender, DataGridSortCommandEventArgs e)
{
DataView dv= New DataView (GetData (). tables["VCO"]);
Dv. Sort= e.sortexpression.tostring ();
MYDATAGRID.DATASOURCE=DV;
Mydatagrid.databind ();
}

#region Web override protected void OnInit (EventArgs e)
{
// //
InitializeComponent ();
Base. OnInit (e);
}

<summary>///</summary>
private void InitializeComponent ()
{
This.ddlWk.SelectedIndexChanged + = new System.EventHandler (this.ddlwk_selectedindexchanged);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

private void Ddlwk_selectedindexchanged (object sender, System.EventArgs e)
{
session["WP"]=ddlwk.selectedvalue;
mydatagrid.currentpageindex=0;//without this, an error occurs when the page number exceeds the range of other data sources
Bindgrid ();
Response.Write ("<script language= ' JavaScript ' >parent.menuframe.location.reload ();</script>");

}





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.