Click here to Skip to main content
15,888,610 members

problem in autogenerate column in datagridview

praveenkumar78 asked:

Open original thread
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.
Tags: C#, Windows Forms

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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