DataGridView 使用 ComboBox 绑定到 DataTable
DataGridView binded to DataTable with ComboBox
我有 DataGridView dgvData,它有两列。
1 列是DataGridViewComboBoxCell 的类型,我link将此列编辑到人的DataSource。
People 具有 Name 和 ID 属性,因此,我将第 1 列 ValueMember 设为 "ID",将 DisplayMember 设为 "Name"。
现在,我想link DataTable 到DataGridView。
DataTable 有 2 列,PeopleName 和 PeopleCallPhone。
我希望绑定将 PeopleName 与我的 DataGridView 的第一列相匹配,并将 CallPhone 绑定到我的 DataGridView 的第二列。
在此之后,当我在整个 DataGridView 上循环时,我希望只找到第一列的值,我指的是人员的 ID(来自列 1 的数据源 - 人员)
你们能帮帮我吗?
让我们假设以下数据库设置:
╔════════════════════════════╗ ╔═════════════════════════════════╗
║ People ║ ║ Your DataTable Info ║
╠════╦═══════════════════════╣ ╠═══════════════╦═════════════════╣
║ ID ║ Name ║ ║ PeopleName ║ PeopleCallPhone ║
╠════╬═══════════════════════╣ ╠═══════════════╬═════════════════╣
║ 1 ║ "John Smith" ║ ║ "John Smith" ║ 123-456-7890 ║
║ 2 ║ "Jane Doe" ║ ║ "Jane Doe" ║ 234-567-8900 ║
║ 3 ║ "Foo Bar" ║ ║ "Foo Bar" ║ 345-678-9000 ║
║ 4 ║ "Justin Time" ║ ║ "Justin Time" ║ 456-789-0000 ║
║ 5 ║ "Imma Mann" ║ ║ "Imma Mann" ║ 567-890-0000 ║
╚════╩═══════════════════════╝ ╚═══════════════╩═════════════════╝
此外,我们假设您的数据结构是:
List<People> people = GetPeopleFromDB();
DataTable table = GetDataTableInfoFromDB();
为了使 DataTable
列 "PeopleName"
与来自 people
的 DataGridViewComboBoxColumn
重合,您必须设置 DataGridViewComboBoxColumn.DataPropertyName
。因为 DataTable
列中的值与 People.Name
匹配,所以必须在 DataGridViewComboBoxColumn.ValueMember
上设置 属性。例如:
var col = new DataGridViewComboBoxColumn();
col.Name = "PeopleName";
col.DataPropertyName = "PeopleName"; // The DataTable column name.
col.HeaderText = "Name";
col.DataSource = people;
col.DisplayMember = "Name";
col.ValueMember = "Name"; // People.Property matching the DT column.
this.dataGridView1.Columns.Add(col);
this.dataGridView1.DataSource = table;
this.dataGridView1.Columns[1].HeaderText = "Phone";
结果:
至于你的第二个问题,要在遍历每一行时找到每个条目的 ID,你首先要获取 ComboBoxColumn 的源。然后您可以遍历每一行并使用第一列中的值,在源中找到与该值关联的 ID。例如:
List<People> ppl = ((DataGridViewComboBoxColumn)this.dataGridView1.Columns[0]).DataSource as List<People>;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
if (row.Index != this.dataGridView1.NewRowIndex)
{
var cell = row.Cells[0] as DataGridViewComboBoxCell;
People person = ppl.SingleOrDefault(p => p.Name == cell.Value.ToString());
if (person != null)
{
Console.WriteLine("{0} {1}, {2}", person.ID, person.Name, row.Cells[1].Value);
}
}
}
输出:
我有 DataGridView dgvData,它有两列。
1 列是DataGridViewComboBoxCell 的类型,我link将此列编辑到人的DataSource。
People 具有 Name 和 ID 属性,因此,我将第 1 列 ValueMember 设为 "ID",将 DisplayMember 设为 "Name"。
现在,我想link DataTable 到DataGridView。 DataTable 有 2 列,PeopleName 和 PeopleCallPhone。
我希望绑定将 PeopleName 与我的 DataGridView 的第一列相匹配,并将 CallPhone 绑定到我的 DataGridView 的第二列。
在此之后,当我在整个 DataGridView 上循环时,我希望只找到第一列的值,我指的是人员的 ID(来自列 1 的数据源 - 人员)
你们能帮帮我吗?
让我们假设以下数据库设置:
╔════════════════════════════╗ ╔═════════════════════════════════╗
║ People ║ ║ Your DataTable Info ║
╠════╦═══════════════════════╣ ╠═══════════════╦═════════════════╣
║ ID ║ Name ║ ║ PeopleName ║ PeopleCallPhone ║
╠════╬═══════════════════════╣ ╠═══════════════╬═════════════════╣
║ 1 ║ "John Smith" ║ ║ "John Smith" ║ 123-456-7890 ║
║ 2 ║ "Jane Doe" ║ ║ "Jane Doe" ║ 234-567-8900 ║
║ 3 ║ "Foo Bar" ║ ║ "Foo Bar" ║ 345-678-9000 ║
║ 4 ║ "Justin Time" ║ ║ "Justin Time" ║ 456-789-0000 ║
║ 5 ║ "Imma Mann" ║ ║ "Imma Mann" ║ 567-890-0000 ║
╚════╩═══════════════════════╝ ╚═══════════════╩═════════════════╝
此外,我们假设您的数据结构是:
List<People> people = GetPeopleFromDB();
DataTable table = GetDataTableInfoFromDB();
为了使 DataTable
列 "PeopleName"
与来自 people
的 DataGridViewComboBoxColumn
重合,您必须设置 DataGridViewComboBoxColumn.DataPropertyName
。因为 DataTable
列中的值与 People.Name
匹配,所以必须在 DataGridViewComboBoxColumn.ValueMember
上设置 属性。例如:
var col = new DataGridViewComboBoxColumn();
col.Name = "PeopleName";
col.DataPropertyName = "PeopleName"; // The DataTable column name.
col.HeaderText = "Name";
col.DataSource = people;
col.DisplayMember = "Name";
col.ValueMember = "Name"; // People.Property matching the DT column.
this.dataGridView1.Columns.Add(col);
this.dataGridView1.DataSource = table;
this.dataGridView1.Columns[1].HeaderText = "Phone";
结果:
至于你的第二个问题,要在遍历每一行时找到每个条目的 ID,你首先要获取 ComboBoxColumn 的源。然后您可以遍历每一行并使用第一列中的值,在源中找到与该值关联的 ID。例如:
List<People> ppl = ((DataGridViewComboBoxColumn)this.dataGridView1.Columns[0]).DataSource as List<People>;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
if (row.Index != this.dataGridView1.NewRowIndex)
{
var cell = row.Cells[0] as DataGridViewComboBoxCell;
People person = ppl.SingleOrDefault(p => p.Name == cell.Value.ToString());
if (person != null)
{
Console.WriteLine("{0} {1}, {2}", person.ID, person.Name, row.Cells[1].Value);
}
}
}
输出: