简体   繁体   中英

C# combo box value and display using invoke method

I am using Windows Forms , and I have a combo box with the values:

Apple
Oranges
Pairs

My text box contains values 1, 2 and 3,

apple = 1 and oranges = 2 and pairs = 3.

This relation is in my table on SQL, fields Account and Name. How do I change the value of the textbox to = 2 if the combo box is selected as oranges?

Basically this is reference to a program I'm working on that has an account combo box that displays the accounts names (user friendly). I need the names ID account number to then be displayed in the text box. From this text box I can then pass the value into my stored procedure that will get all the information on that account.

Update

The above text is out of date. See below!

A note to add is that my code gets the account details and populates my combo box.

Is there an easy way to do this? Or does this require joining in C# similar to SQL joins on tables?

Or without using a text box at all, basically my combo would have 2 fields orange, 2 but only display oranges to the user, but my other method uses the second field 2.

OK, so this part adds the acc field to the combo box.

{
    SqlCommand accountFill = new SqlCommand("SELECT name, acc FROM dbo.Customer", conn1);
    SqlDataReader readacc = accountFill.ExecuteReader();

    while (readacc.Read())
    {
        AddItem(readacc.GetString(0).ToString(), readacc, "name", "acc");
    }
    conn1.Close();
}

Add item is an invoke method used by my task.

private void AddItem(string value, Object dataSource, string name, string acc)
{
    if (accCollection.InvokeRequired)
    {
        accCollection.Invoke(new Action<string,Object,string,string>(AddItem), new Object[] { value });
    }
    else
    {
        //accCollection.Items.Add(value);
        accCollection.DataSource = dataSource;
        accCollection.DisplayMember = name;
        accCollection.ValueMember = acc;
    }
}

ERROR complex databinding accepts as a datasource iether an i.list or an i.list source at datasource.

The ComboBox class has two properties ValueMember and DisplayMember . You need to set the DisplayMember property to the Names of your table or data, ie orange, apple, etc... ,and set the ValueMember property to the Id's of them:

combobox1.Datasource = readacc;
combobox.DisplayMember = 'name';
combobox.ValueMember = 'acc';

All you need then is just use the two properties SelectedValue to get the Id or SelectedText to get the value. For example, you could get the id of the selected item by using ComboBox.SelectedIndexChanged Event like this:

private void ComboBox1_SelectedIndexChanged(object sender, 
    System.EventArgs e)
{
    textbox2.text = int.parse(combobox1.SelectedValue.ToString());
}

or you can get the select text like:

textbox2.text = combobox1.SelectedText;
    DataTable dt = new DataTable("ListItem");
    dt.Columns.Add("Id", Type.GetType("System.Int32"));
    dt.Columns.Add("Name", Type.GetType("System.String"));

    dt.Rows.Add(new object[]{1,"apple"});
    dt.Rows.Add(new object[]{2,"oranges"});
    dt.Rows.Add(new object[]{3,"pairs"});

    comboBox1.DataSource = dt;
    comboBox1.DisplayMember = "Name";
    comboBox1.ValueMember="Id";

    textBox1.DataBindings.Add("Text", dt, "Id");

or

    comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
    comboBox1.SelectedIndex = -1; //for initial step;

void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    textBox2.Text = comboBox1.SelectedValue.ToString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM