【问题标题】:ComboBox.SelectedValue not working as expectedComboBox.SelectedValue 未按预期工作
【发布时间】:2016-04-18 12:23:22
【问题描述】:

所以我有一个 DataGridView,我将它用作表单上的“行选择器”,并且一堆控件绑定到 bindingSource。

其中一个绑定控件是用作查找的组合框,它可以为行选择状态,它是从带有从数据库中提取的数据的 DataTable 中填充的。

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

当从 DGV 中选择给定行时,表单控件按应有的方式显示给定行中的数据,但是“statusComboBox”并没有完全发挥作用。

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

IT 似乎只发生在上述场景中,其中行选择仅引发来自绑定 ComboBox 的显示响应,前提是先前的选择具有不同的“状态 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);
}

ProjectBindingSource 是这样初始化的:

private void InitBindingSource()
{
    ProjectsBindingSource = new BindingSource();
    projectsBindingNavigator.BindingSource = ProjectsBindingSource;
    ProjectsBindingSource.PositionChanged += new EventHandler(ProjectsBindingSource_PositionChanged);
}

ProjectsAddDataBindings 过程,以及 ComboBox 包含的 DataBindings.Add(在另外填充 ProjectsBindingSource 的 GetData 例程末尾执行):

ProjectsAddDataBindings();
{
    …
    this.statusComboBox.DataBindings.Add("Text", ProjectsBindingSource, "GSID");
    …
}

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

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

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

【问题讨论】:

    标签: c# datagridview combobox bindingsource lookup-tables


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-06-04
      • 2022-01-24
      • 2015-05-11
      • 2020-05-15
      • 2014-10-31
      • 2018-02-12
      • 2014-01-20
      • 2015-01-13
      • 2013-08-01
      相关资源
      最近更新 更多