简体   繁体   English

ComboBox.SelectedValue不能按预期工作

[英]ComboBox.SelectedValue not working as expected

So I have a DataGridView which I am using as a “row selector” on a form, and a bunch of controls are bound to the bindingSource. 因此,我有一个DataGridView,它将用作表单上的“行选择器”,并且将一堆控件绑定到bindingSource。

One of the bound controls is a ComboBox serving as a lookup which enables status choices for the rows, this is populated from a DataTable with data pulled from the DB. 绑定控件之一是ComboBox,用作可为行启用状态选择的查找,该控件是从DataTable中填充的,并从DB中提取了数据。

The population of this box is without any issue. 此框的人口没有任何问题。

When a given row is selected from the DGV the form controls display data from the given row as they should, however the “statusComboBox” is not quite playing the game. 当从DGV中选择了给定的行时,窗体控件将按原样显示来自给定行的数据,但是“ statusComboBox”并不是很容易玩。

If in the DGV, I choose a row that has a different status to one previously selected, it works as it should, however, if I choose a row with the same value to a previously selected row, instead of the box showing the DisplayMember it shows the ValueMember. 如果在DGV中选择的状态与先前选择的状态不同的行,则它应能正常工作,但是,如果我选择与先前选择的行具有相同值的行,而不是显示DisplayMember的框,显示ValueMember。

IT only seems to occur in the above scenario, where the rows selection only instigates a display response from the bound ComboBox providng a previous selection had a different “Status ID”. 仅在上述情况下才出现IT问题,其中行选择仅会引发来自绑定的ComboBox的显示响应,前提是先前的选择具有不同的“状态ID”。 What have I dont wrong that would cause this behaviour? 我没有弄错什么会导致这种行为?

So the form load looks like this 所以表单加载看起来像这样

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

The ProjectBindingSource is initialised thus: 因此,对ProjectBindingSource进行了初始化:

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

A ProjectsAddDataBindings procedure, and the contained DataBindings.Add for the ComboBox (executed at the end of a GetData routine that additionally populated ProjectsBindingSource): 一个ProjectsAddDataBindings过程以及ComboBox包含的DataBindings.Add(在另外填充ProjectsBindingSource的GetData例程的末尾执行):

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

After the GetData block the GeneralStatusInitLookup populates the Lookup elements, in a helper class simply because it provides functionality to a number of different forms 在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"));
}

And the DGV initiated row change is handled like this 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;
    }
}

Anyway I hope I haven't swamped readers with to much code, but frankly I cant see where this is going wrong. 无论如何,我希望我不会让太多的代码淹没读者,但坦率地说,我看不到这出了什么问题。 So any help would be greatly appreciated. 因此,任何帮助将不胜感激。

This was a simple solution in the end. 最后,这是一个简单的解决方案。 Both the GeneralStatusInitLookup() and the ProjectsAddDataBindings() blocks made use of DataBindings.Add ... For the lookup table this was fine, but with the binding to the main table; GeneralStatusInitLookup()和ProjectsAddDataBindings()块都使用DataBindings.Add ...对于查找表,这很好,但是可以绑定到主表; the later, I had used "Text" as the propertyName parameter. 后来,我使用“文本”作为propertyName参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM