datagridview (三)DataGridViewComboBoxColumn 列或者DataGridViewComboBoxCell 赋值绑定数据的方法

多次看见问及datagridcombox列赋值的问题

  1. //取得车型代码
  2.             //string mysql = "select patterncode from tabhead where vin='" + comboBox1.SelectedValue.ToString() + "'";
  3.             //string cartype = dbclass.dt(mysql, "mysql").Rows[0][0].ToString();
  4.             根据取得的车型绑定dataGridView2
  5.             //string s = "select workpos,desno,partname,caozuo,guzhang,paigu,bz from tabzzmb where cartype='" + cartype + "' ";
  6.             //dataGridView2.DataSource = dbclass.dt(s, "s").DefaultView;
  7.             //确认你上面的代码没有问题
  8.             //在你上面的数据绑定完毕之后执行如下代码
  9.             //将你的列添加提取到外面来,不能在循环体内添加,是错误的
  10.             DataGridViewComboBoxColumn dcon = new DataGridViewComboBoxColumn();
  11.             dataGridView1.Columns.Insert(0, dcon);
  12.             //上面的代码已经添加了一个新的combobox列所以我们仅需要修改每一行该列combox的数据源并进行数据绑定
  13.             //下面进入循环
  14.             //由于dataGridView1默认添加了一行空白行在数据表的最后面所以我这里dataGridView1.Rows.Count-1
  15.             for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
  16.             {   
  17.                 //获取你需要绑定的数据源由于我的demo环境和你不相同代码略有出入你自己修改
  18.                 //string s1 = "select providername from tabprovider where desno='" + dataGridView2.Rows[0].Cells["desno"].Value.ToString().Trim() + "'";               
  19.                 //因为我没有你的数据表所以下面这一段是我模拟为每一个combobox构建了一个数据源来做演示
  20.                 //你只需要将你的sql数据查询结果替换下面这段构建数据源的代码就行了
  21.                 DataTable dtCombo = new DataTable();
  22.                 #region 为combobox模拟构建数据源               
  23.                 DataColumn dcCombo = new DataColumn("string");
  24.                 DataColumn dcComboValue = new DataColumn("value");
  25.                 dtCombo.Columns.Add(dcCombo);
  26.                 dtCombo.Columns.Add(dcComboValue);
  27.                 int n=0;
  28.                 foreach (char ca in dataGridView1.Rows[i].Cells[2].Value.ToString())
  29.                 {
  30.                     DataRow dr = dtCombo.NewRow();
  31.                     dr[0] = ca.ToString();
  32.                     dr[1] = n;
  33.                     dtCombo.Rows.Add(dr);
  34.                     n++;
  35.                 }
  36.                 #endregion
  37.                 //将你查询出来的数据源绑定到指定的combobox
  38.                 //(DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]这里我之所以写Cells[0]是因为
  39.                 //我在前面插入列的时候是插入的第一列 dataGridView1.Columns.Insert(0, dcon);
  40.                 //你将代码修改成你的指定列就行了
  41.                 
  42.                 ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).DataSource = dtCombo;
  43.                 ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).DisplayMember = "string";
  44.                 ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).ValueMember = "value";
  45.                 //((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).Value = "1";是设定默认选中项
  46.                 //Value属性的设置值就是你ValueMember绑定列其中的一个值,对应起来的这个你没有问题吧
  47.                 ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).Value = "1";
  48.                 #region 你的代码
  49.                 //dcon.DataSource = dbclass.dt(s1, "s1");
  50.                 //dcon.ValueMember = "providername";
  51.                 //dcon.DisplayMember = "providername";
  52.                 //dcon.HeaderText = "供货厂家";
  53.                 #endregion
  54.             }
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值