CSDN博客

img dcboy

DataGrid排序

发表于2004/6/30 21:58:00  748人阅读

 private void BindGrid()
  {
   Teacher.BLL.Admin.ClassBLL bll=new Teacher.BLL.Admin.ClassBLL();
   DataView dv=bll.GetTbCLassDataSet().Tables[0].DefaultView;
   if(ViewState["strSort"]!=null)
   {
    dv.Sort=ViewState["strSort"].ToString();
   }
   if(ViewState["strRowFilter"]!=null)
   {
    dv.RowFilter=ViewState["strRowFilter"].ToString();
   }
   DataGrid1.DataSource=dv;
   DataGrid1.DataBind();
  }

 

private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
  {
   string strSort="";
   foreach(DataGridColumn oCol in DataGrid1.Columns)
   {
    if(e.SortExpression.ToLower().CompareTo(oCol.SortExpression.ToLower()) == 0)
    {
     oCol.HeaderText = oCol.HeaderText.Replace(" (↑)","").Replace(" (↓)","");
     if(e.SortExpression.IndexOf(" ASC") > 1)
     {
      oCol.SortExpression = e.SortExpression.Replace(" ASC"," DESC");
      oCol.HeaderText  = oCol.HeaderText + " (↓)";
      strSort= oCol.SortExpression;
     }
     else if(e.SortExpression.IndexOf(" DESC") > 1)
     {
      oCol.SortExpression = e.SortExpression.Replace(" DESC","");
      strSort= e.SortExpression;
     }
     else
     {
      oCol.SortExpression = e.SortExpression + " ASC";
      oCol.HeaderText  = oCol.HeaderText + " (↑)";
      strSort= oCol.SortExpression;
     }
    }
   }

//这个是排序要保存的排序字段
   ViewState["strSort"]=strSort;

//绑定的datagrid
   BindGrid();
  }

0 0

相关博文

我的热门文章

img
取 消
img