|
當(dāng)DataGrid同時(shí)具有分頁(yè)和排序功能時(shí)應(yīng)注意在重新綁定數(shù)據(jù)源時(shí),MyDataGrid.CurrentPageIndex=0;
下面給實(shí)現(xiàn)以上功能的原碼,也就不多綴了ASPx中包含有DataGrid和控制其數(shù)據(jù)源變化的dropdownlist
DataGrid代碼
<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="序號(hào)"></ASP:BoundColumn>
<ASP:BoundColumn DataField="ID" SortExpression="ID" HeaderText="ID"></ASP:BoundColumn>
<ASP:BoundColumn DataField="NAME" SortExpression="NAME" HeaderText="名稱"></ASP:BoundColumn>
<ASP:BoundColumn DataField="C_NAME" SortExpression="C_NAME" HeaderText="各科名稱"></ASP:BoundColumn>
<ASP:BoundColumn DataField="FLG" SortExpression="FLG" HeaderText="項(xiàng)目"></ASP:BoundColumn>
</Columns>
<PagerStyle NextPageText="下10件" PrevPageText="返回" HorizontalAlign="Center"></PagerStyle>
</ASP:datagrid>
dropdownlist代碼
<ASP:dropdownlist id="ddlWk" Runat="server" AutoPostBack="True" Enabled="False">
<ASP:ListItem Value="0">東京</ASP:ListItem>
<ASP:ListItem Value="3">九州</ASP:ListItem>
<ASP:ListItem Value="8">北海道</ASP:ListItem>
<ASP:ListItem Value="9">四國(guó)</ASP:ListItem>
</ASP:dropdownlist>
ASPx.cs文件代碼核心如下:
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>
/// 返回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()+"件";
return ds;
}
}
/// <summary>
///從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();
//刪除操作
}
/// <summary>
/// 分頁(yè)操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void MyDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
MyDataGrid.CurrentPageIndex=e.NewPageIndex;
BindGrid();
}
/// <summary>
/// 排序
/// </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;//沒(méi)有這一句,當(dāng)該頁(yè)碼超出其他數(shù)據(jù)源的范圍時(shí)會(huì)出錯(cuò)
BindGrid();
Response.Write( "<script language='Javascript'>parent.menuframe.location.reload();</script>");
}
AspNet技術(shù):DataGrid同時(shí)具有分頁(yè)和排序功能及注意點(diǎn),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。