`
lovnet
  • 浏览: 6704151 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

让Asp.NET的DataGrid可排序、可选择、可分页

阅读更多

让Asp.NET的DataGrid可排序、可选择、可分页
作者:李洪根 出处:CSDN
'***************************************************************

‘Author: 李洪根

‘MAIL: lihonggen0@gci-corp.com

‘专栏: http://www.csdn.net/develop/author/netauthor/lihonggen0/

‘如需引用,请指明出处! CSDN论坛VB版欢迎您!

‘***************************************************************
让Asp.NET的DataGrid可排序、可选择、可分页
DataGrid是Asp.NET中的一个重要的控件,经常我们都将DataGrid做成可分页的和可排序的,有时还需要加上选择功能。这些都是经常需要用到

的方法,其实是比较简单的。

设计思路:
为了方便起见,我们连接SQL Server 2000的NorthWind数据库的Orders表,从数据库里得到此表的数据视图。利用DataGrid的SortCommand事件

实现排序。用一个模板列加上CheckBox控件实现选择。可用DataGrid的属性生成器的“分页”选项或者自己修改HTML实现分页。

HTML:

添加一个DataGrid,命名为dgOrder。
添加了一个模板列,模板列里放一个名为Cb的CheckBox控件。此列用来实现选择
为要排序的每个列加上排序表达式SortExpression。
利用列的DataFormatString来格式化列,象DataFormatString="{0:d}"显示日期格式。
设置PageSize="15"每页显示15行数据,AllowPaging="True" 为允许分页 。


整个HTML页代码:

<form id="Form1" method="post" runat="server">

<asp:datagrid id="dgOrder" runat="server" Height="515px" Width="718px" AutoGenerateColumns="False" AllowSorting="True"

CellPadding="4" BorderWidth="1px" BorderColor="#A0ABEB" PageSize="15" BorderStyle="Solid" BackColor="White"

GridLines="Vertical" ForeColor="Black" AllowPaging="True" ShowFooter="True">

<SelectedItemStyle ForeColor="White" BackColor="Black"></SelectedItemStyle>

<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>

<HeaderStyle HorizontalAlign="Center" ForeColor="White" BorderColor="#6876C5" BackColor="#6876C5"></HeaderStyle>

<FooterStyle ForeColor="White" BackColor="#6876C5"></FooterStyle>

<Columns>

<asp:TemplateColumn>

<ItemTemplate>

<FONT face="宋体">

<asp:CheckBox id="Cb" runat="server"></asp:CheckBox></FONT>

</ItemTemplate>

</asp:TemplateColumn>

<asp:BoundColumn DataField="orderid" SortExpression="orderid" HeaderText="ID">

<HeaderStyle Width="180px"></HeaderStyle>

</asp:BoundColumn>

<asp:BoundColumn DataField="ShipCountry" SortExpression="ShipCountry" HeaderText="ShipCountry">

<HeaderStyle Width="180px"></HeaderStyle>

</asp:BoundColumn>

<asp:BoundColumn DataField="ShippedDate" SortExpression="ShippedDate" HeaderText="ShippedDate" DataFormatString="{0:d}">

<HeaderStyle Width="180px"></HeaderStyle>

</asp:BoundColumn>

<asp:BoundColumn DataField="Freight" SortExpression="Freight" HeaderText="Freight">

<HeaderStyle Width="180px"></HeaderStyle>

</asp:BoundColumn>

<asp:BoundColumn DataField="ShipAddress" SortExpression="ShipAddress" HeaderText="ShipAddress">

<HeaderStyle Width="480px"></HeaderStyle>

</asp:BoundColumn>

</Columns>

<PagerStyle HorizontalAlign="Center" ForeColor="Black" Position="TopAndBottom" BackColor="White"

Mode="NumericPages"></PagerStyle>

</asp:datagrid>

</form>

后台类添加以下代码:

Imports System.Data.SqlClient

'得到数据视图,参数为要排序的列

Private Function GetDv(ByVal strSort As String) As DataView

'定义数据库连接

Dim dv As DataView

Dim CN As New SqlConnection()

Try

'初始化连接字符串

CN.ConnectionString = "data source=pmserver;initial catalog=Northwind;persist security info=False;user id=sa;Password=sa;"

CN.Open()

'从NorthWind得到orders表的数据

Dim adp As SqlDataAdapter = New SqlDataAdapter("select * from orders", CN)

Dim ds As New DataSet()

adp.Fill(ds)

'得到数据视图

dv = ds.Tables(0).DefaultView

Catch ex As Exception

#If DEBUG Then

Session("Error") = ex.ToString()

Response.Redirect("../error.aspx") '跳转程序的公共错误处理页面

#End If

Finally

'关闭连接

CN.Close()

End Try

'排序

dv.Sort = strSort

Return dv

End Function

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

If Not IsPostBack Then

ViewState("strSort") = "orderid"

dgOrder.DataSource = GetDv(ViewState("strSort").ToString())

dgOrder.DataBind()

End If

End Sub

'排序

Private Sub dgOrder_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs)

Handles dgOrder.SortCommand

dgOrder.CurrentPageIndex = 0

'得到排序的列

ViewState("strSort") = e.SortExpression.ToString()

dgOrder.DataSource = GetDv(ViewState("strSort").ToString())

dgOrder.DataBind()

End Sub

'分页

Private Sub dgOrder_PageIndexChanged(ByVal source As Object, ByVal e As

System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgOrder.PageIndexChanged

'得到分页的页号

dgOrder.CurrentPageIndex = e.NewPageIndex

dgOrder.DataSource = GetDv(ViewState("strSort").ToString())

dgOrder.DataBind()

End Sub

运行结果如下图所示:(点击列标头可以排序)


为了知道用户选择的是哪些记录,我们可以利用DataGridItem的FindControl得到CheckBox的值,我们来添加一个按钮,再写如下代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim item As DataGridItem

Dim StrScript As String

StrScript = "<script language=javascript>alert('"

'循环表格的项,FindControl

For Each item In Me.dgOrder.Items

If CType(item.FindControl("cb"), System.Web.UI.WebControls.CheckBox).Checked Then

Try

StrScript += item.Cells(1).Text & Space(2)

Catch ex As Exception

End Try

End If

Next

StrScript += "被选择!')</script>"

RegisterClientScriptBlock("系统消息", StrScript)

End Sub

上面的代码RegisterClientScriptBlock添加Java Script脚本弹出对话框。(其实Vb Script的对话框比Java Script的对话框多更多的显示和

控制方式,但Netscape的浏览器不支持,大家可根据相应的项目在程序里选择用哪种脚本)。


总结:

DataGrid是我们常用的Web 控件,有时我们还可以和DataList混合使用,通过修改HTML页,可以达到好的页面效果。上面只是一个例子,为了

便于清楚整个过程,我把数据访问部分(SQL)写到了页面中。在软件开发中,我们一般把访问数据的部分写成数据层,页面调用数据层得到数据

,这样逻辑清晰,修改和维护都很方便。

分享到:
评论

相关推荐

    ASP.NET常见问题集锦.zip

    ASP.NET中实现DataGrid数据排序.doc asp.net中的弹出对话框.doc ASP.NET中自定义控件的创建和使用.doc ASP.NET实现用户在线检测的类源码.txt ASP.NET常见问题集锦.zip ASP.NET弹出式日历选择控件的实现.doc ...

    asp.net知识库

    ASP.NET中大结果集的分页[翻译] .net 2.0 访问Oracle --与Sql Server的差异,注意事项,常见异常 Ado.net 与NHibernate的关系? 动态创建数据库 SQL Server数据库安全规划全攻略 .net通用数据库访问组件SQL ...

    asp.net专家疑难解答200问

    如何在ASP.NET中做一个日期选择器 198.如何在弹出对话框的同时保持页面的显示 199.如何点击按钮弹出新页面,输入数据后返回并且不刷新页面 第11章 文件操作 200.如何读取、修改文本文件 201....

    asp.net技术内幕(1)

    11.3.2 对DataGrid使用样式 11.3.3 对DataGrid控件中的列进行排序 11.3.4 对DataGrid中的记录进行分页 11.3.5 选择DataGrid控件中的行 11.3.6 编辑DataGrid控件中的条目 11.3.7 使用模板...

    ASP.net技术内幕

    11.3.1 在DataGrid控件中创建列 11.3.2 对DataGrid使用样式 11.3.3 对DataGrid控件中的列进行排序 11.3.4 对DataGrid中的记录进行分页 11.3.5 选择DataGrid控件中的行 11.3.6 编辑...

    asp.net专家疑难解答200问源码

    197.如何在ASP.NET中做一个日期选择器 198.如何在弹出对话框的同时保持页面的显示 199.如何点击按钮弹出新页面,输入数据后返回并且不刷新页面 第11章 文件操作 200.如何读取、修改文本文件 201.如何在ASP.NET...

    asp.net技术内幕(5)

    11.3.2 对DataGrid使用样式 11.3.3 对DataGrid控件中的列进行排序 11.3.4 对DataGrid中的记录进行分页 11.3.5 选择DataGrid控件中的行 11.3.6 编辑DataGrid控件中的条目 11.3.7 使用模板...

    asp.net技术内幕(4)

    11.3.2 对DataGrid使用样式 11.3.3 对DataGrid控件中的列进行排序 11.3.4 对DataGrid中的记录进行分页 11.3.5 选择DataGrid控件中的行 11.3.6 编辑DataGrid控件中的条目 11.3.7 使用模板...

    asp.net技术内幕(3)

    11.3.2 对DataGrid使用样式 11.3.3 对DataGrid控件中的列进行排序 11.3.4 对DataGrid中的记录进行分页 11.3.5 选择DataGrid控件中的行 11.3.6 编辑DataGrid控件中的条目 11.3.7 使用模板...

    asp.net技术内幕(2)

    11.3.2 对DataGrid使用样式 11.3.3 对DataGrid控件中的列进行排序 11.3.4 对DataGrid中的记录进行分页 11.3.5 选择DataGrid控件中的行 11.3.6 编辑DataGrid控件中的条目 11.3.7 使用模板...

Global site tag (gtag.js) - Google Analytics