ASP.NET-DataGrid控件的排序功能

| |
[2005/12/21 10:22 | 分类: DotNet技术 | by gdgzboy ]
[此文来源于互联网,牛C网只负责收集整理]
  上一节我们已经知道DataGrid排序功能是由AllowSorting属性控制的,这一小节里,我们将通过实例来验证这个功能。

    在DataCon Web项目里,添加一个窗体,命名为DataGrid_Sample4.aspx,添加一个DataGrid控件,DataGrid_Sample4.aspx的主要HTML代码如下:
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
   <FONT face="宋体"></FONT>&nbsp;
   <asp:DataGrid id="DataGrid1"
            runat="server" PageSize =6
AllowPaging="True" AllowSorting="True" BorderColor="#009900" BorderWidth="1px" CellPadding="0">
    <AlternatingItemStyle
               Font-Size="X-Small" BackColor="WhiteSmoke"> </AlternatingItemStyle>
<ItemStyle Font-Size="X-Small" BackColor="#FFFFFF"> </ItemStyle>
   <HeaderStyle Font-Size="X-Small" Font-Bold="True" BackColor="LightSteelBlue"></HeaderStyle>
   <FooterStyle Wrap="False" BackColor="LightGray"></FooterStyle>
   <PagerStyle Font-Size="X-Small" Font-Bold="True" Position="TopAndBottom" BackColor="Linen" Mode="NumericPages"></PagerStyle>
  </asp:DataGrid>
</form>
</body>
DataGrid_Sample4.aspx.vb中的代码如下:

'----code begin----
'引入名称空间
Imports System
Imports System.Data
Imports System.Web.UI
Public Class DataGrid_Sample4_aspx
    Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
    '此处省略了窗体设计器生成的代码,以节约篇幅
#End Region
  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '使用viewstate("sort")来保存DataGrid排列数据的依据,初始化为id字段
        viewstate("sort") = "id"
        '调用数据绑定过程
        getdata()
End Sub
    '读取数据信息过程
    Sub getdata()
        Dim mycon As OleDb.OleDbConnection
        Try
            mycon = New OleDb.OleDbConnection("provider=microsoft.jet.oledb.4.0;data source="   Server.MapPath(".")   "\StudentInfor.mdb")
            Dim mycmd As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("select * from student", mycon)
            Dim dt As Data.DataSet = New Data.DataSet
            mycmd.Fill(dt)
            Dim dv As Data.DataView = New Data.DataView
            '声明DataView类,并实例化
            dv = dt.Tables(0).DefaultView
            dv.Sort = viewstate("sort")
            '指定DataView类的Sort值,这个值由viewstate("sort")传入
            DataGrid1.DataSource = dv
            '指定DataGrid1的数据源为DataView
            DataGrid1.DataBind()
        Catch ex As Exception
            Response.Write("程序出现错误,信息描述如下:<br>" & ex.Message.ToString)
        Finally
            mycon.Close()
        End Try
End Sub
    '翻页事件过程
Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        getdata()
End Sub
    '请求排列顺序事件过程
Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
        viewstate("sort") = e.SortExpression.ToString
        getdata()
End Sub
End Class
'-----code end ----------
保存编译后,运行效果如图9.9所示。


图9.9  DataGrid_Sample4.aspx运行结果

        

            


作者:gdgzboy@牛C网
地址:http://www.niuc.net/post/6528/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!



评论(0) | 引用(0) | 阅读(40)
发表评论
昵称 [注册]
密码 游客无需密码
网址
电邮
打开HTML 打开UBB 打开表情 隐藏 记住我