0

I have a DatagridView with two DataGridViewComboBoxCell:

  1. Administration
  2. Employee.

What I want to do is when I select an administration from 1st

DataGridViewComboBoxCell, I'll get the list of the employees of selected

Admininstration in the 2nd DataGridViewComboBoxCell. I binded the first

DataGridViewComboBoxCell manually to administrationBindingSource and I tried

this answer code, This is the code I used:

dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);

void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (dataGridView1.IsCurrentCellDirty)
        {
            // This fires the cell value changed handler below
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex == -1)
    {
        return;
    }

    DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.CurrentRow.Cells["administrationColumn"];
    int item = 0;
    if (cb.Value != null)
    {
        item = (Int32)cb.Value;
        selectEmployee(item);
        dataGridView1.Invalidate();
    }

}
private void selectEmployee(int idAdministration)
{
    if (con.State != ConnectionState.Open)
    {
        con.Open();
    }
    SqlCommand Cmd = new SqlCommand("SELECT * FROM Employee WHERE IdAdministration = @idAdministration", con);
    Cmd.Parameters.AddWithValue("@idAdministration", idAdministration);
    DataTable Dt = new DataTable();
    Dt.Load(Cmd.ExecuteReader());
    DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.CurrentRow.Cells["Employee"];
    cb.DataSource = Dt;
    cb.DisplayMember = "Name";
    cb.ValueMember = "CodeEmployee";
    con.Close();
}

This code works fine in the first time but when I try to update admininstration value I got this error message:

DataGridViewComboBoxCell value is invalid

How to fix it?

Community
  • 1
  • 1
user4374239
  • 93
  • 2
  • 11
  • i guess, you have to check the value for the "employee" cell. when you change the administrator, the employee items get refreshed but the value of the cell is still the same. you have to handle the case, that the employee value is not part of the new list. so you have to set the value to any existing item or add en empty item and clear it – Koryu Mar 04 '15 at 13:19
  • Yes i also think about this but i didn't know how to clear employee cell, i tried a lot but i didn't get it. could you please help me – user4374239 Mar 04 '15 at 13:23

1 Answers1

3

Try to clear the value of employee cell before rebind it in the void

"selectEmployee", something like:

private void selectEmployee(int idAdministration)
{
    if (con.State != ConnectionState.Open)
    {
        con.Open();
    }
    SqlCommand Cmd = new SqlCommand("SELECT * FROM Employee WHERE IdAdministration = @idAdministration", con);
    Cmd.Parameters.AddWithValue("@idAdministration", idAdministration);
    DataTable Dt = new DataTable();
    Dt.Load(Cmd.ExecuteReader());
    DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.CurrentRow.Cells["Employee"];
    cb.Value = null; //add this
    cb.DataSource = Dt;
    cb.DisplayMember = "Name";
    cb.ValueMember = "CodeEmployee";
    con.Close();
}
user4340666
  • 1,453
  • 15
  • 36