logo       

RESOLVED: Sorting a Listbox in C#: msg#00067

Subject: RESOLVED: Sorting a Listbox in C#

OK, after playing with it I figured it out:

When I build my DataView, I ONLY pass it the name of the table (do 
NOT pass it the sort or it will treat it as a string).

The next line of code is:
myDV.ApplyDefaultSort = true;

This sorts the DataView by the first column, taking into account 
what type of field it is.  I think this ONLY works if the field you 
want to sort on is the first field in the underlying table.  If you 
give it this command:
myDV.Sort = "ColumnName";

it treats it as a string, regardless of the real datatype.

I would guess this really does solve the issue, since you would 
really only want 1 or 2 columns underlying a Listbox.  If you want 
any more rows, you would probably end up with a datagrid, which has 
sorting capabilities all its own.

Thanks for all the replies!
KRP

--- In padnug@xxxxxxxxxxxxxxx, "Keith R. Pinster" 
<keith.pinster@xxxx> wrote:
> 
> Thanks for responding Peter.  You are absolutely right in that it 
is 
> sorting Alpha instead of Numeric.  
> 
> However, I'm not sure how putting the numeric field into an array 
is 
> going to solve the problem.  Especially when the array doesn't 
> include the field that I want to display.
> 
> Another issue is that the DataView object doesn't seem to have a 
> Rows collection associated with it.  I know I can pull it directly 
> from the root table, but we still have that darned sorting issue.
> 
> KRP
> 
> --- In padnug@xxxxxxxxxxxxxxx, "Peter Ehi" <pehli@xxxx> wrote:
> > Code Addendum :-)
> > 
> > myArray[count] = Int32.parse(myDv.rows[count]);
> > 
> > ________________________________________
> > From: Peter Ehi [mailto:pehli@xxxx] 
> > Sent: Monday, December 13, 2004 9:31 PM
> > To: padnug@xxxxxxxxxxxxxxx
> > Subject: RE: [padnug] Sorting a Listbox in C#
> > 
> > Kieth,
> > Looks like your trying to sort an auto increment field from the 
> name i.e.
> > keyfield which is probably an int. But you're your code says 
it's 
> a string
> > and so sort is sorting on an ASCI value. Try something along 
these 
> lines. 
> > 
> > Int32[] myArray = new Int32[myDv.Rows.count];
> > 
> > Int32 count = 0;
> > 
> > Foreach(DataRowView myDv in myDv.rows)
> > {
> >       myArray[count] = int.parse("keyField");
> > 
> >       count++;
> > }
> > 
> > I'm sure this has all kinds of syntax errors and I just typed it 
> out real
> > quick, but the above should show you if your DataView sort now 
> works if you
> > step through it.
> > 
> > Peter Ehli
> > 
> > ________________________________________
> > From: Keith R. Pinster [mailto:keith.pinster@xxxx] 
> > Sent: Monday, December 13, 2004 4:38 PM
> > To: padnug@xxxxxxxxxxxxxxx
> > Subject: [padnug] Sorting a Listbox in C#
> > 
> > 
> > OK, I must be doing something really stupid.  I have a listbox 
> that I 
> > am trying to pupulate from a sorted view, but I can't get it to 
> > work.  Here is the code that I'm using:
> > 
> > DataView myDV = new DataView(dsFieldList.Tables[0]);
> > myDV .Sort = "KeyField";
> > Listbox1.DisplayMember = "FieldName1";
> > Listbox1.ValueMember = "KeyField";
> > Listbox1.DataSource = myDV ;
> > 
> > But my Listbox1 seems to be populated in a random order.  What 
the 
> > heck am I doing wrong?
> > 
> > Thanks
> > KRP
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > Yahoo! Groups Sponsor
> > ADVERTISEMENT
> > 
> > 
> > 
> > 
> > ________________________________________
> > Yahoo! Groups Links
> > ? To visit your group on the web, go to:
> > http://groups.yahoo.com/group/padnug/
> >   
> > ? To unsubscribe from this group, send an email to:
> > padnug-unsubscribe@xxxxxxxxxxxxxxx
> >   
> > ? Your use of Yahoo! Groups is subject to the Yahoo! Terms of 
> Service.





------------------------ Yahoo! Groups Sponsor --------------------~--> 
$4.98 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/Q7_YsB/neXJAA/yQLSAA/dpFolB/TM
--------------------------------------------------------------------~-> 

 


<Prev in Thread] Current Thread [Next in Thread>