以下是联系人,但我们可以有多个联系人,因此我想在组合框中显示一个列表
DataTable dt = new DataTable();
dt = MainClass.GetDatabyQuery("select * from tbl");
if (dt.Rows.Count > 0)
{
dgv_ClientDetail.DataSource = dt;
}
我有这个方法从datagridview中的数据库中获取值,但我想要一个datagridview组合框列,并希望将数据绑定在一个dgv组合框和dgv texbox中的其他组合框中。如果有人知道那么告诉我。这里有三列名称,城市,联系人。我想在dgv组合框列中显示多个联系人
答案 0 :(得分:3)
只需在Name
中选择City
和dt
,以便您可以执行类似的操作,
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
}
希望这会有所帮助......
答案 1 :(得分:0)
尝试这样的事情
// 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";
}
编辑:您还需要为组合框设置ValueMember
答案 2 :(得分:0)
试试这个: -
首先在DataGridView中为联系人提供数据密钥。在ItemDatabound事件上,只需使用datakey过滤DataGridView中的每个Dataitem。