The DataGrid provides both paging and sorting functions and notes.

Source: Internet
Author: User

When the DataGrid supports both paging and sorting, note that when the data source is rebound, MyDataGrid. CurrentPageIndex = 0;
The following is the source code for implementing the above functions, so we will not have to fix the dropdownlist in aspx that contains the DataGrid and controls 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 = "10" AllowSorting = "True" AllowPaging = "True" CellPadding = "4" Width = "100%">
<AlternatingItemStyle BackColor = "# E9E9E6"> </AlternatingItemStyle>
<HeaderStyle Font-Bold = "True" Wrap = "False" ForeColor = "White" BackColor = "#999999"> </HeaderStyle>
<Columns>
<Asp: ButtonColumn Text = "Port" CommandName = "Delete"> </asp: ButtonColumn>
<Asp: BoundColumn DataField = "NO" SortExpression = "NO" ReadOnly = "True" HeaderText = "Sn"> </asp: BoundColumn>
<Asp: BoundColumn DataField = "ID" SortExpression = "ID" HeaderText = "ID"> </asp: BoundColumn>
<Asp: BoundColumn DataField = "NAME" SortExpression = "NAME" HeaderText = "NAME"> </asp: BoundColumn>
<Asp: BoundColumn DataField = "C_NAME" SortExpression = "C_NAME" HeaderText = "name of each subject"> </asp: BoundColumn>
<Asp: BoundColumn DataField = "FLG" SortExpression = "FLG" HeaderText = "project"> </asp: BoundColumn>
</Columns>
<PagerStyle NextPageText = "10" PrevPageText = "back to" 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 core code of the aspx. cs file 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": ddlWk. SelectedIndex = 0;
Break;
Case "3": ddlWk. SelectedIndex = 1;
Break;
Case "8": ddlWk. SelectedIndex = 2;
Break;
Case "9": ddlWk. 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 = "partition failed:" + ds. Tables ["vCO"]. Rows. Count. ToString () + "pieces ";
Return ds;
}

}
/// <Summary>
/// Remove one from DataSet
/// </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 operation
/// </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; // if this sentence does not exist, 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> ");

}

Related Article

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.