C# ComboBox.SelectedValue未按预期工作

C# ComboBox.SelectedValue未按预期工作,c#,datagridview,combobox,bindingsource,lookup-tables,C#,Datagridview,Combobox,Bindingsource,Lookup Tables,因此,我有一个DataGridView,我将其用作表单上的“行选择器”,并且一组控件绑定到bindingSource 绑定控件之一是一个组合框,用作查找,可为行启用状态选择,它是从数据表中填充的,其中包含从数据库中提取的数据 这个盒子的人口没有任何问题 当从DGV中选择给定行时,表单控件将按其应有的方式显示给定行中的数据,但是“statusComboBox”并不是在玩游戏 如果在DGV中,我选择了一个与先前选择的行具有不同状态的行,它将正常工作。但是,如果我选择了一个与先前选择的行具有相同值的行

因此,我有一个DataGridView,我将其用作表单上的“行选择器”,并且一组控件绑定到bindingSource

绑定控件之一是一个组合框,用作查找,可为行启用状态选择,它是从数据表中填充的,其中包含从数据库中提取的数据

这个盒子的人口没有任何问题

当从DGV中选择给定行时,表单控件将按其应有的方式显示给定行中的数据,但是“statusComboBox”并不是在玩游戏

如果在DGV中,我选择了一个与先前选择的行具有不同状态的行,它将正常工作。但是,如果我选择了一个与先前选择的行具有相同值的行,而不是显示DisplayMember的框,它将显示ValueMember

这似乎只发生在上面的场景中,行选择仅从绑定的组合框触发显示响应,前提是以前的选择具有不同的“状态ID”。我有什么错会导致这种行为

所以表单加载看起来像这样

private void ProjectsForm_Load(object sender, EventArgs e)
{  
    InitBindingSource();

    //// bind Selector
    //ASMod$ this needs to be 'true' unless you explicitly declare columns
    ProjectsDataGridView.AutoGenerateColumns = false;
    ProjectsDataGridView.DataSource = ProjectsBindingSource;

    GetData();

    //Set GeneralStatusBox
    Helpers.GeneralStatusInitLookup(statusComboBox, ProjectsBindingSource);
}
private void ProjectsBindingSource_PositionChanged(object sender, EventArgs e)
{
    try
    {
        // Update the database with the user's changes.
        UpdateProjects();
        statusComboBox.SelectedValue = (int)CurrentDataRowView.Row["GSID"];
    }
    catch (Exception)
    {
    }
}

private void UpdateProjects()
{
    try
    {
        ProjectsDataAdapter.Update((DataTable)ProjectsBindingSource.DataSource);

        DataHelper.CommitProposedChanges(projectsDataSet);
        if (this.projectsDataSet.HasChanges() == true)
        {
            ProjectsBindingSource.EndEdit();
            ProjectsDataAdapter.Update();
        }

        CurrentDataRowView = (DataRowView)ProjectsBindingSource.Current;
    }
    catch (InvalidOperationException)
    {
        throw;
    }
    catch (Exception)
    {
        throw;
    }
}
ProjectBindingSource已初始化,因此:

private void InitBindingSource()
{
    ProjectsBindingSource = new BindingSource();
    projectsBindingNavigator.BindingSource = ProjectsBindingSource;
    ProjectsBindingSource.PositionChanged += new EventHandler(ProjectsBindingSource_PositionChanged);
}
ProjectsAddDataBindings过程,以及ComboBox包含的DataBindings.Add(在另外填充ProjectsBindingSource的GetData例程结束时执行):

在GetData块之后,GeneralStatusInitLookup在助手类中填充查找元素,这仅仅是因为它为许多不同的表单提供了功能

public static void GeneralStatusInitLookup(System.Windows.Forms.ComboBox comboBox, BindingSource primaryBindingSource)
{
    string statusFilter = "";
    statusFilter = Helpers.GetStatusGroupFilter(EndeavourForm.FilterId);
    if (statusFilter != "")
    {
        statusFilter = " WHERE " + statusFilter;
    }
    //// string statusFilter = ""; //// temp

    string sql = "";
    sql = "SELECT GSID, ShortName FROM GeneralStatus" + statusFilter + " ORDER BY Pos";
    GeneralStatusDataTable = Helpers.Db.GetDataTable(sql);

    comboBox.DataSource = GeneralStatusDataTable;
    comboBox.DisplayMember = "ShortName";
    comboBox.ValueMember = "GSID";

    comboBox.DataBindings.Add(new Binding("SelectedValue", primaryBindingSource.DataSource, "GSID"));
}
DGV发起的行更改是这样处理的

private void ProjectsForm_Load(object sender, EventArgs e)
{  
    InitBindingSource();

    //// bind Selector
    //ASMod$ this needs to be 'true' unless you explicitly declare columns
    ProjectsDataGridView.AutoGenerateColumns = false;
    ProjectsDataGridView.DataSource = ProjectsBindingSource;

    GetData();

    //Set GeneralStatusBox
    Helpers.GeneralStatusInitLookup(statusComboBox, ProjectsBindingSource);
}
private void ProjectsBindingSource_PositionChanged(object sender, EventArgs e)
{
    try
    {
        // Update the database with the user's changes.
        UpdateProjects();
        statusComboBox.SelectedValue = (int)CurrentDataRowView.Row["GSID"];
    }
    catch (Exception)
    {
    }
}

private void UpdateProjects()
{
    try
    {
        ProjectsDataAdapter.Update((DataTable)ProjectsBindingSource.DataSource);

        DataHelper.CommitProposedChanges(projectsDataSet);
        if (this.projectsDataSet.HasChanges() == true)
        {
            ProjectsBindingSource.EndEdit();
            ProjectsDataAdapter.Update();
        }

        CurrentDataRowView = (DataRowView)ProjectsBindingSource.Current;
    }
    catch (InvalidOperationException)
    {
        throw;
    }
    catch (Exception)
    {
        throw;
    }
}

无论如何,我希望我没有用太多的代码淹没读者,但坦率地说,我看不出哪里出了问题。因此,任何帮助都将不胜感激。

这最终是一个简单的解决方案。GeneralStatusInitLookup()和ProjectsAddDataBindings()块都使用了数据绑定。添加。。。对于查找表,这很好,但是绑定到主表;后来,我使用“Text”作为propertyName参数