?
Solved

Custom DataGridView column ComboBox - pass parameters to function

Posted on 2011-10-04
1
Medium Priority
?
2,065 Views
Last Modified: 2012-05-12
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
Comment
Question by:AntonioRodrigo
1 Comment
 
LVL 9

Accepted Solution

by:
djon2003 earned 2000 total points
ID: 37048471
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question