c# - ComboBox.SelectedValue 未按预期工作

标签 c# datagridview combobox bindingsource lookup-tables

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

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

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

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

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

IT 似乎只发生在上述场景中,其中行选择仅从绑定(bind)的 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 block 之后,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;
    }
}

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

最佳答案

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

关于c# - ComboBox.SelectedValue 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36693907/

相关文章:

c# - Wpf 图像控件阻止文件

c# - linq 按月份名称和年份排序(MMM yyyy 字符串)

c# - 为什么将函数分配给事件时使用 '+='?

c# - Entity Framework : C# Winforms bindingsource delete on datagridview, 但仅将 isDeleted 字段标记为真(不删除)

c# - 绑定(bind)到 BindingList 的 DataGridView 在值更改时不刷新

c# - 数据绑定(bind)时的 wpf 默认组合框项

c# - 如何让用户登录系统并仅在用户单击注销按钮后注销?

c# - 以编程方式添加 DataGridView 列

winapi - 如何将可编辑的 ComboBox 添加到 Vista 的 Common Item Dialog?

c# - 组合框的复合 DisplayMemberPath