【问题标题】:Is there a way to assign values to a combobox without using a datasource?有没有办法在不使用数据源的情况下将值分配给组合框?
【发布时间】:2014-05-14 22:32:58
【问题描述】:

我正在使用 Entity Framework 将一个组合框与我的 MSSQL 数据库中的值组合在一起,使用以下方法

using (var context = new Entity())
{
    var things = (from p in context.Stuff 
                  where ((p.SourceId == StuffId && p.Domain.Value == "Stuff") 
                    || (p.SourceId == OtherStuffId && p.Domain.Value == "OtherStuff")) 
                    && p.Done == true orderby p.StuffId 
                  select p);

    foreach(var stuff in things)
        cboRejectTask.Items.Add(stuff.StuffId + ": " + stuff.StuffType.Description + " " + stuff.StuffType.DisplayName);
}

我想为每一行分配值,以便在获取用户选择的内容时,我不必进行字符串操作来获得我想要的内容。如果可能,我不想使用数据源。

解决方案

鉴于没有比创建自定义类更好的方法了,我继续使用所选答案的代码进行了修改以供长期使用。 (注意:你真的可以使用任何给定的对象,只要 ToString() 返回“显示文本”并且它有一个 Tag 或任何与你的需要兼容的可写属性)

public class ComboBoxItem
{
    public string Display;
    public object Value;

    public override string ToString()
    {
        return Display;
    }
}

鉴于此代码,我现在可以将代码更改为以下内容:

using (var context = new Entity())
{
    var things = (from p in context.Stuff 
                  where ((p.SourceId == StuffId && p.Domain.Value == "Stuff") 
                    || (p.SourceId == OtherStuffId && p.Domain.Value == "OtherStuff")) 
                    && p.Done == true orderby p.StuffId 
                  select p);

    foreach(var stuff in things)
        cboRejectTask.Items.Add(new ComboBoxItem() { Display = stuff.StuffId + ": " + stuff.StuffType.Description + " " + stuff.StuffType.DisplayName, Value = stuff.StuffId });
}

【问题讨论】:

  • 一些控件有一个可能有用的.Tag 属性。但由于每个单独的项目都不是一个控件,我不确定这是否有效。您可以直接将对象添加到Items 并使用DisplayMember 属性来选择您要显示的内容(ValueMember 属性也可能有用)。

标签: c# winforms entity-framework


【解决方案1】:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        var cbi1 = new ComboBoxItem("Item 1") { Id = 1 };
        var cbi2 = new ComboBoxItem("Item 2") { Id = 2 };
        var cbi3 = new ComboBoxItem("Item 3") { Id = 3 };
        comboBox1.Items.Add(cbi1);
        comboBox1.Items.Add(cbi2);
        comboBox1.Items.Add(cbi3);
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var id = ((ComboBoxItem)comboBox1.SelectedItem).Id;

        MessageBox.Show(id.ToString());

    }
}

public class ComboBoxItem
{
    private readonly string text;

    public int Id { get; set; }

    public ComboBoxItem(string text)
    {
        this.text = text;
    }

    public override string ToString()
    {
        return text;
    }
}

【讨论】:

  • 这确实是一个解决方案(并且是一个完整的解决方案,谢谢),如果没有更容易的事情发生,这将是我采取的路线。可惜这并不容易……这似乎是一件简单(且频繁)的事情。
  • 完全没有描述?我确实认为,如果您提供一些关于您的答案如何解决问题的描述,OP 和其他有类似问题的人会从中学习,这会更有帮助。
【解决方案2】:

我想你可能会觉得这很有用:

comboBox1.Items.Add(1);
comboBox1.Items.Add(2);
comboBox1.Items.Add(3);

private void comboBox1_Format(object sender, ListControlConvertEventArgs e)
{
    //this will set what gets displayed in the combobox, but does not change the value.
    e.Value = "display value";
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    MessageBox.Show((sender as ComboBox).SelectedItem.ToString());
}

【讨论】:

  • 我知道添加项目(毕竟它在我的代码中),我更多地考虑价值blogs.msdn.com/b/jaredpar/archive/2006/11/07/… 我可能应该在这方面更具体...
  • 您必须将数据转换为数组并使用AddRange()
  • 我不太确定我是否理解这将如何为每个项目提供自定义值?...除非您的意思是引用组合框的 selectedindex,然后使用相同的索引来引用数组(我'已经想到了这一点,但数组需要是全局的,我想这会很讨厌)
  • 也许这会有所帮助
猜你喜欢
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2019-07-07
  • 2018-07-07
相关资源
最近更新 更多