Solved

List Box Sort: ASP.net, C#

Posted on 2012-03-13
10
457 Views
Last Modified: 2012-03-14
As stated before I have a table that binds to a list box.
The state names in the list box is displayed as below.  I need the state name to sort ASC.  My code is below, can someone look at it and tell me what I am doing wrong.
The state name is not sorting ASC.  Thanks!

StateID      StateName      Abbrv
1      California                        CA
2      Illinois                        IL
3      Louisiana                        LA
4      Alabama                        AL
5      Mississippi        MS
6      Pennsylvania        PA
7      Alabama                        AL

DataTable dtUSStates = (DataTable)Session["Some_States"];
lbStates.DataSource = dtUSStates;
lbStates.DataValueField = "StateID";
lbStates.DataTextField = "StateName";
lbStates.DataBind();
lbStates.Items.Insert(0, li3);

//Sort by state name
DataView dvUSStates = new DataView();
dvUSStates = dtUSStates.DefaultView;
dvUSStates.Sort = "StateName, StateID ASC";
0
Comment
Question by:jordanhelen
  • 5
  • 3
10 Comments
 
LVL 9

Expert Comment

by:lojk
Comment Utility
check out the MSDN page on this one

http://msdn.microsoft.com/en-us/library/13wb36xf(v=VS.80).aspx

Try following its example.. (i.e. create dataview with the required information rather than instantiating it then changing its properties).

DataView dvUSStates = new DataView(dtUSStates,
   "1=1",
   "StateName, StateID ASC",
   DataViewRowState.CurrentRows);

by '1=1' I just mean 'All Rows' as '1=1' is always true..


I would also recommend a

dtUSStates.AcceptChanges()

before the binding just to be on the safe side.
0
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
DataTable dtUSStates = (DataTable)Session["Some_States"];
lbStates.DataValueField = "StateID";
lbStates.DataTextField = "StateName";
lbStates.Items.Insert(0, li3);

//Sort by state name
DataView dvUSStates = new DataView();
dvUSStates = dtUSStates.DefaultView;
dvUSStates.Sort = "StateName, StateID ASC";

lbStates.DataSource = dvUSStates;  // bind to the dataview you want sorted, not the table itself
lbStates.DataBind(); // bind.
0
 
LVL 9

Expert Comment

by:lojk
Comment Utility
yep - ged beat me to it - only noticed AFTER i posted that you were binding to the table not the dataview.
0
 

Author Comment

by:jordanhelen
Comment Utility
Thanks!

This one works!!!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:lojk
Comment Utility
Thanks - in the interests of fair play ged' solution will also work and probably deserves an assist at least as he more accurately identified the typo in your code. I won't be offended if you change it.

I still prefer my way and then bind the lb to the dv as its more explicit and less lines of code in total. Glad you are happy either way. :-)
0
 

Author Comment

by:jordanhelen
Comment Utility
You are right . . . I made a mistake in the selection.

My apologies to ged . . .

  . . . and to you

I have requested attention and asked that the points go to him/her.

The solution works perfect for what I need!
0
 
LVL 9

Expert Comment

by:lojk
Comment Utility
I wont be offended for an assist though either ;-)
0
 

Author Closing Comment

by:jordanhelen
Comment Utility
Thanks!

It works great!
0
 
LVL 9

Expert Comment

by:lojk
Comment Utility
meh.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now