Search code examples
c#winformsdatagridviewuser-controlsdatagridviewtextboxcell

Use property of a custom DataGridView Column in a custom EditingControl


I've created my own class MyDataGridViewTextBoxEditingControl and my class MyDataGridViewTextBoxCell. Inside the MyDataGridViewTextBoxEditingControl I need receive some information through properties, but I don't know how to send these values from MyDataGridViewTextBoxCell to MyDataGridViewTextBoxEditingControl, then return value from MyDataGridViewTextBoxEditingControl to MyDataGridViewTextBoxCell. I don't know how to exchange information between these two classes through properties. Any help will be very appreciated.

    public class MyDataGridViewTextBoxCell : DataGridViewTextBoxCell
    {
        public double ExactValue { get; set; }
        public int DecimalPlaces { get; set; }
        public Color ErrorForeColor { get; set; }

        public override Type EditType => typeof(MyDataGridViewTextBoxEditingControl);
    }

    public class MyDataGridViewTextBoxEditingControl : DataGridViewTextBoxEditingControl
    {
        double exactValue = 0;
        int decimalPlaces = 3;
        Color errorForeColor = Color.Black;

        public double ExactValue { get => exactValue; }
        public int DecimalPlaces { get => decimalPlaces; set => decimalPlaces = value; }
        public Color ErrorForeColor { get => errorForeColor; set => errorForeColor = value; }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            //My logic goes here
        }
    }

I understand that the example above can be achieved in other, easier ways, but this is just a simple example of what I need.


Solution

  • You can use the following steps to add a new property to your new column type:

    1. Define the property for your custom cell type.
    2. Define the property for your custom editing control.
    3. Define the property for your custom column type and get/set the value of the property of the CellTemplate of the column.
    4. Override Clone method of the cell and include set you new property value for the clone.
    5. Override InitializeEditingControl method of the cell and set the property for the editing control.

    Example - New property for custom DataGridView column and use it in editing control

    In the following example, I've created a MyProperty to my custom column and have used it in the editing control:

    using System;
    using System.Windows.Forms;
    
    public class MyDataGridViewTextBoxColumn : DataGridViewTextBoxColumn
    {
        public MyDataGridViewTextBoxColumn()
        {
            CellTemplate = new MyDataGridCViewTextBoxCell();
        }
        public string MyProperty
        {
            get => ((MyDataGridCViewTextBoxCell)CellTemplate).MyProperty;
            set => ((MyDataGridCViewTextBoxCell)CellTemplate).MyProperty = value;
        }
    }
    
    public class MyDataGridCViewTextBoxCell : DataGridViewTextBoxCell
    {
        public override Type EditType => typeof(MyDataGridViewTextBoxEditingControl);
        public override void InitializeEditingControl(int rowIndex,
            object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, 
                dataGridViewCellStyle);
            var editingControl = this.DataGridView.EditingControl as 
                MyDataGridViewTextBoxEditingControl;
            if (editingControl != null)
            {
                editingControl.MyProperty = MyProperty;
            }
        }
        public string MyProperty { get; set; }
        public override object Clone()
        {
            var clone = (MyDataGridCViewTextBoxCell)base.Clone();
            clone.MyProperty = this.MyProperty;
            return clone;
        }
    }
    
    public class MyDataGridViewTextBoxEditingControl : DataGridViewTextBoxEditingControl
    {
        public string MyProperty { get; set; }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            //Put the logic here, for example:
            MessageBox.Show(this.MyProperty);
        }
    }
    

    Note: As I mentioned in my answer to your previous question, for cases that you don't need to create a new column type, you can easily handle EditingControlShowing event of the DataGridView and check if the event belongs to your desired column, then get the editing control (and cast it to the right type), and then handle the proper event of the editing control, for example you can take a look at first code block in this example.