datagrid分页排序选择 例子(C#)

Imports System.Data.SqlClient

Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents dgOrder As System.Web.UI.WebControls.DataGrid
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    End Sub

#End Region

    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


    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 Function GetDv(ByVal strSort As String) As DataView

        '定义数据库连接

        Dim dv As DataView

        Dim CN As New SqlConnection

        Try

            '初始化连接字符串

            CN.ConnectionString = "data source=(local);initial catalog=Northwind;integrated security=SSPI"

            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 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

 

End Class


-------------------------------------------------------------------------------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body>
  <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>
   <P><FONT face="宋体"></FONT>&nbsp;</P>
   <P>
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
  </form>
 </body>
</HTML>
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值