DataGridViewComboBoxCell valueValue is invalid with binded DataGridViewComboBoxCell

后端 未结 1 1522
梦毁少年i
梦毁少年i 2021-01-27 12:12

I have a DatagridView with two DataGridViewComboBoxCell:

  1. Administration
  2. Employee.

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

1条回答
  •  隐瞒了意图╮
    2021-01-27 12:39

    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();
    }
    

    0 讨论(0)
提交回复
热议问题