日期:2014-05-17  浏览次数:20815 次

C# winform 中DataGridView中的DataGridViewComboBoxColumn
很疑惑,百度了下,越看越不懂,有的人说用Combobox直接添加进DataGridView中,我不需要这么高端,我只需要自带的控件添加进去,可是我没搞定,希望大侠给点指点
首先
1.拖控件。。。DataGridView、GroupBox等等等,此处省略500字。。。
2.根据数据库的字段来编辑字段,因为我知道里面会包含很多控件,单单的Select语句出来的数据源已经无法阻止我。

3.界面搭建好了,去后台代码连接数据库取数据源

        private void Bind2(int Areacode2)
        {
            SqlConnection scConnection = new SqlConnection(connstr); //建立Connection
            SqlCommand scCommand2 = scConnection.CreateCommand(); //建立Command
            scCommand2.CommandText = "select Id,Shipdate,Shipcompany,Company,Portship,Shipday,[20],[40],HQ,Dodate,Endday,Tax,Addcharge,Mark,Areacode1,Areacode2 from zheng where Areacode1=@Areacode1 and Areacode2=@Areacode2";
            scCommand2.Parameters.AddWithValue("@Areacode1", area);
            scCommand2.Parameters.AddWithValue("@Areacode2", Areacode2);
            sdaAdapter2 = new SqlDataAdapter(scCommand2); //建立Adapter
            scbBuilder2 = new SqlCommandBuilder(sdaAdapter2); //该对象负责生成用于更新数据库的SQL语句,不必自己创建这些语句
            dsSet.Clear(); //使用容器前先清空容器
            sdaAdapter2.Fill(dsSet, "zheng"); //填充数据
            bind2 = this.BindingContext[dsSet.Tables["zheng"]];
            dataGridView2.DataSource = dsSet.Tables["zheng"]; //数据源绑定      
        }

自己写了个方法取出DataTable数据源并且绑定到DataGridView 到这里全部正确。

然后问题来了,我绑定DataGridViewTextBoxColumn 的字段都字段名和 DataSouce名字一致,
但是DataGridViewComboBoxColumn 我在图形视图没有设置,导致DataGridView里多出字段来。。。
我需要的是数据库 SELECT出来的字段正好是我DataGridView里的字段,而DataGridViewTextBoxColumn这列显示的也正好是我数据库里的查出来的值,当我鼠标点击下拉框的时候,DataGridViewTextBoxColumn 该列再出现一个数据源,是下拉数据源,比如说 
{{value:男,index:0},{value:女,index:1},{value:未知,index:3}}
怎么做,也就是有两数据源的概念 
这个是数据库查出来的,那么这个 Shipdate这列就应该是第一行是男,第二行是女,当我点击这列时候再出现 “男 女 未知”这个数据源给我选