Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Alphabetizing a Listbox

I've got a list of periods (Prehistory, Antiquity, Medieval, Post-medieval, Modern) that I'm using to populate a listbox with a dataset.

Unfortunately, they appear in the listbox in alphabetical order instead of chronological order (like they do in the dataset). How can I get it to appear in the order they are in the listbox?

Colm O'Connor
Wednesday, June 15, 2005

Set your listbox Sorted property to false.  Then either add them one by one in the order you want, or have them sorted the way you want in your datasource.  Perhaps the SQL in your DataAdapter/DataReader has an ORDER BY clause that's causing the problem?

Kyralessa
Wednesday, June 15, 2005

I can't seem to find the 'sorted' property:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsListBoxPropertiesTopic.asp

Colm O'Connor
Wednesday, June 15, 2005

The Sorted property only works for WinForms listboxes. The help you provided a link to is for the WebControl listbox. WebControl listboxes show items in the order they where added.

Dan

Dan Boris
Wednesday, June 15, 2005

So how do I databind a correctly sorted dataset to a listbox so it is also correctly sorted?

Colm O'Connor
Wednesday, June 15, 2005

Couldn't you sort the DefaultView on the Table in the DataSet and bind on the DefaultView, rather than the Table or DataSet? Seems like this would work (but I'm still too new to this stuff)

Sgt. Sausage
Wednesday, June 15, 2005

Well, even in WinForms I don't usually use the Sorted property; instead I put the items in a collection and sort the collection the way I want, and make that collection the DataSource for the listbox.

But heck, for all I know, maybe the WebForms listbox doesn't have a DataSource property either...

Kyralessa
Wednesday, June 15, 2005

"So how do I databind a correctly sorted dataset to a listbox so it is also correctly sorted?"

correctly sort the dataset before binding it?

Matt B
Thursday, June 16, 2005

>correctly sort the dataset before binding it?

Already done. Doesn't seem to make any difference.

Colm O'Connor
Thursday, June 16, 2005

Are you sure? I don't think Databind() applies any sorting.

If that still fails, you could always just iterate through the DataSet/Table and add the items one by one.

Matt B
Thursday, June 16, 2005

>Are you sure?

What do you think the first thing I checked was?

>If that still fails, you could always just iterate through the
>DataSet/Table and add the items one by one.

I was kinda hoping that I wouldn't have to kludge it like that.

Colm O'Connor
Thursday, June 16, 2005

>What do you think the first thing I checked was?

I only ask because we have tons of code that binds a DataSet to a ListControl, and none of that data is sorted in the List unless explicitly sorted beforehand...

Matt B
Thursday, June 16, 2005

I tried to tell you about 8 posts ago: use a DataView:

SomeDataSet.Tables[SomeTable].DefaultView.Sort = "col1Name, col2Name";

theListBox.DataSource = SomeDataSet.Tables[SomeTable].DefaultView;

Done.

Two lines of code and you can change the sort to anything you want, with any combination of columns.

Sgt. Sausage
Friday, June 17, 2005

I tried it before and it didn't seem to change anything.

Never mind, it appears to work now for no apparent reason.

Colm O'Connor
Friday, June 17, 2005

==>Never mind, it appears to work now for no apparent reason.

I thought it would.

Funny how stuff works like that ... for no apparent reason.

Sgt. Sausage
Friday, June 17, 2005

Thanks for the help, guys.

Colm O'Connor
Friday, June 17, 2005

If it works now for no apparent reason, then later on it'll stop working for no apparent reason.  If I were you, I'd work on making the reason apparent...

Kyralessa
Friday, June 17, 2005

I think it was a bug in Visual Studio. It sometimes changes stuff around. Either that or I was just being really, really dumb.

Colm O'Connor
Saturday, June 18, 2005

==>I think it was a bug in Visual Studio.

99 times out of 100 when I hear myself thinking this thought, after further investigation, it was just me being stupid or not understanding something.

Not calling you stupid, but just saying you might not want to be so quick to blame the tools and take a real look at what you were doing.

Sgt. Sausage
Sunday, June 19, 2005

*  Recent Topics

*  Fog Creek Home