仅选择了显示成员而不是组合框中的值成员 [英] Only the displaymember is getting selected not the valuemember in combo box

查看:51
本文介绍了仅选择了显示成员而不是组合框中的值成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的组合框中选择一些东西

ex:

i'm trying to select something inside of my combobox
ex:

cmbDMGCODE.DisplayMember = "Description";




cmbDMGCODE.ValueMember = "DamageCode";





描述我得到了损坏的描述但它的价值是41

我什么时候才会在文本框中添加它只有我没有得到数字41的显示成员我没有得到我显示的假想价值成员。



这是我的代码





on the description i got the description of the damage but it's value is 41
when ever i will add it in textbox only the displaymember that i got not the number 41 i'm not getting the supposed valuemember of my display.

here's my code

private void LoadDamage()
       {
           try
           {
               DataTable dt = bt.GetDamage();
               DataRow row = dt.NewRow();
               row[0] = "Select Damage";
               row[1] = "";
               dt.Rows.InsertAt(row, 0);
               cmbDMGCODE.DataSource = dt;
               cmbDMGCODE.DisplayMember = "Description";
               cmbDMGCODE.ValueMember = "DamageCode";
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }







private void btnAddDMG_Click(object sender, EventArgs e)
        {
            if (cmbDMGCODE.SelectedIndex < 2) { return; }
            if (dmgList.Text.Length > 0)
            {
                string dam = dmgList.Text.Trim() + ",";
                string value = Lefts(cmbDMGCODE.Text, Convert.ToInt32(IIf((cmbDMGCODE.Text.IndexOf("(") - 1) > 0, cmbDMGCODE.Text.IndexOf("(") - 1, cmbDMGCODE.Text.Length)));
                if (dam.IndexOf(value) < 0)
                {
                    dmgList.Text = dmgList.Text.Trim() + "," + Lefts(cmbDMGCODE.Text, Convert.ToInt32(IIf((cmbDMGCODE.Text.IndexOf("(") - 1) > 0, cmbDMGCODE.Text.IndexOf("(") - 1, cmbDMGCODE.Text.Length)));
                }
            }
            else
            {
                dmgList.Text = Lefts(cmbDMGCODE.Text, Convert.ToInt32(IIf((cmbDMGCODE.Text.IndexOf("(") - 1) > 0, cmbDMGCODE.Text.IndexOf("(") - 1, cmbDMGCODE.Text.Length)));
            }
        }

< br $>







private object IIf(bool expression, object truevalue, object falsevalue)
        {
            return expression ? truevalue : falsevalue;
        }
        private string Lefts(string s, int len)
        {
            return s.Substring(0, len);
        }




private string Lefts(string s, int len)
       {
           return s.Substring(0, len);
       }





我尝试过:



i试图扭转显示和价值,但正如我所说的只有我将在文本框中显示的显示。



What I have tried:

i tried to reverse the display and value but as i said only the display i will get in my textbox.

推荐答案

那是因为当你读出值时,你正在使用ComboBox的Text属性。

DisplayMember告诉ComboBox使用数据的属性并在其上调用ToString进行显示 - 这个字符串成为Text ComboBox的属性

ValueMember告诉ComboBox使用数据的属性并将该值作为Value属性返回。

这允许您显示一组用户名,并将UserID作为选定值返回。例如。
That's because you are using the Text property of the ComboBox when you read out the value.
The DisplayMember tells the ComboBox to use that property of the data and to call ToString on it for display - this string becomes the Text property of the ComboBox
The ValueMember tells the ComboBox to use that property of the data and return that value as the Value property.
This allows you to display a set of user names, and return the UserID as the selected value for example.


这篇关于仅选择了显示成员而不是组合框中的值成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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