Click here to Skip to main content
16,004,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm trying to select something inside of my combobox
ex:
cmbDMGCODE.DisplayMember = "Description";

cmbDMGCODE.ValueMember = "DamageCode";


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)));
            }
        }



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);
       }


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.
Posted
Updated 14-Oct-18 22:25pm

1 solution

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.
 
Share this answer
 

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