如何排序datagridview?

时间:2012-07-23 10:19:32

标签: asp.net vb.net

net with VB.net。我想对列dgv表进行排序,但代码不起作用。

客户端就像这样

 <asp:GridView ID="griddata" runat="server"  AutoGenerateColumns="false" AllowSorting="True" >
                <Columns >
                <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkStatus" runat="server"  />
                            <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# eval("ID") %>' />
                        </ItemTemplate>                   
                </asp:TemplateField>

                <asp:BoundField DataField="First_Name" HeaderText="First Name" SortExpression="First_Name"/>
                <asp:BoundField DataField="Last_Name" HeaderText="Last Name" SortExpression="Last_Name" />
                <asp:BoundField DataField="Address" HeaderText="Address" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" />
            </Columns>        
        </asp:GridView>

像这样的服务器端

Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO

Partial Public Class Course
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        listhendle()

    End Sub

    Protected Sub listhendle()
        Dim ds As New DataSet
        Dim cls As New connections

        ds = cls.returndataset("select * from [m.student] ")

        GridView1.DataSource = ds
        GridView1.DataBind()

    End Sub


End Class

我错过了一些我从here获得引用并且我更改了数据连接

的内容

2 个答案:

答案 0 :(得分:0)

没有Codebehind

你可以做一件事....做数据库连接@codebehind并在aspx文件中复制以下代码并重命名....

它会起作用......

                                                            

答案 1 :(得分:0)

在aspx中:set allowsorting = true

在C#中

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = gvEmployeeList .DataSource as DataTable;
        DataTable dtEmplist = new DataTable();
        dtEmplist = iz.getEmployeeList();
        if (dtEmplist.Rows.Count > 0 )
        {
            DataView dataView = new DataView(dtEmplist);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            gvEmployeeList.DataSource = dataView;
            gvEmployeeList.DataBind();
        }
    }