如何在datagridview组合框列中绑定数据
How to bind data in datagridview combobox column
这是联系人,但我们可以有多个联系人,所以我想在组合框中显示一个列表
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 组合框列中显示多个联系人
只是 select 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
}
希望这对您有所帮助...
尝试这样的事情
// 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
试试这个:-
首先在 DataGridView 中为联系人设置数据键。在 ItemDatabound Events 上,只需使用数据键来过滤该 DataGridView 中的每个数据项。
这是联系人,但我们可以有多个联系人,所以我想在组合框中显示一个列表
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 组合框列中显示多个联系人
只是 select 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
}
希望这对您有所帮助...
尝试这样的事情
// 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
试试这个:-
首先在 DataGridView 中为联系人设置数据键。在 ItemDatabound Events 上,只需使用数据键来过滤该 DataGridView 中的每个数据项。