dataGridView的DataGridViewComboBoxColumn怎样赋值啊?
都说值无效啊。

解决方案 »

  1.   

      //取得车型代码
                //string mysql = "select patterncode from tabhead where vin='" + comboBox1.SelectedValue.ToString() + "'";
                //string cartype = dbclass.dt(mysql, "mysql").Rows[0][0].ToString();            ////根据取得的车型绑定dataGridView2
                //string s = "select workpos,desno,partname,caozuo,guzhang,paigu,bz from tabzzmb where cartype='" + cartype + "' ";
                //dataGridView2.DataSource = dbclass.dt(s, "s").DefaultView;            //确认你上面的代码没有问题
                //在你上面的数据绑定完毕之后执行如下代码
                //////将你的列添加提取到外面来,不能在循环体内添加,是错误的
                DataGridViewComboBoxColumn dcon = new DataGridViewComboBoxColumn();
                dataGridView1.Columns.Insert(0, dcon);
                //上面的代码已经添加了一个新的combobox列所以我们仅需要修改每一行该列combox的数据源并进行数据绑定
                //下面进入循环
                //由于dataGridView1默认添加了一行空白行在数据表的最后面所以我这里dataGridView1.Rows.Count-1
                for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
                {   
                    //获取你需要绑定的数据源由于我的demo环境和你不相同代码略有出入你自己修改
                    //string s1 = "select providername from tabprovider where desno='" + dataGridView2.Rows[0].Cells["desno"].Value.ToString().Trim() + "'";               
                    //因为我没有你的数据表所以下面这一段是我模拟为每一个combobox构建了一个数据源来做演示
                    //你只需要将你的sql数据查询结果替换下面这段构建数据源的代码就行了
                    DataTable dtCombo = new DataTable();
                    #region 为combobox模拟构建数据源               
                    DataColumn dcCombo = new DataColumn("string");
                    DataColumn dcComboValue = new DataColumn("value");
                    dtCombo.Columns.Add(dcCombo);
                    dtCombo.Columns.Add(dcComboValue);
                    int n=0;
                    foreach (char ca in dataGridView1.Rows[i].Cells[2].Value.ToString())
                    {                    DataRow dr = dtCombo.NewRow();
                        dr[0] = ca.ToString();
                        dr[1] = n;
                        dtCombo.Rows.Add(dr);
                        n++;
                    }
                    #endregion                //将你查询出来的数据源绑定到指定的combobox
                    //(DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]这里我之所以写Cells[0]是因为
                    //我在前面插入列的时候是插入的第一列 dataGridView1.Columns.Insert(0, dcon);
                    //你将代码修改成你的指定列就行了
                    
                    ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).DataSource = dtCombo;
                    ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).DisplayMember = "string";
                    ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).ValueMember = "value";
                    //((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).Value = "1";是设定默认选中项
                    //Value属性的设置值就是你ValueMember绑定列其中的一个值,对应起来的这个你没有问题吧
                    ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).Value = "1";
                    #region 你的代码
                    //dcon.DataSource = dbclass.dt(s1, "s1");
                    //dcon.ValueMember = "providername";
                    //dcon.DisplayMember = "providername";
                    //dcon.HeaderText = "供货厂家";
                    #endregion
                }详见http://topic.csdn.net/u/20080721/13/e672f471-353f-48fa-8489-f429cba436b5.html 
      

  2.   

    DataGridViewComboBoxColumn的item要有内容,并且那个值要在item里才能赋值?