Click here to Skip to main content
15,905,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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......
Posted
Updated 14-Jun-11 20:52pm
v3

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.
 
Share this answer
 
Comments
Prerak Patel 16-Jun-11 0:10am    
Have 5. I didn't see those aliases at first.
Use following
c.DataSource = dt;
c.DisplayMember = "Designation";
c.ValueMember = "Designation";
 
Share this answer
 
v2
Comments
Ragi Gopi 15-Jun-11 2:55am    
@Prerak Patel
not working..
Prerak Patel 15-Jun-11 7:59am    
What is the error? While debugging check the data in dt.
Prerak Patel 16-Jun-11 0:09am    
Did you try what Dave said? That should work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900