Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

CustomDataGridView Produces Duplicate Columns in Runtime

$
0
0

Hi there,

Problem in Brief:

I have an CustomGridView, a type of DataGridView. The below code is attached to it. When I drop the datagridview in design time I can see the columns are all in proper but except for the name of the datagridviewcolumn names are different and is not what I gave.

In runtime the Columns are duplicated. Could not figure out why.

Code

public class CustomAccountDataGridView : DataGridView
    {
        private bool _paddingOn = true;

        public bool PaddingOn
        {
            get { return _paddingOn; }
            set { _paddingOn = value; }
        }

        public CustomAccountDataGridView()
        {
            this.AutoGenerateColumns = false;
            this.Columns.Clear();

            DataGridViewTextBoxColumn columnAccountID = new DataGridViewTextBoxColumn();
            columnAccountID.DataPropertyName = "ACCOUNTID";
            columnAccountID.Name = "colACCOUNTID";
            columnAccountID.HeaderText = "A/c";
            columnAccountID.Width = 110;
            columnAccountID.Visible = false;
            this.Columns.Add(columnAccountID);

            DataGridViewTextBoxColumn columnClientAccountID = new DataGridViewTextBoxColumn();
            columnClientAccountID.DataPropertyName = "CLIENTACCOUNTID";
            columnClientAccountID.Name = "colCLIENTACCOUNTID";
            columnClientAccountID.HeaderText = "Client A/c";
            columnClientAccountID.Width = 110;
            this.Columns.Add(columnClientAccountID);

            DataGridViewTextBoxColumn columnParticipantID = new DataGridViewTextBoxColumn();
            columnParticipantID.DataPropertyName = "PARTICIPANTID";
            columnParticipantID.Name = "colPARTICIPANTID";
            columnParticipantID.HeaderText = "Participant";
            columnParticipantID.Width = 60;
            this.Columns.Add(columnParticipantID);

            DataGridViewTextBoxColumn columnAccountName = new DataGridViewTextBoxColumn();
            columnAccountName.Name = "colACCOUNTNAME";
            columnAccountName.HeaderText = "A/c Name";
            columnAccountName.Width = 200;
            columnAccountName.ReadOnly = true;
            this.Columns.Add(columnAccountName);

            DataGridViewTextBoxColumn columnXsactValue = new DataGridViewTextBoxColumn();
            columnXsactValue.DataPropertyName = "XSACTVALUE";
            columnXsactValue.Name = "colXSACTVALUE";
            columnXsactValue.Width = 110;
            columnXsactValue.HeaderText = "Value";
            columnXsactValue.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            columnXsactValue.DefaultCellStyle.Format = "N2";
            this.Columns.Add(columnXsactValue);
        }

        protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
        {
            string sClientID = string.Empty;

            foreach (DataGridViewRow dvr in (this.Rows))
            {
                for (int i = 0; i < dvr.Cells.Count; i++)
                {
                    if (this.Columns[i].DataPropertyName == "ACCOUNTID")
                    {
                        if (dvr.Cells["colACCOUNTID"].Value != null)
                        {
                            sClientID = dvr.Cells["colACCOUNTID"].Value.ToString();
                            dvr.Cells["colACCOUNTNAME"].Value = sClientID; //utility.ClientExtention.getAccountName(sClientID);

                            break;
                        }
                    }
                }
            }

            base.OnDataBindingComplete(e);
        }

        protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control.GetType() == typeof(DataGridViewTextBoxEditingControl))
            {
                (e.Control as TextBox).CharacterCasing = CharacterCasing.Upper;
            }

            base.OnEditingControlShowing(e);
        }

        protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
        {
            string sClientAccountID = string.Empty;
            string sParticipantID = "JKB";

            if (e.RowIndex >= 0)
            {
                if (this.Columns[e.ColumnIndex].DataPropertyName == "CLIENTACCOUNTID")
                {
                    if (this[e.ColumnIndex, e.RowIndex].Value != null)
                    {
                        if (this[e.ColumnIndex, e.RowIndex].Value.ToString().Length != 0)
                        {
                            if (this.PaddingOn)
                            {
                                sClientAccountID = this[e.ColumnIndex, e.RowIndex].Value.ToString().ToUpper().PadLeft(13, '0');
                            }
                            else
                            {
                                sClientAccountID = this[e.ColumnIndex, e.RowIndex].Value.ToString().ToUpper();
                            }

                            this[e.ColumnIndex, e.RowIndex].Value = sClientAccountID;
                        }
                    }
                }

                if (this.Columns[e.ColumnIndex].DataPropertyName == "PARTICIPANTID")
                {
                    if (this[e.ColumnIndex, e.RowIndex].Value != null)
                    {
                        if (this[e.ColumnIndex, e.RowIndex].Value.ToString().Length != 0)
                        {

                            sParticipantID = this[e.ColumnIndex, e.RowIndex].Value.ToString().ToUpper();
                        }

                        this[e.ColumnIndex, e.RowIndex].Value = sParticipantID;
                    }
                }

                if (sClientAccountID != string.Empty & sParticipantID != string.Empty)
                {
                    this["ACCOUNTNAME", e.RowIndex].Value = (string.Format("{0}-{1}", sParticipantID, sClientAccountID));
                }

            }

            base.OnCellValueChanged(e);
        }


    }


Hifni Shahzard Nazeer M.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>