如何将数据源重新绑定到winform DataGridView Combobox列?

时间:2014-11-19 12:24:08

标签: c# winforms datagridview combobox

我在winform中有一个带有2个组合框列的DataGridView:1)公司,2)帐户。 我想根据所选公司更新帐户组合框。 我有这段代码:

void recipientsDataGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        try
        {
            ComboBox cb = e.Control as ComboBox;
            if (cb != null)
            {
                cb.SelectedValueChanged -= new EventHandler(companyCombobox_SelectedValueChanged);
                cb.SelectedValueChanged += new EventHandler(companyCombobox_SelectedValueChanged);
            }
        }
        catch (Exception ex)
        {

        }
    }

    private void companyCombobox_SelectedValueChanged(object sender, EventArgs e)
    {
        try
        {
            var currentCell = recipientsDataGrid.CurrentCellAddress;
            if (currentCell.X == 3)
            {
                var sendingCB = sender as DataGridViewComboBoxEditingControl;
                int companyId = sendingCB.SelectedValue.ToInt();
                DataTable dtAccounts = m_CustomersFunctions.GetCompanyAccounts(companyId);

                DataGridViewComboBoxCell cboAccounts = (DataGridViewComboBoxCell)recipientsDataGrid.Rows[currentCell.Y].Cells["Account"];
                cboAccounts.ValueMember = "account_id";
                cboAccounts.DisplayMember = "AccountName";
                cboAccounts.DataSource = dtAccounts;

                int defaultAccountId = (from row in dtAccounts.AsEnumerable()
                                        where row.Field<string>("AccountName").EndsWith("*")
                                        select row.Field<int>("account_id")).FirstOrDefault();
                if (defaultAccountId > 0)
                    cboAccounts.Value = defaultAccountId;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

我第一次选择公司时工作正常,但当我更改公司并尝试更新帐户组合框的数据源时,我收到错误:

enter image description here

我尝试手动添加项目,而不是使用DataSource,我得到了同样的错误。 我该如何解决? 请...

1 个答案:

答案 0 :(得分:3)

尝试在重新绑定之前清除帐户单元格的值,如下所示:

private void companyCombobox_SelectedValueChanged(object sender, EventArgs e)
{
    try
    {
        var currentCell = recipientsDataGrid.CurrentCellAddress;
        if (currentCell.X == 3)
        {
            var sendingCB = sender as DataGridViewComboBoxEditingControl;
            int companyId = sendingCB.SelectedValue.ToInt();
            DataTable dtAccounts = m_CustomersFunctions.GetCompanyAccounts(companyId);

            DataGridViewComboBoxCell cboAccounts = (DataGridViewComboBoxCell)recipientsDataGrid.Rows[currentCell.Y].Cells["Account"];
            cboAccounts.Value = null; //Add this code
            cboAccounts.ValueMember = "account_id";
            cboAccounts.DisplayMember = "AccountName";
            cboAccounts.DataSource = dtAccounts;

            int defaultAccountId = (from row in dtAccounts.AsEnumerable()
                                    where row.Field<string>("AccountName").EndsWith("*")
                                    select row.Field<int>("account_id")).FirstOrDefault();
            if (defaultAccountId > 0)
                cboAccounts.Value = defaultAccountId;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}