hey all once again i have a little issue ... i'am really having a hard time with this issue
i have a datagridview that contain one column as a text and another column as a combobox i would like to populate the combobox from MS Access Table

and as a simple note i did setup the column in the datagridview using the properties
so.. can any one help me with this ??

Recommended Answers

All 14 Replies

You probably aren't going to receive many responses without posting your code.

Are you manually populating your grid? Or did you specify it in your properties?

I tried the following code :

{
            DataTable GridDT = new DataTable();
            OleDbDataAdapter GridAD = new OleDbDataAdapter("select TYPENAME from PHONETYPES", StrConn);
            GridAD.Fill(GridDT);
            dataGridView1.DataSource = GridDT;  
        }

how ever it seems that it add more column than the one's already there and dosent populate the exisiting one but what i want to do is that i have one of the columns are setted as a combobox in a the datagridview wich i wanted to be populated

// get from DB
DataTable GridDT = new DataTable();
OleDbDataAdapter GridAD = new OleDbDataAdapter("select TYPENAME from PHONETYPES", StrConn);
GridAD.Fill(GridDT); 

// copy tat column values to one list
list<String> l=new List<String>();
l=GridDT..AsEnumerable().select(s=>s.Field<String>("TYPENAME")).ToList();


 // create combobox column and add tat list to that obj
DataGridViewComboBoxColumn cmbCol = new DataGridViewComboBoxColumn();
cmbCol.HeaderText = "TypeName";
cmbCol.DataSource=l;

// add newly customised column to the grid...
dataGridView1.Columns.Add(cmbCol);

well i tried the following and i have relly been working on this for 3 day's now and i came up with this

 {
           OleDbConnection  CONN = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Vendors.accdb;");
            DataGridViewComboBoxColumn PhoneCombo = (DataGridViewComboBoxColumn)PhoneDGV.Rows[0].Cells[2].OwningColumn;         
            string CmbDgvQuery = "select TYPECODE,TYPENAME from PHONETYPES";
            OleDbCommand DBcmd = new OleDbCommand(CmbDgvQuery,CONN);
            CONN.Open();
            OleDbDataReader CMBRdr = DBcmd.ExecuteReader();           
            while (CMBRdr.Read())
            {
                PhoneCombo.Items.Add(CMBRdr["TYPENAME"]);
            }
 }

and it did fill out the combobox box but the issue now is the data is duplicated plus what i want to get a datavalue and did set it up the value member properties in the combobox but it seem that i'm not able to do so.

so can any ony help me with this and please bear in mind that i am setting the up the column's using the propertie of the datagrid view

Try clearing the PhoneCombo combobox before adding the items.PhoneCombo.Items.Clear()

I tried but it seems that it is not working and it clear all of the items in combobox plus i would like to add the value memeber in the combobox and i'm still not sure if the code that i use is realy sufficient so is there any way that we can do it much better ?

Where are you clearing the items? It should be

OleDbDataReader CMBRdr = DBcmd.ExecuteReader();           

//add it here
PhoneCombo.Items.Clear();

while (CMBRdr.Read())
{
commented: that did get the job done +0

Here is a post that fills a combobox from a database. This may work for you.

Fill combobox from database

Cegegier clearin the combo box did actualy work and here is my full code now :

 OleDbConnection  CONN = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Vendors.accdb;");
            DataGridViewComboBoxColumn PhoneCombo = (DataGridViewComboBoxColumn)PhoneDGV.Rows[0].Cells[2].OwningColumn;
            string CmbDgvQuery = "select TYPECODE,TYPENAME from PHONETYPES";         
            OleDbCommand DBcmd = new OleDbCommand(CmbDgvQuery,CONN);
            CONN.Open();
            OleDbDataReader CMBRdr = DBcmd.ExecuteReader();           
            PhoneCombo.Items.Clear();
                while ((CMBRdr.Read()) ) 
            {
                 PhoneCombo.Items.Add(CMBRdr["TYPENAME"]);
            }   

how ever as you can see from my SELECT query i'm retriving two Tables one of them should be the display member and the other should be the value member so
is there is a way can i do this

and it seems that the code is taking some time to run espically that the my table have only 5 rows and is indexed
so any advice regarding that also....?

Probably a database lock issue. Make sure you are closing your connection after using it. Better yet, use a "using" statement.

Also, make sure you don't have the file open in Access. Access is not a DBMS, it is a database file and doesn't support multiple connections--it is single-user. When the database is in use, you will see a ".laccdb" file in the directory. 'l' stands for lock.

using (OleDbConnection  CONN = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Vendors.accdb;"))
{

                       ....

}

On the grids row databound event you can access and run your method that would fill the dropdown. You can even set selected item after its loaded, by using a hidden value in the gridview data.

you guys thank's so much for the effort i did finally did work this issue out and here is the full code

OleDbConnection  CONN = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Vendors.accdb;");
            DataGridViewComboBoxColumn PhoneCombo = (DataGridViewComboBoxColumn)PhoneDGV.Rows[0].Cells[2].OwningColumn;          
            string CmbDgvQuery = "select TYPECODE,TYPENAME from PHONETYPES";        
            OleDbCommand DBcmd = new OleDbCommand(CmbDgvQuery,CONN);
            DataTable phonedt = new DataTable();
            CONN.Open();
            OleDbDataReader CMBRdr = DBcmd.ExecuteReader();
            phonedt.Load(CMBRdr);           
            PhoneCombo.DataSource = phonedt;
            PhoneCombo.DisplayMember = "TYPENAME";
            PhoneCombo.ValueMember = "TYPECODE";
        }

how ever it seems that is still really slow getting the data and i don't know y ?!
but for the issue it is solved
thank you all :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.