将数据添加到ComboBox(未绑定数据)

时间:2011-06-03 01:30:17

标签: c# user-interface

我希望将数据添加到组合代码列表中,但我不确定正确的方法。数据来自原始SQL语句。

我直接从数据库查看了绑定数据,但不清楚所有这些绑定和数据集是如何为我工作的所以我决定跳过这个并将数据自己插入到combox中(在你的帮助下)。 / p>

我在网上看到的代码如下:

public partial class Form1 : Form {
    // Content item for the combo box
    private class Item {
        public string Name;
        public int Value;
        public Item(string name, int value) {
            Name = name; Value = value;
        }
        public override string ToString() {
            // Generates the text shown in the combo box
            return Name;
        }
    }
    public Form1() {
        InitializeComponent();
        // Put some stuff in the combo box
        comboBox1.Items.Add(new Item("Blue", 1));
        comboBox1.Items.Add(new Item("Red", 2));
        comboBox1.Items.Add(new Item("Nobugz", 666));
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
        // Display the Value property
        Item itm = (Item)comboBox1.SelectedItem;
        Console.WriteLine("{0}, {1}", itm.Name, itm.Value);
    }
}

您是否真的必须创建一个新类才能将数据添加到组合框中? 此外,使用上述技术我的代码如下:

    while (rdata.Read()){
        String Name = (String)rdata["vetName"];
        Name = Name.Trim();
        String Surname = (String)rdata["vetSurname"];
        Surname = Surname.Trim();

        String id = rdata["vetID"].ToString().Trim();

        MessageBox.Show("ID " + id);

        int value1 = Convert.ToInt32(id);
        MessageBox.Show("value1 " + value1);

        String display = (String)Name + " " + Surname;

        editVetComboBox.Items.Add(new Item(display, 2));
    }

问题在于,虽然组合框中填充了名字和姓氏,但未添加值(ID)。

任何想法?

非常感谢, 理查德

4 个答案:

答案 0 :(得分:9)

消息来源:http://tipsntricksbd.blogspot.com/2007/12/combobox-is-one-of-most-common-gui.html

ComboBox是最常见的GUI元素之一。它用于为用户提供从列表中选择项目或输入新文本的功能。在这里,我将使用Microsoft Visual Studio .Net 2005向您展示C#中ComboBox的一些常用和有用的功能。

最简单的ComboBox:

在最简单的情况下,我们在列表中添加一些字符串,如下所示 -

myComboBox.Items.Add("Bangladesh");
myComboBox.Items.Add("India");
myComboBox.Items.Add("Pakistan");
myComboBox.Items.Add("Srilanka");
myComboBox.Items.Add("Maldives");
myComboBox.Items.Add("Nepal");
myComboBox.Items.Add("Bhutan");

排序列表:

通常用户希望选项按排序顺序显示。为此,我们必须添加一行代码 -

myComboBox.Sorted = true;

<强> DropDownStyle:

在ComboBox中,用户可以输入文本或只选择列表中的项目。所以开发者应该设置它的风格。有3种选择:

ComboBoxStyle.DropDownList: User can just select one item from a list.
ComboBoxStyle.DropDown: User can either type a text or select an item from list.
ComboBoxStyle.Simple: User can only type a text in text box. Item list is not shown.

示例:

myComboBox.DropDownStyle = ComboBoxStyle.DropDown;

<强>宝贵意见/词典:

当用户输入文本时,如果在键入时组合框下方显示某些建议,则他/她会感到高兴。对于此功能,我们需要写几行 -

myComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
myComboBox.AutoCompleteMode = AutoCompleteMode.Suggest;

诀窍:

可能存在用户选择一些可读文本的情况,但对于程序员来说,相应的值(不是所选文本)很重要。例如,在数据库项目中,StudentID对于程序员而言比StudentName更重要。因此,如果我们可以在组合框中添加(名称,值)组合并且在名称选择时我们可以轻松获得相应的值,那将是很好的。

我们可以通过添加一个包含Name和Value的对象来实现。

class ComboBoxItem
{
public string Name;
public int Value;
public ComboBoxItem(string Name, int Value)
{
this.Name = Name;
this.Value = Value;
}
}


myComboBox.Items.Add(new ComboBoxItem("Ashis Saha",1));
myComboBox.Items.Add(new ComboBoxItem("Subrata Roy", 2));
myComboBox.Items.Add(new ComboBoxItem("Aminul Islam", 3));
myComboBox.Items.Add(new ComboBoxItem("Shakibul Alam", 4));
myComboBox.Items.Add(new ComboBoxItem("Tanvir Ahmed", 5));

但是如果您现在看到ComboBox列表,您会注意到所有项目都是相同的,它们是这些对象的类名。实际上,这些项只不过是那些对象的ToString()函数的输出。因此,如果我们简单地覆盖ToString()函数以表现我们的期望,那么我们就完成了。

class ComboBoxItem
{
public string Name;
public int Value;
public ComboBoxItem(string Name, int Value)
{
this.Name = Name;
this.Value = Value;
}

// override ToString() function
public override string ToString()
{
return this.Name;
}
}

您可以按以下方式获取所选值 -

int selectedValue = ((ComboBoxItem)myComboBox.SelectedItem).Value;

答案 1 :(得分:1)

我可以看到代码中的值始终为2。这可能是你遇到的问题。

此外,尝试简化您的代码:

while (rdata.Read())
{
    string name = (string)rdata["vetName"];
    string surname = (string)rdata["vetSurname"];
    int id = (int)rdata["vetID"];
    string display = name.Trim() + " " + surname.Trim();
    editVetComboBox.Items.Add(new Item(display, id));
}

假设vetID为integer且所有列出的字段在数据库中都不可为空。

答案 2 :(得分:0)

不,您不必创建新类来向ComboBox添加数据,您可以根据需要添加字符串。课程是为了方便,这里看起来很方便 - 你为什么要避免它们? (也许我不太明白你的问题......)

如果你要做的就是执行字符串操作并且一直在类上使用ToString(),而不是访问单个属性,那么就没有必要 - 只需将其格式化为字符串并使用它。

答案 3 :(得分:0)

不,你不必创建自己的类......但它确实对你正在做的事情有意义。在您的应用程序中,最好让类类型代表您的数据库实体。覆盖实体类的ToString()方法。接下来,将这些类类型直接插入到ComboBox中。 ComboBox将在您的实体上调用ToString()以显示文本。

以下是它的样子:

public class Vet
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string SurName { get; set; }

    public Vet()
    {
        // default constructor
    }

    public Vet( IDataRecord record )
    {
        ID = (int) record["vetid"];
        Name = (string) record["vetname"];
        SurName = (string) record["vetsurname"];
    }

    public override string ToString()
    {
        return Name + " " + SurName;
    }
}

接下来,将实例直接添加到您的组合框中:

comboBox.Items.Clear();
while( rdata.Read() )
{
   comboBox.Items.Add( new Vet( rdata ) );
}