如何将表排序为升序或降序? VB.NET

时间:2011-03-03 06:28:31

标签: vb.net sorting datatable

我目前正在通过从XML文档中检索数字和名称并动态构建表来构建表。目前,该表已构建,您可以选中一个框以选择是否要随机化与该名称对应的数字。我需要能够检查另一个框以确定您是否要升序或降序。我不知道如何对表对象进行排序。我是否必须首先将其读入数组,然后将其重新读入表中?

XML文档示例:

- <roster>
- <student>
  <order>01</order> 
  <name>Sabrina Spark</name> 
  </student>
- <student>
  <order>02</order> 
  <name>Ryan Weble</name> 
  </student>
- <student>
  <order>03</order> 
  <name>Fredric Scott</name> 
  </student>
- <student>
  <order>04</order> 
  <name>Cory Snyder</name> 
  </student>

代码:
标记:

&lt;%@ Page Language =“vb”AutoEventWireup =“false”CodeBehind =“Default.aspx.vb”Inherits =“itc320_presentation_order._Default”%&gt;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <link href="StyleSheet1.css" rel="Stylesheet" type = "text/css" media = "screen" />
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:CheckBox ID="Descending" runat="server" Text="Descending" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="ExistingOrder" runat="server" Text="Existing Order" />

        <br />
        <asp:Xml ID="Xml1" runat="server"></asp:Xml>
        <br />
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" 
            DataFile="~/App_Data/roster.xml" TransformFile="~/App_Data/roster.xsl">
        </asp:XmlDataSource>
        <br />
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <br />
        <br />
        <asp:Button ID="cmdRandomize" runat="server" Height="36px" Text="Generate Table" 
            Width="127px" />
        <br />

    </div>
    </form>
</body>
</html>

代码背后:

Imports System.Xml
Imports System.IO
Partial Public Class _Default
    Inherits System.Web.UI.Page
    Dim rand As New Random()
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

        End If
    End Sub
    Protected Sub cmdRandomize_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdRandomize.Click

        Dim intArl(15) As Integer

        Dim ds As DataSet
        Dim dr As DataRow

        Dim tbl As New Table

        Dim strPath As String

        'Placehold control will hold the dynamically created table
        PlaceHolder1.Controls.Clear()

        'build path to data file
        strPath = Server.MapPath("/App_Data") & "\roster.xml"


        ds = New DataSet()

        'load dataset object with data from xml source
        ds.ReadXml(strPath)

        Dim dtRoster As DataTable = ds.Tables(0)

        'Generate table of random order ints 
        If ExistingOrder.Checked = False Then
            genUnique(intArl, dtRoster.Rows.Count - 1)
        Else
            intArl = ViewState("intArl")
        End If


        Dim intSeq As Integer = 0
        For Each dr In dtRoster.Rows
            Dim tr As New TableRow
            For intColCnt = 0 To 1
                Dim tc As New TableCell()
                Dim txtBox As New TextBox()
                If intColCnt = 0 Then
                    txtBox.Text = intSeq
                Else
                    txtBox.Text = dr(intColCnt)
                End If
                'Add the control to the TableCell
                tc.Controls.Add(txtBox)
                'Add the TableCell to the TableRow
                tr.Cells.Add(tc)
            Next
            If intSeq Mod 2 Then
                tr.CssClass = "odd"
            Else
                tr.CssClass = "even"
            End If



            tbl.Rows.Add(tr)
            intSeq += 1

        Next
        PlaceHolder1.Controls.Add(tbl)


        ViewState("intArl") = intArl
    End Sub
    Private Sub genUnique(ByRef intArl() As Integer, ByVal intCnt As Integer)
        Dim intGen As Integer
        Dim intClassCnt As Integer
        For intClassCnt = 0 To intCnt
regen:
            'intGen = Int(Rnd() * 20) + 1
            intGen = rand.Next(1, intCnt + 2)
            If Not alreadyExists(intArl, intGen, intClassCnt) Then
                intArl(intClassCnt) = intGen
                Else : GoTo regen
                End If
        Next
    End Sub
    Private Function alreadyExists(ByRef intArl() As Integer, ByVal num As Integer, ByVal curr As Integer) As Boolean
        Dim intCnt As Integer

        'Scan the entries in the array for the existance of the newly
        'generated number.  Return True if found to already exist in the array

        For intCnt = 0 To curr - 1
            If num = intArl(intCnt) Then
                Return True
            End If
        Next
        Return False
    End Function

End Class

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

由于您是以编程方式生成表,因此无法对其进行排序。您可以轻松sort your data table然后重新生成表格。另一种选择是使用一个可用的数据控件DataGridview,例如为您处理生成的工作,在某些情况下允许您在设计器中添加排序。