(ASP.NET)用动态属性和DataView实现DataGrid的双向排序

类别:.NET开发 点击:0 评论:0 推荐:

DataGridASP.NET中非常重要的一个控件。它能方便的让我们实现编辑、排序功能;但是排序功能默认的是升序(ASC),能不能让DataGrid同时实现升降序排列呢?这篇文章将给你一个比较好的解决方法。

下面的例子将告诉你如何给DataGrid动态添加sortexpression sortdirection 属性,并通过DataView使DataGird中的数据按照这两个属性排列。在这个例子中使用的DataGridsortexpression属性只需要在sortcommand事件中设置(和通常的排序一样),DataGridsortexpression 属性保存了最后一次用来排序的字段名称,DataGridsortdirection 属性保存了最后一次用来排序的字段排列方式(“ASC”或者“DESC”)

 

页面设计

1.       在页面上添加一个DataGrid

2.       设置DataGridAllowSorting属性为True

3.       设置AutogenerateColumns 属性为False

4.       添加要绑定的字段到DataGrid,并按照下表设置其属性。Sortexpression属性和数据库中数据表中的字段名保持一致。

 

我使用Northwind数据库中的Employees来说明这个例子。

 

DataTextField

Header Text

Sort Expression

EmployeeID

Employee ID

EmployeeID

LastName

Last Name

LastName

FirstName

First Name

FirstName

Title

Title

Title

City

City

City

Country

Country

Country

 

程序代码设计

1.       在页面第一次被加载时给DATAGRID添加动态属性sortexpression sortdirection

2.       给指定的字段的sortexpression sortdirection 赋值,在加载时初始化DataGrid的排序字段和排序方式;

3.       得到dataset dataview 对象。设置dataview sort 属性为动态属性sortexpression sortdirection的连接字符串;

4.       dataview绑定datagrid

5.       当表头被点击时,触发datagridsortcommand事件;

6.       得到sortcommand事件传递过来的sortexpression属性,并与datagridsortexpression属性比较;

7.       如果找到相等的

a)       先检查sortdirection属性;

b)       If sortdirection属性等于“ASCthen

                                    i.        设置sortdirection属性的值等于“DESC

c)        Else

                                    i.        设置sortdirection属性的值等于“ASC

d)       End If

8.       重复第3

 

注意:当你运行下面的代码并点击同一列两次或者两次以上,此列的排列方式会自动根据现有的排列方式的反序排列。

下面的下面的代码将给你演示怎么使用DataGrid的动态属性和DataView对象。

 

C# Code:

 

private void Page_Load(object sender, System.EventArgs e)

{

    // 在此处放置用户代码以初始化页面

    if(!Page.IsPostBack)

    {

        if(DataGrid1.Attributes["SortExpression"] == null)

        {

            DataGrid1.Attributes["SortExpression"] = "LastName";

            DataGrid1.Attributes["SortDirection"] = "ASC";

        }

        BindGrid();

    }

}

 

private void BindGrid()

{

    SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=Northwind");

    conn.Open();

    SqlDataAdapter cmd = new SqlDataAdapter("select * from Employees",conn);

    DataSet ds = new DataSet();

    cmd.Fill(ds,"Employees");

    DataView dv = new DataView();

    dv = ds.Tables[0].DefaultView;

    string SortExpression = DataGrid1.Attributes["SortExpression"];

    string SortDirection = DataGrid1.Attributes["SortDirection"];

    dv.Sort = SortExpression + " " + SortDirection;

    DataGrid1.DataSource = dv;

    DataGrid1.DataBind();

}

 

private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)

{

    string SortExpression = e.SortExpression.ToString();

    string SortDirection = "ASC";

    if(SortExpression == DataGrid1.Attributes["SortExpression"])

    {

        SortDirection = (DataGrid1.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC");

    }

    DataGrid1.Attributes["SortExpression"] = SortExpression;

    DataGrid1.Attributes["SortDirection"] = SortDirection;

    BindGrid();

}

 

 

VB.Net Code:

 

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Put user code to initialize the page here

        If Not Page.IsPostBack Then

            If DataGrid1.Attributes("SortExpression") Is Nothing Then

                DataGrid1.Attributes("SortExpression") = "LastName"

                DataGrid1.Attributes("SortDirection") = "ASC"

            End If

            BindDataGrid()

 

        End If

    End Sub

 

    Private Sub BindDataGrid()

        Dim cn As SqlConnection

        Dim cmdSelect As SqlCommand

 

        Cn = New SqlConnection("Server=amandrek;UID=sa;PWD=;Database=Northwind")

        Dim strSQL As String = "Select * From employees"

 

        Dim da As New SqlDataAdapter(strSQL, cn)

        Dim ds As New DataSet()

 

        da.Fill(ds, "Table1")

 

        Dim dv As DataView = ds.Tables(0).DefaultView

 

        Dim SortExpression As String = DataGrid1.Attributes("SortExpression")

        Dim SortDirection As String = DataGrid1.Attributes("SortDirection")

 

        dv.Sort = SortExpression + " " + SortDirection

 

        DataGrid1.DataSource = dv

        DataGrid1.DataBind()

        cn.Close()

 

    End Sub

 

    Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand

        Dim SortExpression As String = e.SortExpression

        Dim SortDirection As String = "ASC"

 

        If SortExpression.Equals(DataGrid1.Attributes("SortExpression").ToString()) Then

            If DataGrid1.Attributes("SortDirection").ToString().StartsWith("ASC") Then

                SortDirection = "DESC"

            Else

                SortDirection = "ASC"

            End If

        End If

        DataGrid1.Attributes("SortExpression") = SortExpression

        DataGrid1.Attributes("SortDirection") = SortDirection

 

        BindDataGrid()

 

    End Sub

本文地址:http://com.8s8s.com/it/it45253.htm