Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everybody.
I developed Multi-column ComboBox and I used the first item in it to use it as a button to add a new item to the ComboBox as needed, I named the linked property NewItemButtonCaption. Till now everything is okay, after that I used that Multi-column CoboBox to create custom DataGridView Column and I cloned the new property in the ComboBoxCell and ComboBoxColumn.

The problem is that when I create the Column in a DataGridView the NewItemButtonCaption doesn't work.


What I have tried:

namespace Custom_DataGridColumn
{
    public class MCCoboBoxCell : DataGridViewComboBoxCell
    {
        public override Type EditType => typeof(MCCBEditingControl);

        public override Type ValueType => base.ValueType;

        public MCCoboBoxCell() : base()
        {
        }

        public override void InitializeEditingControl(
            int rowIndex, object initialFormattedValue,
            DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(
                rowIndex, initialFormattedValue, dataGridViewCellStyle);

            MCCBEditingControl ctl =
                DataGridView.EditingControl as MCCBEditingControl;

            ctl.NewButtonText = NewItemButtonCaption;
            ctl.DropDownStyle = ComboBoxStyle.DropDown;
        }

        public override object DefaultNewRowValue =>
            base.DefaultNewRowValue;

        private string _NewButtonText = string.Empty;


        [NotifyParentProperty(true)]
        public string NewItemButtonCaption
        {
            get
            {
               return _NewButtonText;
            }
            set
            {
                _NewButtonText = value;
            }
        }
 
        public override object Clone()
        {
            var clone = (MCCoboBoxCell)base.Clone();
            clone.NewItemButtonCaption = NewItemButtonCaption;

            return clone;
        }
    }
}

namespace Custom_DataGridColumn
{
    public class MultiColumnComboBox : DataGridViewComboBoxColumn
    {
        MCCoboBoxCell MCCoboBoxCell;
        public MultiColumnComboBox()
        {
            if (MCCoboBoxCell == null)
            {
                MCCoboBoxCell = new MCCoboBoxCell();
                this.CellTemplate = MCCoboBoxCell;
            }
        }

        private string _NewButtonText = string.Empty;

        [NotifyParentProperty(true)]
        public string NewItemButtonCaption
        {
            set
            {
                _NewButtonText = value;
            }
            get
            {
                return _NewButtonText;
            }
        }
       
        public override object Clone()
        {
            var clone = (MCCoboBoxCell)base.CellTemplate;
            clone.NewItemButtonCaption = NewItemButtonCaption;

            return base.Clone();
        }

    }
}
 class Form1 : Form
    {
        private DataGridView dataGridView1;
        private Custom_DataGridColumn.MultiColumnComboBox Column2;
        DataTable dataTable = new DataTable("Accounts");

        public Form1()
        {
            InitializeComponent();

            #region DataTable Info   
        }
       
        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.Column2 = new Custom_DataGridColumn.MultiColumnComboBox();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = 
            this.dataGridView1.Columns.AddRange(new 
 System.Windows.Forms.DataGridViewColumn[] {
            this.Column2});
...
            // 
            // Column2
            // 
            this.Column2.HeaderText = "Column2";
            this.Column2.Name = "Column2";
            this.Column2.NewItemButtonCaption = "Add New Item";\\Not working
            // 
            // Form1
            // 
       ...

        }
    }
Posted
Updated 20-Oct-19 9:22am

 
Share this answer
 
I solved it, and this is the full solution:

[ToolboxItem(false)]
 public class MCCBEditingControl : MultiColumnsComboBox, IDataGridViewEditingControl
 {
     private DataGridView _DataGridView;

     public MCCBEditingControl()
     {
     }
     public DataGridView EditingControlDataGridView
     {
         get { return _DataGridView; }
         set { _DataGridView = value; }
     }

     private int rowIndex;
     public int EditingControlRowIndex
     {
         get { return rowIndex; }
         set { rowIndex = value; }
     }

     protected override void OnSelectedValueChanged(EventArgs e)
     {
         try
         {
             EditingControlValueChanged = true;
             this.EditingControlDataGridView.
                 NotifyCurrentCellDirty(true);
             base.OnSelectedValueChanged(e);
         }
         catch (Exception)
         {

             throw;
         }
     }

     public object EditingControlFormattedValue
     {
         get
         {
             if (this.SelectedIndex > 0)
             {
                 return this.Text.ToString();
             }
             else if (this.SelectedIndex == 0)
             {
                 //Show MessageBox Option to add new item
                 return string.Empty;
             }
             else
             {
                 return string.Empty;
             }
         }
         set
         {
             string newValue = value as string;
             if (newValue != null)
                 this.Text = newValue;
         }
     }

     private bool valueChanged = true;
     public bool EditingControlValueChanged
     { get => valueChanged; set => valueChanged = value; }

     public Cursor EditingPanelCursor =>
         base.Cursor;

     public bool RepositionEditingControlOnValueChange => false;


     public void ApplyCellStyleToEditingControl(
         DataGridViewCellStyle dataGridViewCellStyle)
     {
         this.Font = dataGridViewCellStyle.Font;
         this.ForeColor = dataGridViewCellStyle.ForeColor;
         this.BackColor = dataGridViewCellStyle.BackColor;
     }

     public bool EditingControlWantsInputKey(
         Keys keyData, bool dataGridViewWantsInputKey)
     {
         // Let the DateTimePicker handle the keys listed.
         switch (keyData & Keys.KeyCode)
         {
             case Keys.Left:
             case Keys.Up:
             case Keys.Down:
             case Keys.Right:
             case Keys.Home:
             case Keys.End:
             case Keys.PageDown:
             case Keys.PageUp:
             case Keys.Enter:
                 return true;
             default:
                 return false;
         }
     }

     public object GetEditingControlFormattedValue(
         DataGridViewDataErrorContexts context)
     {
         return EditingControlFormattedValue;
     }

     public void PrepareEditingControlForEdit(bool selectAll)
     {

     }

     protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
     {
         if (keyData == Keys.Tab || keyData == Keys.Enter)
         {
             this.DroppedDown = false;

             if (this.SelectedIndex == 0)
                 return true;
         }
         return base.ProcessCmdKey(ref msg, keyData);
     }


 }

public class MCCoboBoxCell : DataGridViewComboBoxCell
{
    public override Type EditType => typeof(MCCBEditingControl);

    public override Type ValueType => base.ValueType;

    public MCCoboBoxCell() : base()
    {
    }

    public override void InitializeEditingControl(
        int rowIndex, object initialFormattedValue,
        DataGridViewCellStyle dataGridViewCellStyle)
    {
        base.InitializeEditingControl(
            rowIndex, initialFormattedValue, dataGridViewCellStyle);

        MCCBEditingControl ctl=
            DataGridView.EditingControl as MCCBEditingControl;

        ctl.ShowAddNew = true;
        ctl.NewButtonText = NewItemButtonCaption;
        ctl.DropDownStyle = ComboBoxStyle.DropDown;
    }

    public override object DefaultNewRowValue =>
        base.DefaultNewRowValue;

    private string _NewButtonText ="Add New";

    public string NewItemButtonCaption
    {
        get
        {
           return _NewButtonText;
        }
        set
        {
            _NewButtonText = value;
        }
    }
    public static List<MCCoboBoxCell> mCCoboBoxCells = new List<MCCoboBoxCell>();
    public override object Clone()
    {
        var clone = (MCCoboBoxCell)base.Clone();
        clone.NewItemButtonCaption = this.NewItemButtonCaption;
        mCCoboBoxCells.Add(clone);
        return clone;
    }

}

public class MultiColumnComboBox : DataGridViewComboBoxColumn
{
    public MultiColumnComboBox()
    {
        this.CellTemplate = new MCCoboBoxCell();
    }

    private string _NewButtonText = "Add New Account";

    [NotifyParentProperty(true)]
    public string NewItemButtonCaption
    {
        set
        {
            _NewButtonText = value;
            Clone();
            foreach (MCCoboBoxCell cell in MCCoboBoxCell.mCCoboBoxCells)
            {
                cell.NewItemButtonCaption = this.NewItemButtonCaption;
            }
        }
        get
        {
            return _NewButtonText;
        }
    }
    public static List<MCCoboBoxCell> mCCoboBoxCells = new List<MCCoboBoxCell>();
    public override object Clone()
    {
        var clone = (MCCoboBoxCell)base.CellTemplate;
        clone.NewItemButtonCaption = _NewButtonText;
        mCCoboBoxCells.Add(clone);
        return base.Clone();
    }

}
 
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