65.9K
CodeProject 正在变化。 阅读更多。
Home

如何为 ComboBox 项设置值

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.56/5 (27投票s)

2002年6月27日

3分钟阅读

viewsIcon

714036

downloadIcon

5652

如何将 ValueMember 与 ADO.NET 结合使用。

Sample Image - ValueMemberSample.gif

引言

我是一名 C# 初学者,已经学习了 2 个月。最近,我一直在苦苦寻找如何在 ComboBox 项目中设置值的方法。我从书籍、互联网上的示例代码和 MSDN 中寻找答案。我们可以使其显示主 ID 而不是一些有意义的名称,但这不是我想要的。我不想再在我的生活中看到毫无意义的 ID!在 MSDN 中可以找到找到它的提示。您可以在 ValueMember 部分找到代码。但是代码只有 80%。缺少一些东西...我试图找出它,但一个星期都无法得到它。最后,当我尝试意外地创建一个类时,奇迹发生了!无论如何,我希望我可以帮助人们免受像我一样的痛苦。

创建一个示例软件

我们将创建一个示例软件(见图),该软件具有一个用于作者姓名列表的 ComboBox,当您选择一个作者姓名时,它会将书名显示到一个 ListBox 中。关键是,当然,我没有显示作者 ID 而是显示了作者姓名。此外,我使用 ADO.NET 存储数据,所以我们也可以学习它 :-)

准备数据库

我使用 MS Access 存储数据。并且 tblBooks 具有外键 (AuthorID) 以便与 tblAuthors 中的 AuthorID 建立关系。

books.mdb (位置 => /bin/debug/books.mdb)
tblAuthors
AuthorID AuthorName
1 Tomohiro
2 Henry
3 Ben
4 Jungwon
tblBooks
BookID AuthorID 标题
1 1 Hello World
2 1 Japanese Culture
3 1 Fujisan
4 2 蜘蛛
5 2 CDR
6 2 Beginning VB6
7 3 Smart Business
8 3 MBA holder
9 3 Web Business
10 4 English 666
11 4 Korean 102
12 4 How to be beautiful like me
13 4 Precalculous for even stupids
14 4 Super duper cooking

创建界面

现在是开始的时候了!您需要确保您有一个 ComboBox (cboAutors) 和一个 ListBox (lstBooks)。当然,您可以根据自己的喜好更改界面,但请记住,您必须拥有这两个控件。

  • ComboBox - cboAutors
  • ListBox - lstBooks

创建模块级别值

好吧,我们需要首先创建两个模块级别的值。然后,您为连接值提供数据库的类型和路径。并且您需要确保在顶部添加 Using System.Data.OleDb

值条目

  • private bool AuthorsHaveBeenAdded - 确保您的 cboAuthors ComboBox 已构建。
  • private OleDbConnection conn - 连接到 Books.mdb
private OleDbConnection conn;
private bool AuthorsHaveBeenAdded=false;
public Form1()
    {
        InitializeComponent();
        // Create Connection.
        this.conn = new OleDbConnection("
        PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Books.mdb;");

构建 cboAutors ComboBox

现在我们将为 valueMember 设置一个 AuthorID,并为 DisplayMember 设置一个 Author Name。顺序是从数据库读取数据,将数据传递到 ArrayList,然后传递到 cboAuthors ComboBox。稍后我们将创建一个 AddValue 函数。

值条目

  • OleDbCommand cmd - 创建 SQL 命令
  • OleDbDataReader rsAutors - 读取 tblAuthors
  • ArrayList Authors - 存储 Authors 表数据
private void buildcboAuthors()
{
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM tblAuthors",conn);
    this.conn .Open ();
    OleDbDataReader rsAutors = cmd.ExecuteReader();
    ArrayList Authors = new ArrayList();

    while(rsAutors.Read())
    {
        Authors.Add (new AddValue
            (rsAutors.GetString(1),rsAutors.GetInt32 (0)));
                
    }
    rsAutors.Close();
    this.conn.Close();
            
    this.cboAuthors.DataSource = Authors;
    this.cboAuthors .DisplayMember ="Display";
    this.cboAuthors.ValueMember = "Value";        
    AuthorsHaveBeenAdded=true;                        
}

然后调用 buildcboAuthors()

    InitializeComponent();
    // Create Connection.
    this.conn = new OleDbConnection(
       "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Books.mdb;");
            
    buildcboAuthors();
}

创建 AddValue 类

这是我花了一个星期才搞定的。这很简单,只是获取值并创建属性。

    public class AddValue
    {
        private string m_Display;
        private long m_Value;
        public AddValue(string Display, long Value)
        {
            m_Display = Display;
            m_Value = Value;
        }
        public string Display
        {
            get{return m_Display;}
        }
        public long Value
        {
            get{return m_Value;}
        }
    }

构建 lstBooks ListBox

现在我们将显示书名。我们可以使用 WHERE 来显示我们想要的内容。并在 cboAuthors_SelectedIndexChanged 事件中调用 buidlstBooks

值条目

  • OleDbCommand cmd- 创建 SQL 命令
  • OleDbDataReader rsBooks - 读取 tblBooks
        private void buildlstBooks()
        {
            this.lstBooks.Items.Clear();
            OleDbCommand cmd = new OleDbCommand(
                "SELECT * FROM tblBooks WHERE AuthorID =" + 
                this.cboAuthors.SelectedValue,conn);
            this.conn .Open ();
            OleDbDataReader rsBooks = cmd.ExecuteReader();
            while(rsBooks.Read ())
            {
                this.lstBooks .Items.Add (rsBooks.GetString (2));
            }
            rsBooks.Close ();
            this.conn.Close ();

        }

        private void cboAuthors_SelectedIndexChanged
                    (object sender, System.EventArgs e)
        {
            if(this.AuthorsHaveBeenAdded)
                buildlstBooks();
        }

结论

我想就是这样了。祝编程愉快!:0)

© . All rights reserved.