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

C#中取出DatagridView中的值
现在想将datagridview中查询到的内容的某一列添加到combobox的下拉菜单中,该怎么实现?应该是用循环实现吧,我是这样写的:
            int r = 0;
            comboBox5.Items.Clear();
            while (r < this.dataGridView1.RowCount) 
            {
            comboBox5.Items.Add(dataGridView1.Rows[r].Cells[3].Value.ToString());
                r=r+1;
            }
可是运行时报错:“未将对象引用设置到对象的实例”,错误出在哪了?
C# DataGridView

------解决方案--------------------
comboBox5.DataSource = dt;//dt为你从数据库中查询出来的某列的datatable
------解决方案--------------------
你的dataGridView1确定有值?
------解决方案--------------------
楼主你写多一个查询函数就可以了,何必遍历什么dataGridView
//根据你的代码修改
sqlDataAdapter1.SelectCommand.CommandText=("select 某Key,某Value from 某表 where 某条件");
sqlDataAdapter1.Fill(TSGB, "YBGBv");
comboBox5.DataSource = TSGB.Tables[0];
comboBox5.DisplayMember = "某Value";
comboBox5.ValueMember = "某Key";



------解决方案--------------------
查询数据的时候判断一下 如果某项 is null 就取''或0之类的