Hi
experts

i m using this code for create new table and add data in this table from old table,
but i need to sort or filter my old table before adding new table,
how i can this?

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Create the table
        Dim Table1 As DataTable
        Table1 = New DataTable
        Table1.Columns.Add("Id", Type.GetType("System.Int32"))
        Table1.Columns.Add("Name", Type.GetType("System.String"))


        Dim Row As DataRow


        Dim i As Integer
        For i = 0 To ds.Tables("bname").Rows.Count - 1
            Row = Table1.NewRow()
            Row("id") = ds.Tables("bname").Rows(i)("bookid")
            Row("name") = ds.Tables("bname").Rows(i)("bname")
            Table1.Rows.Add(Row)
        Next
        TextBox2.Text = Table1.Rows.Count


        If ds.Tables.Contains("table1") = True Then
            ds.Tables.Remove("table1")
        End If

        ds.Tables.Add("table1")

        Dim j, rp, t As Integer
        t = Table1.Rows.Count - 1

        TextBox1.Text = t
        rp = 0


        For j = 0 To t
            lbbname.Items.Add(Table1.Rows(j)("id"))

            rp += 1

        Next

    End Sub

Hi,

Use the "datatable.Select" method to filter/sort the data in datatable. In your code try with,

ds.Tables["table1"].Select("filter expression","sorting method")

for more detail of "select" method have a look at Select -MSDN help
The select method is results the DataRow Array. you can import the array values into new datatable.

Good luck.

You can create a view of your table and then use Find & Filter methods available. This has performance benefits over using datatable select methods which then lose table indexing.

Hi,

You can try this:

Private Function AlphabeticSort(ByVal dtTable As DataTable, ByVal sortOrder As Integer) As DataTable   
        Dim dsSorted As New DataSet
        Dim columnKey As String = "TabName"
        Dim sortDirection As String = ""
        Dim sortFormat As String = "{0} {1}"
        Select Case sortOrder
            Case 0
                sortDirection = "ASC"
            Case 1
                sortDirection = "DESC"
            Case Else
                sortDirection = "ASC"
        End Select
        dtTable.DefaultView.Sort = String.Format(sortFormat, columnKey, sortDirection)
        Return dtTable.DefaultView.Table
    End Function
This article has been dead for over six months. Start a new discussion instead.