将绑定组合框添加到 datagridview [英] Adding bound combobox to datagridview

查看:26
本文介绍了将绑定组合框添加到 datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景与http://arsalantamiz.blogspot.com/2008/09/binding-datagridview-combobox-column.html.但我不能让它在 c# 上工作...

The scenario is almost the same as http://arsalantamiz.blogspot.com/2008/09/binding-datagridview-combobox-column.html. but I can't get it working on c#...

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

I have mySql db with two tables: 1. protocols 2. pcapdata

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

In protocols tables I have a two fields: idprotocols and protocolName

在 pcaps 表中我有 WizardProtocol(它链接"到 idprotocols 字段)

Int the pcaps table I have wizardProtocol (which is "linked" to the idprotocols field)

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

What I'm trying to get is to have a combobox containing names which will replace the wizardprotocol field. Next, If the user updates the "names" combobox the wizardProtocol will be changed accordingly (so I will be able to update the changes in the database accordingly).

现在,在阅读了网上的一些信息后:我编写了以下代码:

Now, after reading some information on the net: I've written the following code:

  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 属性,但失败了(我知道问题可能出在我的数据绑定上,但我无法弄清楚...)

I'm trying to manipulable the DisplayMember property, but I fail (I know that the problem is with probably with my data-binding, but I can't figure it out...)

更新:感谢回答,我重新附上固定代码

UPDATE: Thanks to the answer, I'm re-attaching the fixed code

            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); 

推荐答案

最明显的错误是您对 datagridview 和组合框列使用相同的绑定源.如果您查看您提供的示例,您会注意到它们创建了第二个 bindingsource productBindingSource.

The most obvious thing that you are doing wrong is that you use the same binding source for both your datagridview and for your comboboxcolumn. If you look at the example you provided you'll notice that they create a second bindingsource productBindingSource.

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

So what you need to do is create a bindingsource (let's call it wizardProtocolBindingSource) which you then fill with the data from your protocols table. This becomes the datasource for your combobox column.

关键代码如下所示:

// 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);      

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

The above should work for you, though not knowing the names of your dataset columns I had to guess a little.

这篇关于将绑定组合框添加到 datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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