alueValue对绑定的DataGridViewComboBo

alueValue对绑定的DataGridViewComboBo

本文介绍了DataGridViewComboBoxCell valueValue对绑定的DataGridViewComboBoxCell无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个DataGridViewComboBoxCell的DatagridView:

I have a DatagridView with two DataGridViewComboBoxCell:


  1. 管理

  2. Employee。

我要做的是当我从1st选择一个管理

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

DataGridViewComboBoxCell,我将在第二个DataGridViewComboBoxCell中获得选定的

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

Admininstration的员工列表。我将第一个

Admininstration in the 2nd DataGridViewComboBoxCell. I binded the first

DataGridViewComboBoxCell手动绑定到administrationBindingSource,然后尝试

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:

如何解决它?

推荐答案

在将其重新绑定到空白之前,请尝试清除员工单元格的值

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

selectEmployee,

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

这篇关于DataGridViewComboBoxCell valueValue对绑定的DataGridViewComboBoxCell无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 06:23