combobox.selectedvalue在winform中显示{} [英] combobox.selectedvalue shows {} in winform

查看:164
本文介绍了combobox.selectedvalue在winform中显示{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



组合框的填充如下:

Hi,
A combobox is populated as follows:

DataTable dt = StatusGet();

            DataRow newRow = dt.NewRow();

            dt.Columns["FullName"].MaxLength = 50;

            newRow["StatusID"] = 0;
            newRow["FullName"] = "<-- Select Status -->";

            dt.Rows.InsertAt(newRow, 0);

            cboTaxStatus.Items.Clear();
            cboTaxStatus.DisplayMember = "FullName";
            cboTaxStatus.ValueMember = "StatusID";
            cboTaxStatus.DataSource = dt;

            cboTaxStatus.SelectedIndex = 0; //None selected...



然后我从组合框中选择一个项目。

但是,所选的值没有设置我得到:




Then I select an item from the combobox.
BUT, the selectedvalue does not get set and I get:

cboTaxStatus.SelectedValue --> shows as {}



请注意,我希望看到StatusID的值,即1或2,具体取决于从组合框中选择的项目。

另外,请注意,如果我在生产中执行此操作,我会获得selectedValue的值,但如果在开发服务器中运行,我会得到{}。

检索数据的源在开发和生产中填充组合框是完全相同的。



有什么想法吗?

谢谢


Note that I expect to see the value of the StatusID i.e. 1 or 2 depending on which item is selecte dfrom the combobox.
Also, note that if I do this in production, I do get a value for the selectedValue but I get {} if this is run in development server.
The source where the data is retrieved to populate the combobox is exactly the same in both development and production.

Any thoughts please?
Thanks

推荐答案

这种方法并不好。



这是一个全面的方法:不要将字符串存储为列表部分的元素组合框。任何其他类型的对象可以是元素。唯一的问题是:当在UI中显示时,项目中将显示哪些文本?答案是:方法返回的任何内容 ToString 。因此,解决方案是:在您的结构或类中覆盖虚拟方法 Object.ToString 以用作列表元素。



请查看我的过去答案,其中包含代码示例。请注意,您可以使用 ComboBox 的方法也可以用于 ListBox 和其他一些类型,所以不要''混淆:我的所有答案都适用于你的情况。请参阅:

Windows窗体列表框选择 [ ^ ],

通过此站点将Combobox中的值发送到应用程序中的numericUpDown [ ^ ],

我希望从组合框中选择项目的数量并自动计算列表框中的价格 [ ^ ],

从ComboBox获取文本 [ ^ ],

从列表框中显示图像 [ ^ ],

C#动态GUI对象生成 [ ^ ],

C#我想存储在说文本框控件中输入的设置值,以保存在组合框中的项目下。 [ ^ ]。



-SA
This approach is not good.

Here is a comprehensive approach: don''t store string as the element of the list part of combo box. An object of any other type can be an element. The only problem is: what text will be shown in the item when it is presented in UI? The answer is: whatever is returned by the method ToString. So, the solution is: override the virtual method Object.ToString in your structure or class to be used as the list element.

Please see my past answers where it is explained with code sample. Note that the method you can use with ComboBox can also be used for ListBox and some other types, so don''t be confused: all my answers are applicable to your case. Please see:
Windows Form ListBox Selections[^],
Send value from Combobox to numericUpDown in application from this site[^],
i want select item''s quantity from combo-box and calculate price in list box automatically[^],
get text from ComboBox[^],
Displaying an image from a list box[^],
C# Dynamic GUI Object Generation[^],
C# I want to store a set values entered in say textbox controls to be saved under an item which is in the combo box.[^].

—SA


使用SelectedItem而不是SelectedValue。 SelectedItem将是DataRowView的类型。您将通过使用列索引或列名读取所选行中的所有值。



Use SelectedItem rather than SelectedValue. The SelectedItem will be type of DataRowView. You will get all values from the selected row by reading with either column index or column name.

DataRowView dr = (DataRowView)comboBox1.SelectedItem;
dr["StatusID"]  -->  This will give you the SelectedValue


这篇关于combobox.selectedvalue在winform中显示{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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