Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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! enter image description here

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?

like image 328
lemunk Avatar asked Feb 15 '26 06:02

lemunk


2 Answers

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]);
}
like image 187
KV Prajapati Avatar answered Feb 17 '26 19:02

KV Prajapati


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 .

like image 41
Sandeep Pathak Avatar answered Feb 17 '26 18:02

Sandeep Pathak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!