Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's the easiest way to sort a webforms listbox? 1

Status
Not open for further replies.

JGresko

Programmer
Apr 24, 2002
86
US
Was thread855-398034

I've set up two listboxes in the classic 'select from one, add to another' (pic below). I quickly discovered that ASP listboxes in web forms don't have a sort properity... Does anyone know an easy way to keep the lists sorted as users move items back and forth?

Thanks,
Judy


example2.jpg
 
I don't think there is an easy way such as a .sort command or anything. However, one way to do this would be to do a bubble sort on the data and put the sorted data into the list box.

It's one way to do this. If you find another please post so we know about it. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
bubble sort? How does that work?

I thought if he put it into a SortedList.
 
J

I'm guessing that you need to do a postback to process the move from one box to another, right?

What we did was take the data into a dataview, and call the dataview's sort method. You just tell it which field and ASC or DESC, and it does it from there.

The bubble sort is where you take one item and compare it to teh previous one. If its greater, then it moves it up (bubbles up) until it finds a value that isn't greater than it (or less than it, depending on how you bubble).

D'Arcy
 
Thanks D'Arcy,

I think that's what I need. One more question though, what do you use as the string to sort on for a list box? I tried the code below, but it keeps saying 'Object reference not set to an instance of an object' when it hits the sort (in red below)

Judy


[tt]
Sub listSort(ByRef l As System.Web.UI.WebControls.ListBox,
ByRef s As System.Web.UI.WebControls.ListBox)
Dim lView As DataView
Dim sView As DataView

lView = l.DataSource
sView = s.DataSource

lView.Sort = l.DataTextField
sView.Sort = s.DataTextField


l.DataSource = lView
l.DataBind()

s.DataSource = sView
s.DataBind()
End Sub
[/tt]
 
From what I remember when we did this, the .Sort actually just took a string which was composed of the column to sort by, and either ASC or DESC for the sort order.

so for your code, it would be

lView.Sort = l.DataTextField & " ASC"
sView.Sort = s.DataTextField & " DESC"

Now, I'm not sure if l.DataTextField will return the name of the text field in a string format. if it does, this should work. if not, then you may have to get the name of the field first, and then call the sort function using that name.

let me know how it goes

D
 

This is just not working, as far as I can tell, listboxes don't have a 'name' of their text field.

They have the text property and the value property of the selected item, but that doesn't correlate to a column name to use for the sort. I tried using the values of their DataTextField and DataValueField properties but that didn't work.

There's got to be a simple way to sort an ASP.NET listbox... why did they remove the sort property???????
 
Hey Judy,

This code will take all the entries from a listbox, store them in a datatable, assign them to a dataview, sort them by the "Text" field, and rebind them to a listbox

Dim i As Integer
Dim dv As New DataView()
Dim dt As New DataTable("Temp")
Dim dr As DataRow


dt.Columns.Add("Text")
dt.Columns.Add("Value")

For i = 0 To ListBox1.Items.Count - 1
dr = dt.NewRow
dr.Item(0) = ListBox1.Items(i).Text
dr.Item(1) = ListBox1.Items(i).Value
dt.Rows.Add(dr)
Next

dv = dt.DefaultView
dv.Sort = "Text DESC"
ListBox1.DataSource = dv.Table
ListBox1.DataTextField = "Text"
ListBox1.DataValueField = "Value"
ListBox1.DataBind()

dv = Nothing
dr = Nothing

If you have any questions, just let me know
:)

D'Arcy

p.s. If you want the text field to sort differently, just switch DESC to ASC
 

THANK YOU !!!

... can you tell I'm on a deadline :-D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top