Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI ALL,

I am developing a C# windows application in which i have a combobox which displays currency name in ascending order.I want to set the default currency as USD but cant change the order.Please help me..
Posted

C#
combobox1.SelectedText = "USD"; // use whatever string is applicable


Keep in mind that using text to set a combobox is ill-advised. You're better off setting the SelectedIndex or the SelectedItem.
 
Share this answer
 
Comments
fjdiewornncalwe 30-Nov-11 9:38am    
Of course.
BillWoodruff 1-Dec-11 21:25pm    
SelectedItem can be set using a string argument (see my answer below).
A better approach is to fetch Id from your datasource and then assign it to dropdown selected value .

If you have Datasource type Table then you can do something like this.


combobox1.SelectedValue= table.Select("tt='USD'")[0]["Id"];


Assuming "USD" would always be in datasource. If not this code would throw exception. Then in that case you may apply

DataRow[] rows = table.Select("tt='USD'");

if(rows.Length>0)
 combobox1.SelectedValue=  rows[0]["Id"];



Hope this would be helpful.
 
Share this answer
 
v2
Comments
RaviRanjanKr 1-Dec-11 14:33pm    
Always wrap your code in pre tag. :)
comboBox1.SelectedItem = "USD";
Also works.

fyi: 'SelectedItem' is a native property of the ComboBox control: 'SelectedValue' is an inherited property from ListControl. In practice this makes little difference except perhaps in scnearios where you are using databinding. See:[^]

But, note that SelectedItem works by: "The ComboBox class searches for the specified object by using the IndexOf method. This method uses the Equals method to determine equality." ... from the .C# docs.

If you are setting/re-setting the default Currency frequently in your application, or perhaps the list of currencies changes dynamically, probably better to calculate, when you need to, the default currency's index in the ComboBox List, stick that in an integer variable, and then use that variable as the argument to SelectedIndex.

But, if this is a one off, done at application initialization, and the default currency list is not changed, or does not need to be 'reset,' it probably doesn't matter which technique you use.
 
Share this answer
 
v2
combobox1.SelectedValue= "INR";
 
Share this answer
 
Comments
BillWoodruff 1-Dec-11 21:21pm    
SelectedValue will not change the selected item in the ComboBox DropDownList: SelectedValue "Gets or sets the value of the member property specified by the ValueMember property."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900