单击列头可以排序的DATAGRID (vb.net实现)

请按照步骤进行:
1: datagrid的属性
AllowSorting=”true”:表示启用DataGrid排序的功能
2: 在HTML里面(加在table外面)加入一个<input id="sortfield" type="hidden" value="activity_student_id" name="sortfield" runat="server">
作为排序时改变字段用
3:datagrid中的字段排序表达式和数字字段一致
就是它的SortExpression属性
4 :加入命名空间  Imports System.Data 
                          Imports System.Data.SqlClient

5:Private Sub dg_activity_info_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dg_activity_info.SortCommand
       If  InStr(sortfield.Value, "desc") = 0 Then
            sortfield.Value = e.SortExpression & " desc"
        Else
            sortfield.Value = e.SortExpression & " asc"
        End If
        BindGrid()

    End Sub
 说明:   《1》e.SortExpression等于“被按下之排序链接”的字段名称。
           《2》 If InStr(sortfield.Value, "desc") = 0如果原来的排序方式原来的排序方式为“递增排序”。这时把排序方式(softfield.value)设置成”Desc”(表示递减排序)。否则设置为””,表示为“递增排序”
           《3》这时softfield.value获得的就是”字段”+”排序”  接着BindGrid().
Sub bindgrid()
        txtSQL = "select * from  ……where……….
        DBSet = ExecuteSQL(txtSQL, ErrorMsg)                '每个程序员获取dataset的方式都不一样,
                     '总之这里你就想办法获取一个dataset数据集 
        Dim dtable As DataTable = DBSet.Tables(0)
        Dim dview As New DataView(dtable)
        dview.Sort = sortfield.Value
        dg.DataSource = dview
        dg.DataBind()
    End Sub


其实排序方法由于很多种,下面的是Doug Seven的一种方法

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>ASPNextGen.com - Column Sorting in the DataGrid</title>
<script runat="server" language="VB">
 Protected _sqlStmt As String = "SELECT CompanyName, ContactName, ContactTitle, Phone, Fax FROM Customers" 

 Sub Page_Load(Source As Object, E As EventArgs)
  If Not Page.IsPostBack Then
   SQLStatement.Text = _sqlStmt
   BindData()
  End If
 End Sub

 Sub BindData()
  Dim myDataSet As New DataSet
  DimConStringAsString="server=localhost;database=Northwind;uid=sa;pwd=;"
  Dim myDataAdapter As New SqlDataAdapter(SQLStatement.Text, ConString)
  myDataAdapter.Fill(myDataSet, "Customers")
  myDataGrid.DataSource = myDataSet.Tables("Customers")
  myDataGrid.DataBind()
 End Sub

 Sub PageIndexChanged_OnClick(Source As Object, E As DataGridPageChangedEventArgs)
  myDataGrid.CurrentPageIndex = E.NewPageIndex
  BindData()                   '点击分页后必须重新绑定
 End Sub

 Sub SortCommand_OnClick(Source As Object, E As DataGridSortCommandEventArgs)
  SQLStatement.Text =_sqlStmt & " ORDER BY " & E.SortExpression
  BindData()
 End Sub
</script>
<style>
 .DataGrid {font:x-small Verdana, Arial, sans-serif}
</style>
</head>
<body>
<form runat="server" method="post">
<asp:Label id="SQLStatement" runat="server" Visible="False" />//利用隐藏的label
<asp:DataGrid runat="server" id="myDataGrid"
  Border="0"
  Cellpadding="4"
  Cellspacing="0"
  AlternatingItemStyle-BackColor="#EFEFEF"
  ShowHeader="True"
  CssClass="DataGrid"
  HeaderStyle-BackColor="Black"
  HeaderStyle-ForeColor="White"
  HeaderStyle-Font-Bold="True"
  AllowSorting="True"
  OnSortCommand="SortCommand_OnClick"
  AllowPaging="True"
  OnPageIndexChanged="PageIndexChanged_OnClick"
  PageSize="10"
  PagerStyle-Mode="NumericPages"
  PagerStyle-HorizontalAlign="Right"
/>
</form>
</body>
</html>

posted on 2005-02-27 17:07  kasafuma  阅读(1424)  评论(0)    收藏  举报