如何在组合框中设置默认值 [英] How to set deafault value in combobox

查看:130
本文介绍了如何在组合框中设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在开发一个C#Windows应用程序,其中有一个组合框,该组合框以升序显示货币名称.我想将默认货币设置为USD,但无法更改顺序.请帮助我.方案

 combobox1.SelectedText = " ; // 使用任何适用的字符串 



请记住,使用文本设置组合框是不明智的.最好设置SelectedIndexSelectedItem.


更好的方法是从数据源中获取ID,然后将其分配给下拉选择的值. >
如果您的数据源类型为Table,则可以执行以下操作.


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



假设"USD"将始终位于数据源中.如果没有,则此代码将引发异常.那么在这种情况下,您可以申请

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

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




希望这会有所帮助.


comboBox1.SelectedItem = "USD";

也可以.

仅供参考:"SelectedItem"是ComboBox控件的本机属性:"SelectedValue"是ListControl的继承属性.实际上,除了在使用数据绑定的场景中,这几乎没有什么区别.参见:[^ ]

但是,请注意SelectedItem的工作方式为:"ComboBox类使用IndexOf方法搜索指定的对象.此方法使用Equals方法确定相等性." ...来自.C#文档.

如果您经常在应用程序中设置/重置默认货币,或者货币列表是动态变化的,那么可能更好地计算出来.在需要时,可以将ComboBox列表中的默认货币指数计算在内.一个整数变量,然后将该变量用作SelectedIndex的参数.

但是,如果这是一次性的,是在应用程序初始化时完成的,并且默认货币列表未更改或不需要重置",则使用哪种技术可能都没有关系.


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..

解决方案

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.


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.


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.


这篇关于如何在组合框中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆