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);
    }
}
AntonioRodrigoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

djon2003Senior software engineerCommented:
Using the EditingControlShowing event of the DataGridView would allow you to get access to the instance of your custom custom using the eventArg.Control.

From there, you can set the DataSource to it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.