【问题标题】:Modifying a column in DGV to be a Combobox Bound to DataBase将 DGV 中的列修改为绑定到数据库的组合框
【发布时间】:2013-05-21 10:31:24
【问题描述】:

我创建了一个名为“FamilyView”的视图来加入 2 个表(Families 和 SStatus),并将第一个表(Families)中的 id 替换为第二个表中存在的名称(SStatus)(该列现在称为“Status” ")

此视图现在显示在用户可以修改的数据网格视图中

        Sda = new SqlDataAdapter("Select * from FamilyView",con);
        Sda.Fill(ds);
        dg.DataSource = ds.Tables[0];
        dg.Refresh();

我想要的是将列单元格“状态”转换为包含来自 SStatus 的所有名称的组合框

        SqlDataAdapter sadapter = new SqlDataAdapter("Select * From SStatus", con);
        DataSet ds1 = new DataSet();
        sadapter.Fill(ds1);

我找到了这段代码:

        DataGridViewComboBoxCell ComboColumn = (DataGridViewComboBoxCell)(dg.Rows[i].Cells[0]);
        ComboColumn.DataSource = ds1.Tables[0];
        ComboColumn.DisplayMember = "Name";

但我无法替换单元格

还有这段代码,但我不知道如何使用它:

Adding bound combobox to datagridview

        BindingSource StatusBD = new BindingSource();
        StatusBD.DataSource = ds1;


        DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();
        colType.HeaderText = "Status";
        colType.DropDownWidth = 90;
        colType.Width = 90;
        //colType.DataPropertyName = "Name";
        colType.DataSource = ds;
        colType.DisplayMember = "Name";
        colType.ValueMember = "IdStatus";

        dg.Columns.Insert(dg.Columns.GetColumnCount(DataGridViewElementStates.None) - 1, colType);

【问题讨论】:

    标签: c# sql workflow-foundation


    【解决方案1】:

    您的数据源是空的:它应该包含 2 个表:一个包含 SStatus,另一个包含 Families。

    然后按照我在您的帖子中的方式进行操作:

              DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();
            BindingSource wizardBindingSource = new BindingSource();
            wizardBindingSource.DataSource = dataSet; 
            wizardBindingSource.DataMember = "protocol"; // This is the table in the set
            colType.HeaderText = "Type";
            colType.DropDownWidth = 90;
            colType.Width = 90;
            colType.DataPropertyName = "wizardProtocol";
            colType.DataSource = wizardBindingSource;
            colType.DisplayMember = "protocolName";
            colType.ValueMember = "idprotocols"
    

    这里 idProtocol(在“pcap”表中被称为协议)映射到协议名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      相关资源
      最近更新 更多