Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a datagridview..where there are seven columns ..in which two of them r combo cell ...and others are text cell...i have been asked to populate the grid from an accdb table ..but i cannot do so ...kindly help me out
Posted

1 solution

You can code something like following to retrive data from accdb and binding datagridview.

C#
string connectionString= @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\YourDB.accdb";
string sql = "SELECT * FROM TableName";
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand(sql, con);
con.Open();
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt= new DataTable();
da.Fill(dt);
YourDataGridviewId.DataSource = dt;


Now for adding values to comboboxcells you can write something like-
C#
DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)myDataGrid.Rows[0].Cells[0];
cbCell.DataSource = dataSourceForCombobox; //get the datasource for combobox prior to this
cbCell.ValueMember = "ValueField";
cbCell.DisplayMember = "DisplayField";

You can make use of OnItemDataBound, to apply this for all the rows.

Hope, it helps :)
 
Share this answer
 
Comments
code1212 28-Jul-15 4:25am    
I have a grid where two of the cells r combo and other 5 as text columns .... when the form loads ..it should load all the values from my table ...
Means eaxh tuple in my grid should follow the tuples in my accdb ...

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