【问题标题】:DataGridView binded to DataTable with ComboBoxDataGridView 使用 ComboBox 绑定到 DataTable
【发布时间】:2015-11-29 06:26:17
【问题描述】:

我有 DataGridView dgvData,它有两列。

1 Column 是 DataGridViewComboBoxCell 的类型,我将此列链接到人员的 DataSource。

People 具有 Name 和 ID 属性,因此,我将第一列 ValueMember 设为“ID”,将 DisplayMember 设为“Name”。

现在,我想将 DataTable 链接到 DataGridView。 DataTable 有 2 列,PeopleName 和 PeopleCallPhone。

我希望绑定将 PeopleName 匹配到我的 DataGridView 的第一列,并将 CallPhone 绑定到我的 DataGridView 的第二列。

在此之后,当我在整个 DataGridView 上循环时,我希望只找到我的第一列的值,我的意思是人的 ID(来自第 1 列的数据源 - 人)

你们能帮帮我吗?

【问题讨论】:

  • 请说明您的类型:People.IDintPeople.NamestringPeopleNamestring(匹配 People.Name),而 PeopleCallPhone 是什么?
  • 一些字符串,与 People 类无关。

标签: c# data-binding datagridview combobox datatable


【解决方案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" 与源自peopleDataGridViewComboBoxColumn 一致,您必须设置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);
        }
    }
}

输出:

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 2011-05-16
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多