search combobox displaymember value with valuemember

705 Views Asked by At

I have a datagridview which lists customerID. I have a combobox which list customerName.

When I clicked to datagridview I know selected customerID but I can't list customerName of this customerID on combobox.

Help?

private void comboboxFill()
{
    dt = new DataTable();
    OleDbDataAdapter da = new OleDbDataAdapter("Select * from Customers", conn);
    da.Fill(dt);
    dt.Columns.Add("FullName", typeof(String), "CustomerName+' '+CustomerSurName");
    cbx_FullName.ValueMember = "CustomerID";
    cbx_FullName.DisplayMember = "FullName";
    cbx_FullName.DataSource = dt;
}

For Example CustomerID=6 CustomerName="Rocky", CustomerSurName="Balboa" FullName= "Rocky Balboa"

the record's CustomerID lists on datagridview. When select row that CustomerID=6 I wanna to see FullName on cbx_FullName

1

There are 1 best solutions below

1
On

in cellclick event of datagridview

private void dataGridView1_CellClick(object sender,DataGridViewCellEventArgs e)
{
    string firstname = dataGridView1.Rows[e.RowIndex].Cells["CustomerName"].Value.ToString();
    string lastname = dataGridView1.Rows[e.RowIndex].Cells["CustomerSurName"].Value.ToString();
    string ID = dataGridView1.Rows[e.RowIndex].Cells["CustomerID"].Value.ToString();
    string fullname = firstname+" "+lastname;
    cbx_FullName.SelectedIndex = cbx_FullName.Items.IndexOf(fullname);
}