• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2143
  • Last Modified:

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);
    }
}
0
AntonioRodrigo
Asked:
AntonioRodrigo
1 Solution
 
djon2003Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now