Hi All,

I have two datagridviews on a form, one databound and one not (populated programmatically from Active Directory). I want to allow a user to double click on an item in the unbound DGV and have that item appear at the bottom of the bound DGV.

The doubleclick event calls BindingSource.AddNew(), which is picked up by the departmenttblBindingSource_AddingNew event. I've created a separate DepartmentCreator() class which should provide e.NewObject with the required object. However, it is throwing an error here. i.e. System.InvalidOperationException: Objects added to a BindingSource's list must all be of the same type.

However, I pretty sure they should be! The database table is very simple, just two fields: dept_ID (int and the PK), and department(nvarchar(150)). In the dataset they are Int32 and String. What am I doing wrong???

My code is as follows:

private void ADDepts_dataGridView_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DepartmentCreator dc = new DepartmentCreator();
                dc.Department = ADDepts_dataGridView[0, e.RowIndex].FormattedValue.ToString();
                
                departmenttblBindingSource.AddNew();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }

        private void departmenttblBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            try
            {
                e.NewObject = DepartmentCreator.CreateNewDepartment();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }


public class DepartmentCreator
{
    private Guid dept_ID = Guid.NewGuid();
    private string department = String.Empty;

    public DepartmentCreator()
    {
        department = "Empty";
    }

    public static DepartmentCreator CreateNewDepartment()
    {
        return new DepartmentCreator();
    }

    public string Department
    {
        get
        {
            return this.department;
        }

        set
        {
            this.department = value;
        }
    }
}

Many thanks in advance for any help.
Jon

Edited 6 Years Ago by __avd: Added [code] tags.

This article has been dead for over six months. Start a new discussion instead.