DataGridViewComboBoxCell valueValue 对绑定的 DataGridViewComboBoxCell 无效
DataGridViewComboBoxCell valueValue is invalid with binded DataGridViewComboBoxCell
我有一个带有两个 DataGridViewComboBoxCell 的 DatagridView:
- 管理
- 员工。
我想做的是当我 select 从 1st
开始管理
DataGridViewComboBoxCell,我将获取 selected
的员工列表
第二个 DataGridViewComboBoxCell 中的管理。我绑定了第一个
DataGridViewComboBoxCell 手动添加到 administrationBindingSource,我试过了
this answer code,这是我使用的代码:
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();
}
此代码第一次运行良好,但当我尝试更新管理值时,我收到此错误消息:
DataGridViewComboBoxCell value is invalid
如何解决?
尝试清除员工单元格的值,然后再将其重新绑定到 void
"selectEmployee",类似于:
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();
}
我有一个带有两个 DataGridViewComboBoxCell 的 DatagridView:
- 管理
- 员工。
我想做的是当我 select 从 1st
开始管理DataGridViewComboBoxCell,我将获取 selected
的员工列表第二个 DataGridViewComboBoxCell 中的管理。我绑定了第一个
DataGridViewComboBoxCell 手动添加到 administrationBindingSource,我试过了
this answer code,这是我使用的代码:
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();
}
此代码第一次运行良好,但当我尝试更新管理值时,我收到此错误消息:
DataGridViewComboBoxCell value is invalid
如何解决?
尝试清除员工单元格的值,然后再将其重新绑定到 void
"selectEmployee",类似于:
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();
}