Results 1 to 5 of 5

Thread: Sorting datatable

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    180

    Sorting datatable

    I have a listbox that must be bound to a datatable. This morning I attempted to write some code that would allow the user to move a listbox item up or down in the list, and to disable the sort buttons if the item is on top or bottom. When I ran the applicaiton, I recieved an error stating that I cannot do this while the listbox is bound to a datasource. Basically I assumed that the listbox works the same as a datagridview in that manipulating the listbox also manipulates the datatable. This is not the case. I must have the listbox bound to a dattable because later I need it to export to xml. So my question is this, I need to change the index of a datatable row. Basically it is a datatable with two columns, an index and a string.

    thx

    Juri


    ps... here is the code I use for sorting.....


    VB Code:
    1. 'Arealist code
    2.  
    3.     Private Sub AreaList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AreaList.SelectedIndexChanged
    4.         ProcessGrid.DataMember = AreaList.GetItemText(AreaList.SelectedItem)
    5.         Dim selectedIndex As Integer = AreaList.SelectedIndex
    6.         ListUp.Enabled = selectedIndex <> -1 AndAlso selectedIndex > 0
    7.         ListDown.Enabled = selectedIndex <> -1 AndAlso selectedIndex < AreaList.Items.Count - 1
    8.     End Sub
    9.  
    10.     Private Sub MoveSelectedItem(ByVal increment As Integer)
    11.         Dim selectedIndex As Integer = AreaList.SelectedItem + increment
    12.         Dim selectedItem As Object = AreaList.SelectedItem
    13.  
    14.         AreaList.Items.Remove(selectedItem)
    15.         AreaList.Items.Insert(selectedIndex, selectedItem)
    16.         AreaList.SelectedIndex = selectedIndex
    17.     End Sub
    18.  
    19.     Private Sub ListDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListDown.Click
    20.         MoveSelectedItem(1)
    21.     End Sub
    22.  
    23.     Private Sub ListUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListUp.Click
    24.         MoveSelectedItem(-1)
    25.     End Sub
    Using Visual Studio 2008

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Sorting datatable

    Add an extra column to your DataTable and populate it with an ascending integer value. Sort the table's DefaultView on that column. When you need to switch two items in the ListBox you swap their values in this column. This will automatically swap their relative positions in the DefaultView, which is where the bound control gets its data from. If the ListBox doesn't update its display automatically just set its DataSource to Nothing and then back to the DataTable again. Note that you will also have to get the SelectedIndex before this and then set it again after.

    Take Note: When you bind a DataTable directly to a control it is still the contents of that table's DefaultView that is displayed in the control. Any sorting or filtering you perform on the DefaultView will be reflected in the control.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    180

    Re: Sorting datatable

    Ok good deal, I tried something similiar by swapping the row's VB index, without using a first column. Swapping in the default view means I have to change the values out correct?

    For instance....

    1 Color
    2 Animal
    3 Bird

    if I swap Bird and color it will read ....

    3 Bird
    2 Animal
    1 Color

    it should be ...

    1 Bird
    2 Animal
    3 Color
    Using Visual Studio 2008

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    180

    Re: Sorting datatable

    ok, I wrote this line of code ....

    VB Code:
    1. DataSet.Tables(0).Rows(AreaList.SelectedIndex).Item(0) = AreaList.SelectedIndex + 1

    This is basically the code to move an item down the list once. It does not work because at runtime it states that there is already something in that position. The index column is set to unique and to auto-increment.
    Last edited by estonianman; Sep 12th, 2006 at 12:56 PM.
    Using Visual Studio 2008

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Sorting datatable

    I didn't suggest using the records' IDs and I didn't suggest making the column require unique values. What I said was add an extra column to your database and populate it with ascending integer values. Nothing about autoincrementing and nothing about unique. You simply set the DefaultView.Sort to sort on that column. When you want to move a row up you just swap its value in that column with the row above it in the DefaultView. When you want to move a row down you simply swap its value in that column with the row below it in the DefaultView. The order of the rows in the DefaultView will update automatically. Simple.
    Last edited by jmcilhinney; Sep 12th, 2006 at 06:17 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width