Click here to Skip to main content
14,239,866 members
Rate this:
Please Sign up or sign in to vote.
See more:
i am using combobox to bind the datagridview and i am created a comboboxcolumn
in datagridview .everytime if i selected different value from combox it will
recreate the another new comboboxcolumn. i set autogeneratecolumn=false. stillnot working and my code is

        SqlConnection con = new SqlConnection("data source = localhost\\SQLEXPRESS;initial catalog = mgrsch_schools; integrated security = true");
        string s = "select * from hmloading1 ";
        string s2 = "select *from teacherdetailloading where sub1='"+comboBox1.Text+"' ";

        DataSet ds = new DataSet();
        SqlDataAdapter dd = new SqlDataAdapter(s, con);
        SqlDataAdapter dd1 = new SqlDataAdapter(s2, con);
        SqlCommandBuilder cmb1 = new SqlCommandBuilder(dd);
        SqlCommandBuilder cmb2 = new SqlCommandBuilder(dd1);
        dd.Fill(ds, "hmloading1");
        dd1.Fill(ds, "teacherdetailloading");
        SqlCommand cmd = new SqlCommand(s, con);

        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.AutoGenerateColumns = false;
            //panel1.Visible = true;
            this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            dataGridView1.Columns["slno"].Visible = false;

        DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
        cmb.HeaderText = "Prekg";
        cmb.Name = "cmb";
        cmb.DataSource = ds.Tables["teacherdetailloading"];
        cmb.DisplayMember = "name";
        cmb.ValueMember = "name";

        dataGridView1.DataSource = ds.Tables[0];
        dataGridView1.AutoGenerateColumns = false;
        DataGridViewComboBoxColumn comb1 = new DataGridViewComboBoxColumn();
        comb1.HeaderText = "Lkg";
        comb1.Name = "comb1";
        comb1.DataSource = ds.Tables["teacherdetailloading"];
        comb1.DisplayMember = "name";
        comb1.ValueMember = "name";
        dataGridView1.DataSource = ds.Tables[0];
        dataGridView1.AutoGenerateColumns = false;

    catch (Exception ex)


i write this code in comboxselected indexchange event.

if anyone have any idea helpme.
Updated 29-Mar-12 1:38am
Peace ON 29-Mar-12 7:38am
<pre> tag added.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You are missing the Databind() in the last three lines of code

                dataGridView1.DataSource = ds.Tables[0];
                dataGridView1.AutoGenerateColumns = false;

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100