开发者

How can I sort a Table into ascending or descending? VB.NET

I am currently building a table by retrieving a number and a name from an XML document and dynamically building a table. Currently, the table is built and you can check a box to choose whether or not you want to randomize the numbers that correspond to the name. I need to be able to check another box to determine whether or not you want it ascending or descending order. I don't know how to do sorts on a Table Object. Do I have to read it into an Array first and then re-read them into the table?

XML Document Example:

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

Code:

Markup:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="itc320_presentation_order._Default" %>

<!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 = "scree开发者_C百科n" />
</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>

Code Behind :

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


You're going to want to sort the DataTable before your loop.

http://www.akadia.com/services/dotnet_filter_sort.html#Filtering%20and%20Sorting%20with%20the%20DataTable%20Select%20Method


Since you are programmatic generating the table it can't be sorted as such. You can easily sort your data table and then regenerate your table. Another option would be to use one of the available data controls DataGridview for example which handle the job of generation for you and in some cases allow you to add sorting in the designer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜