C# 如何绑定datagridview组合框列中的数据

C# 如何绑定datagridview组合框列中的数据,c#,windows,datagridviewcombobox,C#,Windows,Datagridviewcombobox,这里有联系人,但我们可以有多个联系人,所以我想在组合框中显示一个列表 DataTable dt = new DataTable(); dt = MainClass.GetDatabyQuery("select * from tbl"); if (dt.Rows.Count > 0) { dgv_ClientDetail.DataSource = dt; } 我使用此方法从datagridview中的数据库中获取值,但我需要一个datagridview组合框列,并希望在一个dgv

这里有联系人,但我们可以有多个联系人,所以我想在组合框中显示一个列表

DataTable dt = new DataTable();
dt = MainClass.GetDatabyQuery("select * from tbl");

if (dt.Rows.Count > 0)
{
    dgv_ClientDetail.DataSource = dt;
}

我使用此方法从datagridview中的数据库中获取值,但我需要一个datagridview组合框列,并希望在一个dgv组合框中绑定数据,在dgv组合框中绑定其他数据。如果有人知道,那就告诉我。这里有三列:姓名、城市、联系人。我想在dgv组合框列中显示多个联系人

只需在
dt
中选择
Name
City
,您就可以执行以下操作:

        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);
编辑:


希望这有帮助…

试试这样的方法

// 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";
}
编辑:您还需要为combobox设置ValueMember

尝试以下操作:-


首先,在DataGridView中为联系人设置数据键。对于ItemDatabound事件,只需对DataGridView中的每个Dataitem使用datakey for筛选器

您需要详细说明,显示您的gridview结构我无法发布图像。没有太多的名声这一排有组合框吗?每行都有一个文本框。combobox和textbox是否属于不同的列?请详细解释一下。这里有三个Coulumn的名字,城市,联系人。我想在dgv组合框列中显示多个联系人,但在dgv文本框列中显示姓名和城市我想您想为每行将城市绑定到城市组合框中?右侧它显示的是每行中相同的联系人。是的…那么您准备在每行中设置不同的联系人集吗?是的,在新行中,有一个新成员有两个联系人,我想在dgv combo中显示他的行中的这两个联系人。如果任何实体有1个触点,并且在组合不工作时应显示1个触点。给出错误“无法将'System.Windows.Forms.DataGridViewTextBoxCell'类型的对象强制转换为'System.Windows.Forms.DataGridViewComboxCell'@user3355213如何?给出错误”无法将类型为“System.Windows.Forms.DataGridViewTextBoxCell”的对象强制转换为类型为“System.Windows.Forms.DataGridViewComboxCell”。这意味着正在拾取textbox单元格,请检查是否传递了正确的索引,从零开始。如果列的顺序为名称、城市和联系人。然后联系人组合框列具有索引2。
// 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";
}