Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将绑定组合框添加到datagridview_C#_Mysql_Visual Studio 2010_Datagridview - Fatal编程技术网

C# 将绑定组合框添加到datagridview

C# 将绑定组合框添加到datagridview,c#,mysql,visual-studio-2010,datagridview,C#,Mysql,Visual Studio 2010,Datagridview,这种情况几乎与我们的情况相同。但我不能让它在c#上工作 我有mySql数据库和两个表: 1.协议 2.pcapdata 在protocols表中,我有两个字段:idprotocols和protocolName 在pcaps表中,我有向导协议(它“链接”到idprotocols字段) 我试图得到的是一个包含名称的组合框,它将替换wizardprotocol字段。接下来,如果用户更新“名称”组合框,向导协议将相应地更改(因此我将能够相应地更新数据库中的更改) 现在,在网上阅读了一些信息后:我编写了以

这种情况几乎与我们的情况相同。但我不能让它在c#上工作

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

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

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

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

所以,您需要做的是创建一个bindingsource(我们称之为wizardProtocolBindingSource),然后用protocols表中的数据填充它。这将成为combobox列的数据源

关键代码如下所示:

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

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

我也有同样的问题,你能帮忙吗