When the DataGrid has both paging and sorting functions, please note that when rebinding the data source, MyDataGrid.CurrentPageIndex=0;
The following is the original code to implement the above functions, so there will be no more modifications. aspx contains the DataGrid and the dropdownlist that controls changes in its data source.
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="口" 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="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="item"></asp:BoundColumn>
</Columns>
<PagerStyle NextPageText="Next 10 items" PrevPageText="Return" 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">Jiuzhou</asp:ListItem>
<asp:ListItem Value="8">Hokkaido</asp:ListItem>
<asp:ListItem Value="9">Shikoku</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="ヒット:"+ds.Tables["vCO"].Rows.Count.ToString()+"pieces";
return ds;
}
}
/// <summary>
///Divide 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;//Without this sentence, an error will occur when the page number exceeds the range of other data sources
BindGrid();
Response.Write( "<script language='javascript'>parent.menuframe.location.reload();</script>");
}