How to bind data in datagridview combobox column

眉间皱痕 提交于 2019-11-29 14:22:50

Just select only Name and City in dt, so that you can do something like,

        dgv_ClientDetail.DataSource = dt;
        DataGridViewComboBoxColumn dgvCboColumn = new DataGridViewComboBoxColumn();
        dgvCboColumn.Name = "Contacts";
        dgvCboColumn.DataSource = dtContacts; //DataTable that contains contact details
        dgvCboColumn.DisplayMember = "Name";
        dgvCboColumn.ValueMember = "Id";
        dataGridView1.Columns.Add(dgvCboColumn);

EDIT:

        dgv_ClientDetail.DataSource = new DataView(dt)
                                         .ToTable(true, new string[] { "Name", "City" });
        DataGridViewComboBoxColumn dgvCboColumn = new DataGridViewComboBoxColumn();
        dgvCboColumn.Name = "Contacts";
        dgv_ClientDetail.Columns.Add(dgvCboColumn);
        foreach (DataGridViewRow row in dgv_ClientDetail.Rows)
        {
            DataGridViewComboBoxCell cboContacts = (DataGridViewComboBoxCell)
                                                         (row.Cells["Contacts"]);
            cboContacts.DataSource = //Get the contact details of a person,
                                     //using his Name or Id field (row.Cells["Name"]);
            cboContacts.DisplayMember = "Name"; //Name column of contact datasource
            cboContacts.ValueMember = "Id";//Value column of contact datasource
        }

Hope this helps...

Try something like this

// Loop through rows and get each combobox

foreach (DataGridViewRow row in dgv_ClientDetail.Rows)
{
     DataGridViewComboBoxCell ContactCombo = (DataGridViewComboBoxCell)(row.Cells[index of Contact column]);

     ContactCombo.DataSource = // your contacts datasource;
     ContactCombo.DisplayMember = "name of field to be displayed like say ContactName";
     ContactCombo.ValueMember = "Id";
}

Edit: You need to set ValueMember for combobox as well

Try This:-

First just have data key in DataGridView for the contact. And on ItemDatabound Events just use the datakey for filter for each Dataitem with in that DataGridView.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!