将绑定的组合框添加到 datagridview

Posted

技术标签:

【中文标题】将绑定的组合框添加到 datagridview【英文标题】:Adding bound combobox to datagridview 【发布时间】:2011-11-26 19:36:43 【问题描述】:

场景和http://arsalantamiz.blogspot.com/2008/09/binding-datagridview-combobox-column.html差不多。但我无法让它在 c# 上运行...

我的 mysql 数据库有两个表: 1. 协议 2. pcapdata

在协议表中,我有两个字段:idprotocols 和 protocolName

在我有wizardProtocol的pcaps表中(它“链接”到idprotocols字段)

我想要得到的是一个包含名称的组合框,它将替换向导协议字段。接下来,如果用户更新“名称”组合框,wizardProtocol 将相应更改(因此我将能够相应地更新数据库中的更改)。

现在,在网上看了一些资料后:我写了以下代码:

  public void Bind(ref DataGridView dataGridView)
    
        try
        

            mySqlDataAdapter = new MySqlDataAdapter(SELECT_ALL_PCAP, _con);
            mySqlCommandBuilder = new MySqlCommandBuilder(mySqlDataAdapter);

            mySqlDataAdapter.UpdateCommand = mySqlCommandBuilder.GetUpdateCommand();
            mySqlDataAdapter.DeleteCommand = mySqlCommandBuilder.GetDeleteCommand();
            mySqlDataAdapter.InsertCommand = mySqlCommandBuilder.GetInsertCommand();
            dataSet = new DataSet();
            mySqlDataAdapter.Fill(dataSet, "pcap");


            MySqlDataAdapter adp2 = new MySqlDataAdapter(SELECT_ALL_PROTOCOL, _con);
            MySqlCommandBuilder builder = new MySqlCommandBuilder(adp2);

            adp2.UpdateCommand = builder.GetUpdateCommand();
            adp2.DeleteCommand = builder.GetDeleteCommand();
            adp2.InsertCommand = builder.GetInsertCommand();
            adp2.Fill(dataSet, "protocol");


            bindingSource = new BindingSource();
            bindingSource.DataSource = dataSet;
            bindingSource.DataMember = "pcap";
            dataGridView.DataSource = bindingSource;


            dataGridView.Columns["length"].ReadOnly = true;
            dataGridView.Columns["length"].DefaultCellStyle.ForeColor = System.Drawing.Color.SandyBrown;
            dataGridView.AllowUserToAddRows = false;
            dataGridView.AllowUserToDeleteRows = false;


            DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();
            colType.HeaderText = "Type";
            colType.DropDownWidth = 90;
            colType.Width = 90;
            colType.DataPropertyName = "wizardProtocol";
            colType.DataSource = bindingSource;
            colType.DisplayMember = "protocolName";
            colType.ValueMember = "idprotocols";
            dataGridView.Columns.Insert(dataGridView.Columns.GetColumnCount(DataGridViewElementStates.None) - 1, colType);


        
        catch (System.Exception e)
        
            MessageBox.Show(e.ToString());
        
    

我正在尝试操作 DisplayMember 属性,但我失败了(我知道问题可能与我的数据绑定有关,但我无法弄清楚...)

更新:感谢回答,我正在重新附加固定代码

            mySqlDataAdapter = new MySqlDataAdapter(SELECT_ALL_PCAP, _con);
            mySqlCommandBuilder = new MySqlCommandBuilder(mySqlDataAdapter);

            mySqlDataAdapter.UpdateCommand = mySqlCommandBuilder.GetUpdateCommand();
            mySqlDataAdapter.DeleteCommand = mySqlCommandBuilder.GetDeleteCommand();
            mySqlDataAdapter.InsertCommand = mySqlCommandBuilder.GetInsertCommand();
            dataSet = new DataSet();
            mySqlDataAdapter.Fill(dataSet, "pcap");


            MySqlDataAdapter adp2 = new MySqlDataAdapter(SELECT_ALL_PROTOCOL, _con);
            MySqlCommandBuilder builder = new MySqlCommandBuilder(adp2);

            adp2.UpdateCommand = builder.GetUpdateCommand();
            adp2.DeleteCommand = builder.GetDeleteCommand();
            adp2.InsertCommand = builder.GetInsertCommand();
            adp2.Fill(dataSet, "protocol");



            bindingSource = new BindingSource();
            bindingSource.DataSource = dataSet;
            bindingSource.DataMember = "pcap";
            dataGridView.DataSource = bindingSource;
            dataGridView.AllowUserToAddRows = false;
            dataGridView.AllowUserToDeleteRows = false;


            DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();
            BindingSource wizardBindingSource = new BindingSource();
            wizardBindingSource.DataSource = dataSet; 
            wizardBindingSource.DataMember = "protocol";
            colType.HeaderText = "Type";
            colType.DropDownWidth = 90;
            colType.Width = 90;
            colType.DataPropertyName = "wizardProtocol";
            colType.DataSource = wizardBindingSource;
            colType.DisplayMember = "protocolName";
            colType.ValueMember = "idprotocols";
            dataGridView.Columns.Insert(dataGridView.Columns.GetColumnCount(DataGridViewElementStates.None) - 1, colType); 

【问题讨论】:

我也有同样的问题,请帮忙***.com/questions/16758407/… 【参考方案1】:

你做错的最明显的事情是你为你的datagridview和你的comboboxcolumn使用了相同的绑定源。如果您查看您提供的示例,您会注意到他们创建了第二个 bindingsource productBindingSource。

所以您需要做的是创建一个绑定源(我们称之为wizardProtocolBindingSource),然后用您的协议表中的数据填充它。这将成为您的组合框列的数据源。

关键代码如下所示:

// You bind the datagridview just as before
// this dataset should have the idprotocols field which is your foreign key
// to the protocols table - you will probably want this to be hidden.
bindingSource = new BindingSource(); 
bindingSource.DataSource = dataSet; 
bindingSource.DataMember = "pcap"; 
dataGridView.DataSource = bindingSource; 

// hide the foreign key column
dataGridView.Columns["idProtocols"].Visible = false;

// here we populate your comboboxcolumn binding source
wizardProtocolBindingSource= new BindingSource(); 
// this dataset is from the protocols table
wizardProtocolBindingSource.DataSource = dataSet; 

// Add the combobox column
DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();      
colType.HeaderText = "Type";      
colType.DropDownWidth = 90;      
colType.Width = 90;      
colType.DataSource = wizardProtocolBindingSource;      
// The DataPropertyName refers to the foreign key column on the datagridview datasource
colType.DataPropertyName = "wizardProtocol";    
// The display member is the name column in the column datasource  
colType.DisplayMember = "protocolName";    
// The value member is the primary key of the protols table  
colType.ValueMember = "idprotocols";    
// I usually just add the column but you can insert if you need a particular position  
dataGridView.Columns.Add(colType);      

以上内容应该对你有用,虽然我不知道你的数据集列的名称,但我不得不猜测一下。

【讨论】:

以上是关于将绑定的组合框添加到 datagridview的主要内容,如果未能解决你的问题,请参考以下文章

Access 使用未绑定的组合框查找和添加记录

手动添加时绑定datagridview组合框

在 Windows 窗体应用程序 C# 中以编辑模式将值绑定到组合框

如何将数据源绑定到 datagridview 组合框和复选框

Thymeleaf/Spring - 将项目从组合框添加到表中

将一些组合框绑定到一个集合