Gridview 外部手工排序(非点击标题栏方式)

Gridview 本身可以通过点击每一列的标题栏来排序,这种方式网上的文章就太多了,这里不再重复。我们有是否使用gridview的时候是隐藏了标题栏的,这时候我们就会需要通过外部的手工方式来排序。接下来,主要介绍两种方式,一种是gridview绑定的数据源是通过sqldatabase数据源方式,一种是asp.net自带的sql data source控件的方式。

 

1 sql 编程方式的数据源

  

        protected void Button_Click(object sender, EventArgs e)
        {
            string type = "";
            if ("DESC" == (string)ViewState["sort_type"])
            {
                type = "ASC";
            }
            else
            {
                type = "DESC";
            }

            DataTable dt = new DataTable();
            dt = ((DataSet)Session["ds"]).Tables[0];
           

            if (dt != null)
            {
                DataView dv = new DataView(dt);
                dv.Sort = "sort_column_name" + " " + type;
               
                SearchGview.DataSource = dv;
                SearchGview.DataBind();

                WriteDataTableToXml(Server.MapPath(".") + "/tmp_table.xml", DataViewToDataTable(dv));

                DataSet ds = new DataSet();
                ds.ReadXml(Server.MapPath("tmp_table.xml"));
                DataColumn dc = new DataColumn("tmp_column");
                dc.DataType = typeof(int);  ---int is the type of sort_column_name
                ds.Tables[0].Columns.Add(dc);

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ds.Tables[0].Rows[i]["tmp_column"] = ds.Tables[0].Rows[i]["sort_column_name"];
                }
                int ordinal = ds.Tables[0].Columns["sort_column_name"].Ordinal;
                ds.Tables[0].Columns.Remove("sort_column_name");
                ds.Tables[0].Columns["tmp_column"].ColumnName = "sort_column_name";
                ds.Tables[0].Columns["sort_column_name"].SetOrdinal(ordinal);
            }
           

        }

        private DataTable DataViewToDataTable(DataView dv)
        {
            DataTable dtTemp = dv.Table.Clone();
            dtTemp.TableName = "Row";

            foreach (DataRowView drv in dv)
                dtTemp.ImportRow(drv.Row);
            return dtTemp;
        }

        private void WriteDataTableToXml(String fileName, DataTable dt)
        {
            DataSet dsTmp = new DataSet();
            DataTable dtTmp = dt.Copy();
            dsTmp.Tables.Add(dtTmp);

            StreamWriter sr = new StreamWriter(fileName);
            dsTmp.WriteXml(sr);
            sr.Close();
        }

 

 

2  sql data source 控件作为数据源

   

  void SortButton_Click(Object sender, EventArgs e)
  {

    String expression = "";
    SortDirection direction;

    // Create the sort expression from the values selected
    // by the user from the DropDownList controls. Multiple
    // columns can be sorted by creating a sort expression
    // that contains a comma-separated list of field names.
    expression = SortList1.SelectedValue + "," + SortList2.SelectedValue;

    //  Determine the sort direction. The sort direction
    // applies only to the second column sorted.
    switch (DirectionList.SelectedValue)
    {
      case "Ascending":
        direction = SortDirection.Ascending;
        break;
      case "Descending":
        direction = SortDirection.Descending;
        break;
      default:
        direction = SortDirection.Ascending;
        break;
    }

    // Use the Sort method to programmatically sort the GridView
    // control using the sort expression and direction.
    CustomersGridView.Sort(expression, direction);

  }

 

Reference:

MSDN http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sort.aspx

你可能感兴趣的:(Gridview 外部手工排序(非点击标题栏方式))