columns appearing multiple times in datagridview

rahul kumar 605 Reputation points
2023-06-18T12:22:36.54+00:00

I have created a CustomDatagridview by the following

   public partial class Paymentdatagridview : DataGridView
        {
            cusList = new Dictionary<string, string>();
            cusList.Add("deepak(market)", "21.00");
            cusList.Add("xxx(market)", "10.00");
            // i have set enableheadervisualstyles to false 
            // make bold the headers
            // Add the columns to your custom DataGridView
            DataGridViewColumn column1 = new DataGridViewTextBoxColumn();
            column1.Name = "Column1";
            column1.HeaderText = "Particulars";
            column1.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            column1.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Columns.Add(column1);

            DataGridViewColumn column2 = new DataGridViewTextBoxColumn();
            column2.Name = "Column2";
            column2.HeaderText = "Amount";
            column2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            column2.SortMode = DataGridViewColumnSortMode.Programmatic;
            this.Columns.Add(column2);

            this.RowHeadersVisible = false;
            this.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 9.75F, FontStyle.Bold);
            this.CellBorderStyle = DataGridViewCellBorderStyle.None;
            this.AllowUserToAddRows = false;
            InitializeComponent();
        }

and used it in my form named by drag and drop ( after clean solution and Rebuilt solution , then after closing the file and opening again ) but i found out that the column names are appearing twice . I tried draging the control again from the toolbox but the same problem happens to the other customdatagridview as well . below is the codebehind and designer file

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using datagridviewusingchatgpt.Firstlevelmaster;

namespace datagridviewusingchatgpt.SecondLevelMaster
{
    public partial class paymentreceiptmasterform : mainmaster
    {
        public paymentreceiptmasterform()
        {
            InitializeComponent();
        }

        private void paymentreceiptmasterform_Load(object sender, EventArgs e)
        {

        }
    }
}

namespace datagridviewusingchatgpt.SecondLevelMaster
{
    partial class paymentreceiptmasterform
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.customtextbox1 = new datagridviewusingchatgpt.customtextbox();
            this.paymentdatagridview1 = new datagridviewusingchatgpt.Paymentdatagridview();
            this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.paymentdatagridview2 = new datagridviewusingchatgpt.Paymentdatagridview();
            this.dataGridViewTextBoxColumn3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.paymentdatagridview1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.paymentdatagridview2)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(215)))), ((int)(((byte)(37)))), ((int)(((byte)(81)))));
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            this.label1.Location = new System.Drawing.Point(1, 13);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(158, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "Payment";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(165, 13);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(100, 23);
            this.label2.TabIndex = 1;
            this.label2.Text = "No .";
            // 
            // customtextbox1
            // 
            this.customtextbox1.BackColor = System.Drawing.Color.LavenderBlush;
            this.customtextbox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.customtextbox1.DisableFeatures = false;
            this.customtextbox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.customtextbox1.ForeColor = System.Drawing.Color.Gold;
            this.customtextbox1.Location = new System.Drawing.Point(208, 13);
            this.customtextbox1.Name = "customtextbox1";
            this.customtextbox1.Size = new System.Drawing.Size(167, 22);
            this.customtextbox1.TabIndex = 2;
            // 
            // paymentdatagridview1
            // 
            this.paymentdatagridview1.AllowUserToAddRows = false;
            this.paymentdatagridview1.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
            dataGridViewCellStyle1.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold);
            dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
            dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            this.paymentdatagridview1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
            this.paymentdatagridview1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.paymentdatagridview1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.dataGridViewTextBoxColumn1,
            this.dataGridViewTextBoxColumn2});
            this.paymentdatagridview1.Location = new System.Drawing.Point(5, 111);
            this.paymentdatagridview1.Name = "paymentdatagridview1";
            this.paymentdatagridview1.RowHeadersVisible = false;
            this.paymentdatagridview1.RowHeadersWidth = 51;
            this.paymentdatagridview1.RowTemplate.Height = 24;
            this.paymentdatagridview1.Size = new System.Drawing.Size(797, 86);
            this.paymentdatagridview1.TabIndex = 3;
            // 
            // dataGridViewTextBoxColumn1
            // 
            this.dataGridViewTextBoxColumn1.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
            this.dataGridViewTextBoxColumn1.HeaderText = "Particulars";
            this.dataGridViewTextBoxColumn1.MinimumWidth = 6;
            this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
            this.dataGridViewTextBoxColumn1.Width = 130;
            // 
            // dataGridViewTextBoxColumn2
            // 
            this.dataGridViewTextBoxColumn2.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridViewTextBoxColumn2.HeaderText = "Amount";
            this.dataGridViewTextBoxColumn2.MinimumWidth = 6;
            this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
            this.dataGridViewTextBoxColumn2.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Programmatic;
            // 
            // paymentdatagridview2
            // 
            this.paymentdatagridview2.AllowUserToAddRows = false;
            this.paymentdatagridview2.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Control;
            dataGridViewCellStyle2.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold);
            dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.WindowText;
            dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            this.paymentdatagridview2.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
            this.paymentdatagridview2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.paymentdatagridview2.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.dataGridViewTextBoxColumn3,
            this.dataGridViewTextBoxColumn4});
            this.paymentdatagridview2.Location = new System.Drawing.Point(13, 228);
            this.paymentdatagridview2.Name = "paymentdatagridview2";
            this.paymentdatagridview2.RowHeadersVisible = false;
            this.paymentdatagridview2.RowHeadersWidth = 51;
            this.paymentdatagridview2.RowTemplate.Height = 24;
            this.paymentdatagridview2.Size = new System.Drawing.Size(681, 198);
            this.paymentdatagridview2.TabIndex = 4;
            // 
            // dataGridViewTextBoxColumn3
            // 
            this.dataGridViewTextBoxColumn3.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
            this.dataGridViewTextBoxColumn3.HeaderText = "Particulars";
            this.dataGridViewTextBoxColumn3.MinimumWidth = 6;
            this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3";
            this.dataGridViewTextBoxColumn3.Width = 130;
            // 
            // dataGridViewTextBoxColumn4
            // 
            this.dataGridViewTextBoxColumn4.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridViewTextBoxColumn4.HeaderText = "Amount";
            this.dataGridViewTextBoxColumn4.MinimumWidth = 6;
            this.dataGridViewTextBoxColumn4.Name = "dataGridViewTextBoxColumn4";
            this.dataGridViewTextBoxColumn4.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Programmatic;
            // 
            // paymentreceiptmasterform
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(217)))));
            this.ClientSize = new System.Drawing.Size(803, 450);
            this.Controls.Add(this.paymentdatagridview2);
            this.Controls.Add(this.paymentdatagridview1);
            this.Controls.Add(this.customtextbox1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "paymentreceiptmasterform";
            this.Text = "paymentreceiptmasterform";
            this.Load += new System.EventHandler(this.paymentreceiptmasterform_Load);
            ((System.ComponentModel.ISupportInitialize)(this.paymentdatagridview1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.paymentdatagridview2)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private customtextbox customtextbox1;
        private Paymentdatagridview paymentdatagridview1;
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
        private Paymentdatagridview paymentdatagridview2;
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;
    }
}

Can Someone please found out the bug from side which is causing this .

Developer technologies | Windows Forms
Developer technologies | C#
{count} votes

Accepted answer
  1. Anonymous
    2023-06-19T06:26:07.9166667+00:00

    Hi @Rahul Kumar , Welcome to Microsoft Q&A.

    After careful testing, Designer will add column again when dragging into datagridview custom control.

    The current emergency solution is: manually delete this.paymentdatagridview.Columns.AddRange in the designer code add page.

    In testing, this problem occurs with both .Net and Framework.

    I'll keep researching to see if it's a bug.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.