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

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

问题描述

的场景是几乎相同的http:// arsalantamiz .blogspot.com / 2008/09 /绑定的DataGridView-组合框-column.html 。但我不能让它工作在C#...



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



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



内部的pcaps表我有wizardProtocol(这是挂到idprotocols场)



我想要得到的是有包含名称将取代wizardpro​​tocol领域的组合框。接下来,如果用户更新了名称组合框中wizardProtocol也将相应变化(这样我就可以在数据库中更新相应的更改)。



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

 公共无效绑定(参考DataGridView中的dataGridView)
{

{

MySqlDataAdapter的=新MySqlDataAdapter的(SELECT_ALL_PCAP,_con);
的MySqlCommandBuilder =新的MySqlCommandBuilder(MySqlDataAdapter的);

mySqlDataAdapter.UpdateCommand = mySqlCommandBuilder.GetUpdateCommand();
mySqlDataAdapter.DeleteCommand = mySqlCommandBuilder.GetDeleteCommand();
mySqlDataAdapter.InsertCommand = mySqlCommandBuilder.GetInsertCommand();
=数据集新的DataSet();
mySqlDataAdapter.Fill(数据集PCAP);


MySqlDataAdapter的ADP2 =新MySqlDataAdapter的(SELECT_ALL_PROTOCOL,_con);
的MySqlCommandBuilder建设者=新的MySqlCommandBuilder(ADP2);

adp2.UpdateCommand = builder.GetUpdateCommand();
adp2.DeleteCommand = builder.GetDeleteCommand();
adp2.InsertCommand = builder.GetInsertCommand();
adp2.Fill(DataSet中,协议);


的BindingSource =新的BindingSource();
bindingSource.DataSource =数据集;
bindingSource.DataMember =PCAP;
dataGridView.DataSource = BindingSource的;


dataGridView.Columns [长度] =只读真实;
dataGridView.Columns [长度] = DefaultCellStyle.ForeColor System.Drawing.Color.SandyBrown。
dataGridView.AllowUserToAddRows = FALSE;
dataGridView.AllowUserToDeleteRows = FALSE;


DataGridViewComboBoxColumn colType =新DataGridViewComboBoxColumn();
colType.HeaderText =类型;
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);


}
赶上(System.Exception的E)
{
MessageBox.Show(e.ToString());
}
}



我试图操纵DisplayMember属性,但我失败了(我知道这个问题是可能与我的数据绑定,但我不出来...)



更新:感谢答案,我重新连接固定的代码

  MySqlDataAdapter的=新MySqlDataAdapter的(SELECT_ALL_PCAP,_con) ; 
的MySqlCommandBuilder =新的MySqlCommandBuilder(MySqlDataAdapter的);

mySqlDataAdapter.UpdateCommand = mySqlCommandBuilder.GetUpdateCommand();
mySqlDataAdapter.DeleteCommand = mySqlCommandBuilder.GetDeleteCommand();
mySqlDataAdapter.InsertCommand = mySqlCommandBuilder.GetInsertCommand();
=数据集新的DataSet();
mySqlDataAdapter.Fill(数据集PCAP);


MySqlDataAdapter的ADP2 =新MySqlDataAdapter的(SELECT_ALL_PROTOCOL,_con);
的MySqlCommandBuilder建设者=新的MySqlCommandBuilder(ADP2);

adp2.UpdateCommand = builder.GetUpdateCommand();
adp2.DeleteCommand = builder.GetDeleteCommand();
adp2.InsertCommand = builder.GetInsertCommand();
adp2.Fill(DataSet中,协议);



的BindingSource =新的BindingSource();
bindingSource.DataSource =数据集;
bindingSource.DataMember =PCAP;
dataGridView.DataSource = BindingSource的;
dataGridView.AllowUserToAddRows = FALSE;
dataGridView.AllowUserToDeleteRows = FALSE;


DataGridViewComboBoxColumn colType =新DataGridViewComboBoxColumn();
的BindingSource wizardBindingSource =新的BindingSource();
wizardBindingSource.DataSource =数据集;
wizardBindingSource.DataMember =协议;
colType.HeaderText =类型;
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 ),你再与从协议表的数据填写。这将成为您的组合框列中的数据源



关键代码看起来是这样的:

  //您绑定的DataGridView像以前一样
//这个数据应该有idprotocols场哪个是你的外键
//的协议表 - 你可能会想这被隐藏。
的BindingSource =新的BindingSource();
bindingSource.DataSource =数据集;
bindingSource.DataMember =PCAP;
dataGridView.DataSource = BindingSource的;

//隐藏的外键列
dataGridView.Columns [idProtocols]可见=假。

//这里我们填充您的comboboxcolumn绑定源
wizardProtocolBindingSource =新的BindingSource();
//这个数据是从协议表
wizardProtocolBindingSource.DataSource =数据集;

//添加组合框列
DataGridViewComboBoxColumn colType =新DataGridViewComboBoxColumn();
colType.HeaderText =类型;
colType.DropDownWidth = 90;
colType.Width = 90;
colType.DataSource = wizardProtocolBindingSource;
//的DataPropertyName是指在数据源的DataGridView
colType.DataPropertyName =wizardProtocol外键列;
//显示器件在列数据源
colType.DisplayMember =protocolName的名字列;
//值的成员是protols表
colType.ValueMember =idprotocols的主键;
//我通常只是添加列,但如果你需要一个特定的位置
dataGridView.Columns.Add(colType)可以插入;



以上应为你工作,虽然不知道你的数据列的名字,我不得不猜测小。


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#...

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

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

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

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

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

解决方案

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.

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.

The key code looks something like this:

// 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天全站免登陆