【问题标题】:DataGridView + ComboBox + data from 2 tablesDataGridView + ComboBox + 来自 2 个表的数据
【发布时间】:2012-06-26 09:44:59
【问题描述】:

我有 2 张桌子:

第一:

id | 2ndTableId | Name

第二:

id | name

我希望它们在 dataGridView 上显示为列: 1 -> 带有来自第二个表的名称列表的组合框(带有一些 where 子句),选定的值 = 来自第一个表 '2ndTableId' 2-> 第一个表的名称

此外,我想保留 ComboBox 2 值(id 和 name)。我使用 Id 和 Name 属性创建了自己的 MyComboBox 类,但出现错误:

System.argumentException:DataGridViewComboBoxCell 值无效。

我不知道如何管理这个。你能帮帮我吗?

【问题讨论】:

    标签: c# datagridview combobox npgsql


    【解决方案1】:

    我有同样的情况。我为两个表创建了两个 DTO 类并分配了它们。 创建两个类

    public class Table1
    {
        public int Id { get; set; }
        public int Table2Id { get; set; }
        public string Name { get; set; }
    }
    
    public class Table2
    {
        public int Table2Id { get; set; }
        public string Table2Name { get; set; }
    }
    

    然后在你里面创建一个有四列的datagridview。

    Column1=> Name:idDropDown Default Text Style:DataGridViewCellStyle { }
    Column 2=> Name:Table1Id DataPropertyName:Table1Id 
    Column 3=> Name:Table2Id DataPropertyName:Table2Id 
    Column 4=> Name:Tbale1Name DataPropertyName:Tbale1Name 
    

    然后生成两个列表,其中包含您需要的数据。

    List<Table1> dropDownList;
    List<Table2> gridData;
    

    在你的代码中使用如下

    idDropDown.DisplayMember="Table2Name";
    idDropDown.ValueMember="Table2Id";
    idDropDown.DataSource=dropDownList;
    
    gridview1.AutoGenerateColumns = false;
    gridview1.DataSource=gridData;
    

    主要是我们必须在这段代码中将 AutoGenerateColumns 设置为 False

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      相关资源
      最近更新 更多