日期:2009-07-21  浏览次数:20455 次

当DataGrid同时具有分页和排序功能时应注意在重新绑定数据源时,MyDataGrid.CurrentPageIndex=0;
下面给实现以上功能的原码,也就不多缀了ASPx中包含有DataGrid和控制其数据源变化的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="序号"></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="项目"></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">四国</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"].ToStri