使用valuemember搜索combobox displaymember值

时间:2016-05-02 20:58:24

标签: c# datagridview combobox valuemember

我有一个datagridview,其中列出了customerID。 我有一个列出customerName的组合框。

当我点击datagridview时,我知道选择了customerID,但我无法在组合框上列出此cus​​tomerID的customerName。

帮助?

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;
}
  

例如CustomerID = 6 CustomerName =“Rocky”,CustomerSurName =“Balboa”FullName =“Rocky Balboa”

     

记录在datagridview上的CustomerID列表。当CustomerID = 6的选择行时,我想在cbx_FullName上看到FullName

1 个答案:

答案 0 :(得分:0)

在datagridview的cellclick事件中

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);
}