Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim table As DataTable = New DataTable("Players")
        ' Add 2 columns.
        table.Columns.Add(New DataColumn("Size", GetType(Integer)))
        table.Columns.Add(New DataColumn("Team", GetType(Char)))
        ' Add 5 rows.
        table.Rows.Add(100, "a"c)
        table.Rows.Add(235, "a"c)
        table.Rows.Add(250, "b"c)
        table.Rows.Add(310, "b"c)
        table.Rows.Add(150, "b"c)

        

         '' table.DefaultView.Sort = "Size ASC"
        Dim size1 As String = table.Rows(4).Item("Size")
        table.DefaultView.ToTable()
        MsgBox(size1)
''' am getting 150 insted of 310

    End Sub
End Class


What I have tried:

''' am getting 150 instead of 310
after sorting i must get 310 as 4th number if this begins from 0
Posted
Updated 26-Apr-24 4:17am
v3
Comments
Richard MacCutchan 26-Apr-24 8:56am    
Your table has not been sorted, as the sort command has been commented out.
Member 10974007 26-Apr-24 9:50am    
even if your remove the comment it wont work...
PIEBALDconsult 26-Apr-24 10:19am    
Use DefaultView in:
Dim size1 As String = table.Rows(4).Item("Size")

It is the view that gets sorted not the source table. So you need to capture the DataView object and display the row from there. See DataView.Sort Property (System.Data) | Microsoft Learn[^] for an example.
 
Share this answer
 
v2
You are accessing the row at index 4, which is the 5th row in the DataTable which is why you get the vale of '150. Datatable indexes start from 0 -
VB
Dim size1 As String = table.Rows(3).Item("Size")


If you want to sort the DataTable by the 'Size' column in ascending order before accessing the rows -
VB
table.DefaultView.Sort = "Size ASC"


Your code should then look like -
VB.NET
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim table As DataTable = New DataTable("Players")

        'Add 2 columns...
        table.Columns.Add(New DataColumn("Size", GetType(Integer)))
        table.Columns.Add(New DataColumn("Team", GetType(Char)))

        'Add 5 rows...
        table.Rows.Add(100, "a"c)
        table.Rows.Add(235, "a"c)
        table.Rows.Add(250, "b"c)
        table.Rows.Add(310, "b"c)
        table.Rows.Add(150, "b"c)

        'Sort your DataTable by the 'size' column in ascending order...
        table.DefaultView.Sort = "Size ASC"

        'Get the value from the 4th row (index 3)...
        Dim size1 As String = table.Rows(3).Item("Size")

        'Convert the DataView back to a DataTable...
        table = table.DefaultView.ToTable()

        MsgBox(size1) 'Output should now be 310...
    End Sub
End Class
 
Share this answer
 
Comments
Member 10974007 26-Apr-24 11:02am    
Dim size1 As String = table.Rows(4).Item("Size")
as a last item and you will get 150 again..
Andre Oosthuizen 26-Apr-24 13:36pm    
I am not sure what your comment means...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900