Click here to Skip to main content
15,947,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
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


C#
    try
    {
        SqlConnection con = new SqlConnection("data source = localhost\\SQLEXPRESS;initial catalog = mgrsch_schools; integrated security = true");
        con.Open();
        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.Columns.Add(cmb);

        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.Columns.Add(comb1);
        dataGridView1.DataSource = ds.Tables[0];
        dataGridView1.AutoGenerateColumns = false;

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }

}


i write this code in comboxselected indexchange event.

if anyone have any idea helpme.
Posted
Updated 29-Mar-12 1:38am
v2
Comments
[no name] 29-Mar-12 7:38am    
<pre> tag added.

1 solution

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

C#
dataGridView1.Columns.Add(comb1);
                dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Databind();
                dataGridView1.AutoGenerateColumns = false;
 
Share this answer
 

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