Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C组合框双重数据绑定-基于entityframework值选择项_C#_Entity Framework_Data Binding_Combobox - Fatal编程技术网

C# C组合框双重数据绑定-基于entityframework值选择项

C# C组合框双重数据绑定-基于entityframework值选择项,c#,entity-framework,data-binding,combobox,C#,Entity Framework,Data Binding,Combobox,我有几个表,使用实体框架6。我的目标是将类table1绑定到ComboBox值成员 ComboBox数据源是: ComboBoxBasicDB[] statType = new ComboBoxBasicDB[] { new ComboBoxBasicDB { Text = "A1", Value = 0 }, new ComboBoxBasicDB { Text = "A2", Value = 1 }, new Combo

我有几个表,使用实体框架6。我的目标是将类table1绑定到ComboBox值成员

ComboBox数据源是:

ComboBoxBasicDB[] statType = new ComboBoxBasicDB[] {
            new ComboBoxBasicDB { Text = "A1", Value = 0 },
            new ComboBoxBasicDB { Text = "A2", Value = 1 },
            new ComboBoxBasicDB { Text = "A3", Value = 2 },
            new ComboBoxBasicDB { Text = "A4", Value = 4 },
            new ComboBoxBasicDB { Text = "B12", Value = 12 },
            new ComboBoxBasicDB { Text = "B13", Value = 13 },
            new ComboBoxBasicDB { Text = "B14", Value = 14 }
        };

statBS.DataSource = statType; // statBS == BindingSource, configured throught VS designer, comboBox.DataSource = statBS, comboBox.ValueMember = Value, comboBox.DisplayMember = Text
表1包含名为ex.Value1的属性,该属性包含0、1、2、4、12、13和14中的一个

我试图做的是从DB行加载,并在TextBox上使用类似的内容:

textBox.DataBindings.Add("Text", binding, "Name");
这很好用

我试过这样的方法:

comboBox.DataBindings.Add("SelectedValue", binding, "Value1");
但它不工作,查询后未选择任何内容。文本框绑定成功

我使用了SelectedIndex,但有一个问题,即值大于7,因为statType中有7项,而不是14项

我希望你明白我想做什么:/ 我想我可以通过comboBox.DataManager来实现,但它是私有的


谢谢您的建议。

所以解决方案是自定义实现,在提到的数据绑定中,将SelectedValue更改为SelectedItemValue

实施:

public class ComboBoxBasic : ComboBox
{
    bool diffTextColor = false;

    public ComboBoxBasic()
    {

    }

    public object SelectedItemValue
    {
        get
        {
            return (SelectedItem as ComboBoxBasicDB).Value;
        }

        set
        {
            for(int i = 0; i < Items.Count; i++)
            {
                ComboBoxBasicDB item = Items[i] as ComboBoxBasicDB;

                if(item.Value.ToString() == value.ToString())
                {
                    SelectedIndex = i;

                    break;
                }
            }
        }
    }

    public bool DifferentTextColor
    {
        get { return diffTextColor; }

        set
        {
            diffTextColor = value;

            if (diffTextColor)
            {
                DrawItem += ComboBoxBasic_DrawItem;
                DrawMode = DrawMode.OwnerDrawFixed;
            }
            else
                DrawItem -= ComboBoxBasic_DrawItem;
        }
    }

    void ComboBoxBasic_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();

        if (e.State == DrawItemState.Focus)
            e.DrawFocusRectangle();

        Brush brush = new SolidBrush((sender as Control).ForeColor);

        ComboBoxBasicDB item = (sender as ComboBoxBasic).Items[e.Index] as ComboBoxBasicDB;

        if (item.ForeColor != Brushes.Black)
            brush = item.ForeColor;

        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
        e.Graphics.DrawString(item.Text, (sender as Control).Font, brush, e.Bounds.X, e.Bounds.Y);
    }
}

另外还有自定义DrawItem,如果它是由DifferentittextColor启用的

您是否像我说的那样尝试过:comboBox.DataBindings.AddSelectedValue,binding,Value1;不工作:/n即使我尝试将按钮操作作为组合框。SelectedValue=14;