views:

21

answers:

0

I have a DataGridView with a DataGridViewComboBoxCell. In the gv.EditingControlShowing handler, I am setting the DataSource of the DataGridViewComboBoxCell. I also have the gv.CellValidating event wired to a handler. When I check the DataSource of the DataGridViewComboBoxCell, it is different than what I set it to in the CellValidating event. Why? this is driving me nuts!

EDIT: Ok, I picked up a big part of my mistake. Apparently a DataGridViewComboBoxEditingControl.DataSource is not the same as a DataGridViewComboBoxCell.DataSource. I don't understand what the difference is between the two. Here is some relevant sample code I whipped up in Linqpad to demonstrate.

void Main()
{
    Application.Run(new Form1());
}

public partial class Form1 : Form
{
    public Form1()
    {
        var gv = new DataGridView();
        gv.Columns.Add("col1","col1");
        gv.Rows.Add();
        var cb = new DataGridViewComboBoxCell();
        gv.Rows[0].Cells[0] = cb;

        gv.EditingControlShowing += (sender, args) => 
        {
            var cbCell = args.Control as DataGridViewComboBoxEditingControl;
            cbCell.DropDownStyle = ComboBoxStyle.DropDown;

            //try flipping the order of these two statements
            cb.DataSource = new string[] {"Cell","DataSource"};
            cbCell.DataSource = new string[] {"Editing","Control","DataSource"};
        };

        gv.CellValidating += (sender, args) => 
        {
            //and flip these two statements and you'll see different output
            //it seems these two datasources are related, but I can't figure out how
            cb.DataSource.Dump("cell datasource");
            ((DataGridViewComboBoxEditingControl)gv.EditingControl).DataSource.Dump("editing control datasource");
        };
        Controls.Add(gv);
    }
}

Warning: this code seems to crash linqpad sometimes.