Solved

List Box Sort: ASP.net, C#

Posted on 2012-03-13
10
460 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
ID: 37714820
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 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 37714822
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
ID: 37714848
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
ID: 37715949
Thanks!

This one works!!!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:lojk
ID: 37716121
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
ID: 37716321
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
ID: 37716362
I wont be offended for an assist though either ;-)
0
 

Author Closing Comment

by:jordanhelen
ID: 37719293
Thanks!

It works great!
0
 
LVL 9

Expert Comment

by:lojk
ID: 37719351
meh.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 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

16 Experts available now in Live!

Get 1:1 Help Now