Solved

List Box Sort: ASP.net, C#

Posted on 2012-03-13
10
461 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Author Comment

by:jordanhelen
ID: 37715949
Thanks!

This one works!!!
0
 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

832 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