0

我是winforms的新手,我正在使用组合框。我喜欢学习如何从数据库加载到控件(组合框)以及如何将值从控件(组合框)保存到数据库字段。

下面我需要一些有关从 Combobox 获取过程并将/选择值放入组合框的帮助...但不知道该怎么做...

下面我获取列表中的所有行并绑定到组合框。这很好用,但喜欢做一些我正在努力的动作。

有人可以建议如何执行这些操作吗?

  1. 我想在组合框列表的顶部“添加”一个空值,这样他们也可以在组合框列表中选择一个空值。

  2. 从 DB 到 Control 时如何在组合框中选择一个值。我正在这样做,comboboxAccount.SeletedText = _account.Number,但它没有选择。

  3. 现在它在组合框中显示“数字”,但我喜欢显示数字+“-”描述。这该怎么做?现在它显示例如 4000 但我喜欢显示“4000 - Description1”

  4. 我喜欢将 Account.Id 存储到数据库中而不是 Account.Number 中,但是我该怎么做,因为组合框显示的是数字……所以 SelectedText 不是我想要的。

代码:

public class Account
{
    public Int32 Id { get; set; }             e.g. 1
    public string Number{ get; set; }         e.g. 4000 (alpha-numeric)
    public string Description1 { get; set; }  e.g. Customer
    public string Description2 {get; set;}    e.g. VAT account
    public string Language {get; set;}        e.g. EN 
}

  //returns all rows
  IList<Account> _account = new List<Account>(Account_repository.GetAll());
  comboboxAccount.DataSource = account;
  comboboxAccount.DisplayMember = "Number";
  comboboxAccount.Add(??); (see point 1)

  //saving to database
  Client _client = new Client();
  _client.Account = comboboxAccount.??; (see point 4)
4

2 回答 2

1

首先,我认为您应该使用 Dictionary 或 List> 或 DataTable 之类的东西。

1)你应该在你的列表中插入你的空值,然后将它绑定到你的组合框

    _account.Insert(0, "Empty");

2)定义你的ValueMember(你应该有一个列表或数据表):

如果你有一个数据表:

    comboboxAccount.ValueMember = "columnID";
    comboboxAccount.DisplayMember = "columnValue";
    comboboxAccount.DataSource = yourDataTable;
    combobox.SelectedIndex = 0; //your empty value

3)您应该在您的类中创建另一个属性并将其用作 DisplayMember;

    string NumberDescription{ get; set; };
    comboboxAccount.DisplayMember = "NumberDescription";

4)如果要存储所选值的 ID,则应使用先前定义的 ValueMember(列 ID):

    int value = Convert.ToInt32(cbomboboxAccount.SelectedValue);

    // if you want to save the text of the value selected in the combobox:
    string strValue = comboboxAccount.Text;
于 2013-04-01T09:23:16.170 回答
1

1)我想在组合框列表的顶部“添加”一个空值,这样他们也可以在组合框列表中选择一个空值。

如果设置了 DataSource 属性,则永远不能将项目直接添加到组合框项目中。因此,在将列表(数据源)设置为数据源之前,应使用 empyt/dummy 对象更新列表(数据源)。您可能必须找到一种方法来描述您当前的对象是一个虚拟对象。

例如,您可以引入 EmptyItem 属性,并在保存之前对其进行处理/过滤:

    public class Account
    {
        public Account(bool isEmptyItem =false)
        {
            this.EmptyItem = isEmptyItem;
        }

        public Int32 Id { get; set; }
        public string Number { get; set; }
        public string Description1 { get; set; }
        public string Description2 { get; set; }
        public string Language { get; set; }

        public bool EmptyItem { get; private set; } 
    }

   IList<Account> _account = new List<Account>();
   _account.Add(new Account(true))
   _account.AddRange(Account_repository.GetAll());

.

2)*如何在从 DB 到 Control 时选择组合框中的值。我正在这样做,comboboxAccount.SeletedText = _account.Number,但它没有选择。*

由于组合框绑定到一个对象,你不能设置它,SelectedText而是你应该使用SelectedItem

示例://您需要编写一个逻辑来了解如何获取此项目。

this.comboboxAccount.SelectedItem = this.comboboxAccount.Items[1];

3)现在它在组合框中显示“数字”,但我喜欢显示数字+“-”描述。这该怎么做?现在它显示例如 4000 但我喜欢显示“4000 - Description1”

您可能必须公开另一个属性说DisplayText并将其绑定到DisplayMember组合框。

示例

public class Account
{
    public string Number { get; set; }
    public string Description1 { get; set; }

    public bool EmptyItem { get; private set; }

    public string DisplayText
    {
        get{return this.Number + "-" + this.Description1}
    }
}

this.comboboxAccount.DisplayMember = "DisplayText";

4)我喜欢将 Account.Id 存储到数据库中,而不是 Account.Number,但我该怎么做,因为组合框显示的是数字......所以 SelectedText 不是我想要的。

你应该使用SelectedItem,因为你已经绑定了一个对象

var account = this.comboboxAccount.SelectedItem as Account;

var accountID = account.Id;
于 2013-04-01T09:23:56.803 回答