Click here to Skip to main content
15,890,946 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am dynamically create a gridview in windows application and bind it with some field when i rebind this gridview its again recreate a new gridview in the form.
C#
this.DG1 = new System.Windows.Forms.DataGridView();
            this.cl1 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.cl2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.cl22 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.cl3 = new System.Windows.Forms.DataGridViewComboBoxColumn();
            this.cl4 = new System.Windows.Forms.DataGridViewButtonColumn();
            this.DG1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.DG1.Dock = System.Windows.Forms.DockStyle.Left;
            this.DG1.Location = new System.Drawing.Point(0, 0);
            this.DG1.Name = "DG1";
            this.DG1.Size = new System.Drawing.Size(525, 293);
            this.DG1.TabIndex = 0;
            this.DG1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dg1_cellContentClick);
            this.DG1.CellEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dg1_cellContentEnter);
            this.DG1.CellLeave += new System.Windows.Forms.DataGridViewCellEventHandler(this.dg1_cellcontentLeave);


            //cl1
            this.cl1.HeaderText = "CL1";
            this.cl1.Name = "CL1";
            this.cl1.DataPropertyName = "ID";
            this.cl1.TrueValue = "1";
            this.cl1.FalseValue = "0";
            this.DG1.Columns.Insert(0, cl1);
            //this.DG1.Columns.Add(cl1);
            //cl22
            this.cl22.HeaderText = "Tr_No";
            this.cl22.Name = "Tr_No";
            this.cl22.DataPropertyName = "Tr_No";
            this.DG1.Columns.Add(cl22);
            //cl2
            this.cl2.HeaderText = "CL2";
            this.cl2.Name = "CL2";
            this.cl2.DataPropertyName = "NAME";
            this.DG1.Columns.Add(cl2);

            //cl3
            this.cl3.HeaderText = "CL3";
            this.cl3.Name = "CL3";
            this.cl3.DataPropertyName = "CAT";
            this.cl3.DisplayMember = "Type";
            this.cl3.DataSource = Gettable();//set datasource
            this.DG1.Columns.Add(cl3);
            //cl4
            this.cl4.HeaderText = "Button";
            this.cl4.Name = "Button";
            this.cl4.Text = "EDIT";
            this.cl4.DataPropertyName = "ID";
            this.cl4.UseColumnTextForButtonValue = true;
            this.DG1.Columns.Add(cl4);
            this.Controls.Add(DG1);
Posted
Updated 23-Apr-11 20:05pm
v2

1 solution

Well, yes. It would.

That is exactly what your code does. Creates a new DataGridView:
this.DG1 = new System.Windows.Forms.DataGridView();

And displays it:
this.Controls.Add(DG1);
If you don't want a new one each time, then don't create it and don't display it! Remove the two lines!
 
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