简体   繁体   中英

Get selected value from comboBox

I'm using C#, winforms.

Got a small problem. I've debugged as much as possible that leads me to believe the code I'm using is causing the problem. Ok so I have a combo box that is filled with data from a query.

There are 2 columns "name", and "keycode". I display the name only using:

accCollection.DisplayMember = "name";

Then I use the following to get the keycode value that corresponds to the name.

string acct = accCollection.SelectedValue.ToString();

The problem I have is the keycode does NOT match the name. I though this may be my query so what I did was to display the query results in a data grid view JUST before I fill the comboBox. The data grid view displays the correct results which leads me to believe that I'm using the wrong code!

Hopefully it's a silly one line problem, if you guys want any more code let me know and I will edit this post.

UPDATE heres the code i forgot to mention, as you can see i already have assigned the data member

accCollection.DataSource = myTable;
accCollection.DisplayMember = "name";
accCollection.ValueMember = "keycode";

UPDATE:::: Ok some more info. the selected value should be 1557 which is the names account number. but i get 1855, which is a different account number. like i said the datatable is correct....this is why im sooooooo confused!

UPDATE:: heres some code so you can see how i update the combo box with the info!

SqlCommand accountFill = new SqlCommand("SELECT name, keycode FROM dbo.Customer", conn1);
SqlDataAdapter readacc = new SqlDataAdapter(accountFill);
DataTable dt = new DataTable();


readacc.Fill(dt);
dataGridView3.DataSource = dt;
conn1.Close();
accCollection.DataSource = dt;
accCollection.DisplayMember = "name";
accCollection.ValueMember = "keycode";

then i pass the following into my task that calls my other query.

private void button1_Click_1(object sender, EventArgs e)
{
    checkBox1.Checked = true;
    string acct = accCollection.SelectedValue.ToString();

    Task t = new Task(() => GetsalesFigures(acct));
    t.Start();
}

UPDATE:: just to show the data i get! 在此输入图像描述

ok so after some help with debugging, ive used the follwing code to get these resuults.

var result = accCollection.SelectedValue;
if (result != null)
{
   MessageBox.Show(result.ToString());
}

this = 1885 which is wrong

BUT when i do this.

DataRowView row = (DataRowView)accCollection.SelectedItem;
if (row != null)
{
   MessageBox.Show(row[0] + " " + row[1]);
}

its shows the correct data, "melksham" "1557"

why is this?

You may use SelectedValue or SelectedItem property but also you have to check whether the returned value is null or not.

If you bind the DataTable then the SelectedItem property return DataRowView.

DataRowView row = (DataRowView)accCollection.SelectedItem;
if(row!=null)
{
   MessageBox.Show(row[0] + " " + row[1]);
}

In case of SelectedValue,

var result = accCollection.SelectedValue;
if (result != null)
{
   MessageBox.Show(result.ToString()); 
}          

EDIT:

Code in Form_Load event:

private void Form1_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("A1", typeof(int));
    dt.Columns.Add("A2");
    dt.Rows.Add(1, "A");
    dt.Rows.Add(2, "B");

    comboBox1.DataSource = dt;
    comboBox1.DisplayMember = "A2";
    comboBox1.ValueMember = "A1";
}

Code in Click handler of Button:

var result = comboBox1.SelectedValue;
if (result != null)
{
   MessageBox.Show(result.ToString());
}


DataRowView row = (DataRowView)comboBox1.SelectedItem;
if (row != null)
{
   MessageBox.Show(row[0] + " " + row[1]);
}

Several Things:

  1. DisplayMember property should be the name of the column to be displayed inside the combobox.
  2. ValueMember property should be the name of the column which is the value of the item.

     accCollection.DisplayMember = "name"; accCollection.ValueMember = "key"; 

    If you want the value of the selected item you should use:

     string acct = accCollection.SelectedValue.ToString(); 

    Get the Display text as :

     string acct = accCollection.SelectedText; 

See this for details .

I have created a DataTable & i used to bind the dataTable to the combobox to display the title types and i wanted to get the title id which is in the datatable to a variable.

      cmbTitle.DataSource=dataTable;
      cmbTitle.DisplayMember="title_type";
      cmbTitle.ValueMember="id";

then on the selection changed event of cmbtitle, i got the selected value member to a string and parsed in into an integer to save it back for the database as it is a foreign key.

  private void cmb_title_SelectedIndexChanged(object sender, EventArgs e)
  {
       string userTitle = cmb_title.SelectedValue.ToString();            
       int.TryParse(userTitle, out this.userTitle);
       MessageBox.Show(this.userTitle.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