|
-
Sep 11th, 2006, 08:42 AM
#1
Thread Starter
Addicted Member
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:
'Arealist code
Private Sub AreaList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AreaList.SelectedIndexChanged
ProcessGrid.DataMember = AreaList.GetItemText(AreaList.SelectedItem)
Dim selectedIndex As Integer = AreaList.SelectedIndex
ListUp.Enabled = selectedIndex <> -1 AndAlso selectedIndex > 0
ListDown.Enabled = selectedIndex <> -1 AndAlso selectedIndex < AreaList.Items.Count - 1
End Sub
Private Sub MoveSelectedItem(ByVal increment As Integer)
Dim selectedIndex As Integer = AreaList.SelectedItem + increment
Dim selectedItem As Object = AreaList.SelectedItem
AreaList.Items.Remove(selectedItem)
AreaList.Items.Insert(selectedIndex, selectedItem)
AreaList.SelectedIndex = selectedIndex
End Sub
Private Sub ListDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListDown.Click
MoveSelectedItem(1)
End Sub
Private Sub ListUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListUp.Click
MoveSelectedItem(-1)
End Sub
-
Sep 11th, 2006, 08:55 AM
#2
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.
-
Sep 11th, 2006, 09:53 AM
#3
Thread Starter
Addicted Member
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
-
Sep 12th, 2006, 12:52 PM
#4
Thread Starter
Addicted Member
Re: Sorting datatable
ok, I wrote this line of code ....
VB Code:
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
-
Sep 12th, 2006, 06:14 PM
#5
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|