Winforms - want Combobox(dropdown) items in sorted order

Posted by Madhavi under C# on 2/18/2010 | Views : 13532 | Status : [Member] | Replies : 9
Hi

I had one comboBox which displays all country names. Data is coming but not in sorted order. I used Hashtable to which data comes thorugh query

public void GetCountryNames(ref Hashtable ht1, ref Hashtable ht2)
{
OracleCommand vCountrynames;

vCountrynames = new OracleCommand(sqlcommandfile.GetCountrynames, oracleCon);

try
{
oracleCon.Open();

OracleDataReader Reader = vCountrynames.ExecuteReader(CommandBehavior.SingleRow);

while (Reader.Read())
{
ht1.Add(Reader.GetString(0), Reader.GetString(1));
ht2.Add(Reader.GetString(1), Reader.GetString(0));
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
oracleCon.Close();
}
}



"Sqlcommandfile.GetCountrynames" will fetch data through Query. Given below is the query.


SELECT ITEM_NAME, ITEM_KEY FROM COUNTRY_CODE WHERE ITEM_ID='1220' and FLG= 1 ORDER BY ITEM_KEY


while debugging i found , after reader operation, ht1.Add() is there, here while adding to Hash table not adding in sorted order


Can any one help how to get sorted list

Madhavi




Responses

Posted by: Vuyiswamb on: 2/18/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
I suggest you use a List that has a sort Function

List<string> List1 = new List<string>(ht1.Values);

List1 .Sort();



List<string> List2 = new List<string>(ht2.Values);

List2.Sort();



Thank you for Posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Madhavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bubbly on: 2/18/2010 [Member] Bronze

Up
0
Down
Yes its better to use list which already has sort.

++
Thanks & Regards,
Deepika

Madhavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 2/18/2010 [Member] Bronze

Up
0
Down
You can use list of generics with lambda expressions combinations for better sorting. (if you use c#2.0 or later)

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Madhavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/18/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
I would Rather go for SortedList<T> which always sorts itself when you insert data into the list.

www.abhisheksur.com

Madhavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhavi on: 2/18/2010 [Member] Starter

Up
0
Down
Hi,

How to use lambda expressions . Can u tell me with example?


Madhavi

Madhavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhavi on: 2/18/2010 [Member] Starter

Up
0
Down
HI,

getting error

he best overloaded method match for 'System.Collections.Generic.List<string>.List(int)' has some invalid arguments D:\GRT\


rgument '1': cannot convert from 'System.Collections.ICollection' to 'int'


Need Example for SortedList

Madhavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhavi on: 2/23/2010 [Member] Starter

Up
0
Down
Hi

I was stuck up with some error while using "List", Alternatively as a solution what i did is -

Made Combobox "Sorted" property set to "True", now all the coutnry names are coming in alphabetical order. But i want "Other" to appear last in the list . I added "other" because if user come across any country names other than in the list then user chooses "Other".


Is there any simple smart way to make it appear at last in the list. Plzzz help me in this. My requirement will be fulfilled if i can make other to appear as last item.


Madhavi

Madhavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/23/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Just after bind,Remove the element Other, and add using
drpDown.Items.Insert(location, item);




www.abhisheksur.com

Madhavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 2/23/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Sort First and Add the Other at the end after Sorting

Thank you for posting at Dotnetfunda
[Administrator]

Madhavi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response