Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

List Box Sort: ASP.net, C#

Posted on 2012-03-13
10
Medium Priority
?
465 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

721 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