将 DGV 中的列修改为绑定到数据库的组合框 [英] Modifying a column in DGV to be a Combobox Bound to DataBase

查看:18
本文介绍了将 DGV 中的列修改为绑定到数据库的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a view named "FamilyView" to join 2 tables(Families and SStatus) and replace an id in the first table(Families) by its name existing in the second table(SStatus) (the column is now called "Status")

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

This view is now displayed in a datagridview that the user can modify

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

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

What i want is to transform the column cells "Status" to comboboxes containing all Names from 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";

但我不能更换电池

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

and this code but I'm not sure how to use it:

将绑定组合框添加到 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);

推荐答案

你的数据源是空的:它应该包含 2 个表:一个是 SStatus,一个是 Families.

Your data source is empty: it should contain 2 tables: one with the SStatus and one with Families.

然后按照我在你的帖子中的描述:

and then follow my as in your post:

          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"表中被称为协议)被映射到协议名称.

Here idProtocol (which is referenced as protocol in the "pcap" table) is mapped to protocolName.

这篇关于将 DGV 中的列修改为绑定到数据库的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆