Question

I have table with 4 primary key fields. I load that in to drop down list in my WinForm application created by using C#.

On the TextChanged event of drop down list I have certain TextBox and I want to fill the information recived by the table for the certain field I selected by the drop down list.

So as I say the table having 4 fields. Can I get those all 4 fields into value member from the data set, or could you please tell me whether is that not possible?

Thank you.

Datatable dt=dba.getName(); cmb_name.ValueMember="id"; cmb_name.DisplayMember="name"; cmb_name.DataSource=dt;

this is normal format.. but i have more key fields.. so i need to add more key fields..

Was it helpful?

Solution

You can use DataSource property to bind your source data to the ComboBox (e.g. a List of Entities, or a DataTable, etc), and then set the DisplayMember property of the ComboBox to the (string) name of the field you want to display.

After the user has selected an Item, you can then cast the SelectedItem back to the original row data type (Entity, DataRow, etc - it will still be the same type as you put in), and then you can retrieve your 4 composite keys to the original item.

This way you avoid the SelectedValue problem entirely.

Edit:

Populate as follows:

cmb_name.DisplayMember = "name";
cmb_name.DataSource = dt;

// Ignore ValueMember and Selected Value entirely

When you want to retrieve the selected item

var selectedRow = (cmb_name.SelectedItem as DataRowView );

Now you can retrieve the 4 values of your PK, e.g. selectedRow["field1"], selectedRow["field2"], selectedRow["field3"] etc

If however you mean that you want to DISPLAY 4 columns to the user (i.e. nothing to do with your Table Key), then see here How do I bind a ComboBox so the displaymember is concat of 2 fields of source datatable?

OTHER TIPS

cmb_name.DisplayMember = "name";
cmb_name.DataSource = dt;
DataRowView selectedRow = (cmb_name.SelectedItem as DataRowView );

The result will be here:

MessageBox.Show(selectedRow.Row[0].ToString());
MessageBox.Show(selectedRow.Row[1].ToString());
MessageBox.Show(selectedRow.Row[2].ToString());
MessageBox.Show(selectedRow.Row[3].ToString());
.....

If you want to get some data from a ComboBox in to a List you can use something like this

List<string> ListOfComboData = new List<string>();
ListOfComboData = yourComboBox.Items.OfType<string>().ToList<string>();

I have no real idea if this is what you mean as the question is very poorly structured. I hope this helps...

Edit: To put the selected text in to some TextBox use

yourTextBox.Text = youComboBox.Text;

in the SelectedIndexChanged event of your ComboBox.

You could follow the approach here with the following class:

public class ComboBoxItem
{
   public string Text { get; set; }
   public object[] PrimaryKey { get; set; }
}

private void Test()
{
    ComboboxItem item = new ComboboxItem();
    item.Text = "Item text1";
    item.PrimaryKey = new object[] { primaryKey1, primaryKey2, primaryKey3, primaryKey4};

    comboBox1.Items.Add(item);

    comboBox1.SelectedIndex = 0;

    MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top