Avatar of AntonioRodrigo
AntonioRodrigo

asked on 

Custom DataGridView column ComboBox - pass parameters to function

Hello,

I am using Windows Forms.
I've created a custom DataGridView column - class is called ComboBoxColumn. When I edit cell content, the ComboBox appears in that cell. I am doing this because ComboBox is much faster than the DataGridViewComboBoxColumn. My problem is, that I can't pass any parameters to my combo box - I would like to pass DataSource as parameter. ComboBox is in the class ComboBoxEditingControl. Is is called automatically from the class ComboBoxCell - instance of ComboBoxEditingControl is nowhere created. If I want to add my custom column to the DataGridView, I start with creating the ComboBoxColumn - everything else is automatic. I've redesigned this code from that example:

http://msdn.microsoft.com/en-us/library/7tas5c80.aspx


(it's DateTimePicker inside DataGridView). So, any idea how to pass parameters to class ComboBoxEditingControl?

Here are all my three classes:

public class ComboBoxColumn : DataGridViewColumn
{
    public ComboBoxColumn() : base(new ComboBoxCell())
    {
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {

            // Ensure that the cell used for the template is a ComboBoxCell.
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(ComboBoxCell)))
            {
                throw new InvalidCastException("Must be a ComboBoxCell");
            }
           
            base.CellTemplate = value;
        }
    }
}




public class ComboBoxCell : DataGridViewTextBoxCell
{    
    public ComboBoxCell(): base()
    {
       
    }

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


       
    }    

    public override Type EditType
    {
        get
        {
            // Return the type of the editing control that CalendarCell uses.
            return typeof(ComboBoxEditingControl);            
        }

       
    }

   

    public override Type ValueType
    {
        get
        {
            return typeof(string);
        }
    }
}


public class ComboBoxEditingControl : ComboBox, IDataGridViewEditingControl
{    
    DataGridView dataGridView;
    private bool valueChanged = false;
    int rowIndex;


    public ComboBoxEditingControl()
    {
       
    }

    public object EditingControlFormattedValue
    {
        get
        {
            return this.ValueMember.ToString();
        }
        set
        {
            this.ValueMember = value.ToString();
        }
    }

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


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

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

    public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
    {
        // Let the ComboBox 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:
                return true;
            default:
                return !dataGridViewWantsInputKey;
        }
    }

    public void PrepareEditingControlForEdit(bool selectAll)
    {

    }

    public bool RepositionEditingControlOnValueChange
    {
        get
        {
            return false;
        }
    }

    public DataGridView EditingControlDataGridView
    {
        get
        {
            return dataGridView;
        }
        set
        {
            dataGridView = value;
        }
    }

    public bool EditingControlValueChanged
    {
        get
        {
            return valueChanged;
        }
        set
        {
            valueChanged = value;
        }
    }

    public Cursor EditingPanelCursor
    {
        get { return base.Cursor; }
    }

    protected override void OnValueMemberChanged(EventArgs e)
    {
        valueChanged = true;
        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        base.OnValueMemberChanged(e);
    }
}
C#Visual Basic.NET

Avatar of undefined
Last Comment
djon2003
ASKER CERTIFIED SOLUTION
Avatar of djon2003
djon2003
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo