none
Sorting a DataView not giving the expected result RRS feed

  • Question

  • I have a DataTable with columns FruitID (integer) and Fruit (string) which is filled from my database.  I create a DataView as follows:

    Dim dvFruit As New DataView(ds.Tables("Fruits"), Nothing, Nothing, DataViewRowState.CurrentRow)

    which I use as the ItemsSource for a ComboBox.  I want to give the user a chance to select nothing so I add to the DataView as follows:

    Dim NewFruit As DataRowView = dvFruit.AddNew
    NewFruit("FruitID") = 0
    NewFruit("Fruit") = "<None>"
    dvFilter.Sort = "Fruit"

    The sort produces, for example,

    Apple
    Banana
    Pear
    <None>

    But I want the "<None>" to be the first item and it seems to me it should be because the character "<" comes before the alphabetic characters.  Why is my sort not working as expected?





    • Edited by SezMe Tuesday, July 18, 2017 1:21 PM
    Tuesday, July 18, 2017 1:18 PM

Answers

  • The answer to my OP question turns out to be quite simple.  I forgot to end the edit process:

    NewFruit.EndEdit

    Now it works fine.


    • Marked as answer by SezMe Tuesday, July 18, 2017 10:15 PM
    • Edited by SezMe Tuesday, July 18, 2017 10:16 PM
    Tuesday, July 18, 2017 10:15 PM

All replies

  • No matter if it's a DataView (Table is a property of the view) or DataTable why not simply populate the ComboBox with a List.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn With {.ColumnName = "FirstName"})
    
            dt.Rows.Add(New Object() {"Karen"})
            dt.Rows.Add(New Object() {"Ann"})
            dt.Rows.Add(New Object() {"Bill"})
    
            Dim results =
                (
                    From item In dt.AsEnumerable
                    Select item.Field(Of String)("FirstName")) _
                    .OrderBy(Function(item) item
                ).ToList
    
            results.Insert(0, "<None>")
            ComboBox1.DataSource = results
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, July 18, 2017 2:05 PM
    Moderator
  • We see only a part of your code. If you have such a problem than try to make a simple program which does what you think is the problem. 

    If I do that I get the right result. 

    Module Module1
        Sub Main()
            Dim content = {"Banana", "Apple", "Pear", "<None>"}
            Dim dtFruit As New DataTable
            dtFruit.Columns.Add(New DataColumn("Fruits", GetType(System.String)))
            For i = 0 To content.Length - 1
                Dim dr As DataRow = dtFruit.NewRow
                dr("Fruits") = content(i)
                dtFruit.Rows.Add(dr)
            Next
            dtFruit.DefaultView.Sort = "Fruits"
            For i = 0 To dtFruit.DefaultView.Count - 1
                Console.WriteLine(dtFruit.DefaultView(i)(0))
            Next
            Console.ReadLine()
        End Sub
    End Module

    The Defaultview is the inbuild dataview in a datatable.


    Success
    Cor


    Tuesday, July 18, 2017 4:28 PM
  • Because the data come from my database as I indicated.  The Apples, Bananas,  Pears was just an example.  If I added them all using the Add method, that would entail looping through the dataset three times, once to get the data, once to loop though the Add method and once via LINQ.  At the very least it's inelegant.
    Tuesday, July 18, 2017 9:36 PM
  • Because the data come from my database as I indicated.  The Apples, Bananas,  Pears was just an example.  If I added them all using the Add method, that would entail looping through the dataset three times, once to get the data, once to loop though the Add method and once via LINQ.  At the very least it's inelegant.

    I really don't understand what you write. 

    I simply made in the code I made to test your question as if it came from the database and then this does exact what it has to do.

    dtFruit.DefaultView.Sort = "Fruits"
    Are you just joking and don't look at the replies?


    Success
    Cor

    Tuesday, July 18, 2017 9:43 PM
  • Because the data come from my database as I indicated.  The Apples, Bananas,  Pears was just an example.  If I added them all using the Add method, that would entail looping through the dataset three times, once to get the data, once to loop though the Add method and once via LINQ.  At the very least it's inelegant.

    My sample was not implying to use the Add method, it was to show a concept which is to get the items into a list, sort then insert an item.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, July 18, 2017 9:54 PM
    Moderator
  • The answer to my OP question turns out to be quite simple.  I forgot to end the edit process:

    NewFruit.EndEdit

    Now it works fine.


    • Marked as answer by SezMe Tuesday, July 18, 2017 10:15 PM
    • Edited by SezMe Tuesday, July 18, 2017 10:16 PM
    Tuesday, July 18, 2017 10:15 PM