如何在datagridviewcombobox列中绑定数据

这里是联系人,但我们可以有多个联系人,所以我想在combobox中显示列表

DataTable dt = new DataTable(); dt = MainClass.GetDatabyQuery("select * from tbl"); if (dt.Rows.Count > 0) { dgv_ClientDetail.DataSource = dt; } 

我有这种方法来从datagridview中的数据库中获取值,但我想要一个datagridviewcombobox列,并希望绑定在一个dgvcombobox和其他在dgv texboxes中的数据。 如果有人知道,然后告诉我。 这里有三列名称,城市,联系人。 我想在dgv combobox列中显示多个联系人

Solutions Collecting From Web of "如何在datagridviewcombobox列中绑定数据"

只需在dt选择NameCity ,就可以做类似的事情,

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

编辑:

  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 } 

希望这可以帮助…

尝试这样的事情

 // 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的数据键过滤即可。