将数据库数据加载到DataGridView的组合框中 [英] loading DB data in to a combobox in a DataGridView

查看:74
本文介绍了将数据库数据加载到DataGridView的组合框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将DB数据加载到DataGridView的组合框中.

我尝试了以下代码..

how can i load DB data in to a combobox in a DataGridView.

i tried following code..

private void edit_res_per_Load(object sender, EventArgs e)
       {
           mycon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\ETMSetms.accdb");
           string query = "Select resid as ID, resname as Person_Name, resorg as Organisation, orgadd as Office_Address, resdesg as Designation, resdept as Department,resadd as Residential_Address,resph as Phone_no, reseid as Email_Id  From resperson order By resid ";
           da = new OleDbDataAdapter(query, mycon);
           OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
           dt = new DataTable();
           da.Fill(dt);

           dgv1.DataSource = dt;

           DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
           c.HeaderText = "Designation";
           c.DataSource = "resperson";
           c.DisplayMember = "resperson.resdesg";
           c.ValueMember = "resperson.resdesg";
           dgv1.Columns.Insert(4,c);



           mycon.Close();
       }




但是什么都没用...
谁能帮我...




But nothing is working...
Can any one help me......

推荐答案

看看您从SQL查询返回的列名.您在AS子句中输入的名称就是列在数据表中要使用的名称.您的代码应该更像:
Look at the column names you''re returning from your SQL query. The names you''re putting in the AS clauses are the ones the columns are going to have in the datatable. Your code should be more like:
c.DataSource = dt;
c.DisplayMember = "Designation";
c.ValueMember = "Designation"



但是,您没有返回指定列表以供选择,因此您所做的工作毫无意义.



Though, you''re not returning a table of Designations to choose from, so what you''re doing is kind of pointless.


使用以下
c.DataSource = dt;
c.DisplayMember = "Designation";
c.ValueMember = "Designation";


这篇关于将数据库数据加载到DataGridView的组合框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆