简体   繁体   English

如何填充c#windows表单组合框?

[英]How to populate c# windows forms combobox?

如何从sql数据库填充一个组合框(带有id和名称列的学生表),显示文本代表学生的名字,组合框项目的值是该学生的id,当我得到的值时组合框我会得到id值

Below are the important properties for you. 以下是您的重要属性。

ComboBox.DataSource Property ComboBox.DataSource属性

A data source can be a database, a Web service, or an object that can later be used to generate data-bound controls. 数据源可以是数据库,Web服务或稍后可用于生成数据绑定控件的对象。 When the DataSource property is set, the items collection cannot be modified. 设置DataSource属性时,无法修改items集合。

ComboBox.DisplayMember Property ComboBox.DisplayMember属性

A String specifying the name of an object property that is contained in the collection specified by the DataSource property. 一个String,指定DataSource属性指定的集合中包含的对象属性的名称。 The default is an empty string (""). 默认值为空字符串(“”)。

ComboBox.ValueMember Property ComboBox.ValueMember属性

A String representing the name of an object property that is contained in the collection specified by the DataSource property. 一个String,表示DataSource属性指定的集合中包含的对象属性的名称。 The default is an empty string (""). 默认值为空字符串(“”)。

DataTable dataTable = GetDataTable("Select * from Student"); // You have to implement the ways to retrieve data from the database.
comboBox1.Datasource = dataTable;
comboBox1.DisplayMember = StudentName; // Column Name
comboBox1.ValueMember = StuentId;  // Column Name

Here is one way if you want to add items programmatically. 如果您想以编程方式添加项目,这是一种方法。

private class Item 
{
      public string _Name;
      public int _Id

      public Item(string name, int id) 
      {
          _Name = name; 
          _Id = id;
      }

      public string Name
      {
          get { return _Name; }
          set { _Name = value; }
      }

      public string Id
      {
          get { return _Id; }
          set { _Id = value; }
      }
}   

comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";

comboBox1.Items.Add(new Item("Student 1", 1));
comboBox1.Items.Add(new Item("Student 2", 2));
comboBox1.Items.Add(new Item("Student 3", 3));

There are various ways of doing this. 有很多方法可以做到这一点。

How to: Add and Remove Items from a Windows Forms ComboBox 如何:从Windows窗体组合框中添加和删除项目

ComboBox.Items Property ComboBox.Items属性

First off you need to figure out how you're going to get the data back from the DB, but I'll assume you either know that or intend to ask another question in regards to that. 首先,您需要弄清楚如何从数据库中获取数据,但我会假设您已经知道或打算就此提出另一个问题。 From there, your best bet is to bind some collection to the ComboBox . 从那里,你最好的办法是将一些集合绑定到ComboBox Here is an example of doing that with a DataSet . 以下是使用DataSet执行操作的示例。 You can also bind to List<T> or other IEnumerable<T> , which would make more sense if you're going to use LINQ to get at the data. 您还可以绑定到List<T>或其他IEnumerable<T> ,如果您要使用LINQ获取数据,这将更有意义。 Here is a question here on SO about binding a List to a ComboBox Perhaps you could tell us how you intend to get at the data so we could give you a more tailored answer? 这里有一个关于将List绑定到ComboBox也许您可以告诉我们您打算如何获取数据,以便我们可以为您提供更加量身定制的答案?

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

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