Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取值并将值放入combobox_C#_Winforms_Combobox - Fatal编程技术网

C# 获取值并将值放入combobox

C# 获取值并将值放入combobox,c#,winforms,combobox,C#,Winforms,Combobox,我是winforms的新手,我使用的是组合框。我喜欢学习如何从数据库加载到控件(组合框),以及如何将值从控件(组合框)保存到数据库字段 下面我需要一些关于从Combobox获取和将/选择值放入Combobox的过程的帮助。。。但我不知道怎么做 下面我得到列表中的所有行并绑定到一个组合框。这很好,但我喜欢做一些我正在努力的动作 有人能建议如何做这些行动吗 我想在comboboxList的顶部添加一个空值,这样他们也可以在ComboxList中选择一个空值 如何在从DB放入控件时在combobox中

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

下面我需要一些关于从Combobox获取和将/选择值放入Combobox的过程的帮助。。。但我不知道怎么做

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

有人能建议如何做这些行动吗

  • 我想在comboboxList的顶部添加一个空值,这样他们也可以在ComboxList中选择一个空值

  • 如何在从DB放入控件时在combobox中选择值。。我是这样做的 comboboxAccount.SeletedText=\u account.Number,但它没有选择

  • 现在它在组合框中显示“Number”,但我喜欢显示Number+“-”描述。如何做到这一点?现在它显示在例如4000中,但我喜欢显示“4000-说明1”

  • 我喜欢将Account.Id存储到数据库中,而不是Account.Number,但是如何才能做到这一点,因为组合框显示的是数字。。。所以SelectedText不是我想要的

  • 代码:

    公共类帐户
    {
    public Int32 Id{get;set;}例如1
    公共字符串编号{get;set;}例如4000(字母数字)
    公共字符串描述1{get;set;}例如客户
    公共字符串描述2{get;set;}例如增值税账户
    公共字符串语言{get;set;}例如EN
    }
    //返回所有行
    IList_account=新列表(account_repository.GetAll());
    comboboxAccount.DataSource=帐户;
    comboboxAccount.DisplayMember=“Number”;
    comboboxAccount.Add(??);(见第1点)
    //保存到数据库
    客户端_Client=新客户端();
    _client.Account=comboboxAccount。??;(见第4点)
    
    首先,我认为应该使用字典、列表或数据表之类的工具

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

        _account.Insert(0, "Empty");
    
    2) 还要定义ValueMember(您应该有一个列表或数据表):

    如果您有一个DataTable:

        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):

    1) 我想在comboboxList的顶部添加一个空值,这样他们就可以 在comboboxlist中选择一个空值

    如果设置了DataSource属性,则永远不能将项直接添加到comobbox项中。 因此,在将列表(数据源)设置为数据源之前,应该使用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());
    
    3) 现在它在组合框中显示“Number”,但我喜欢显示Number+“-” 描述如何做到这一点?现在是4000,但我喜欢 显示“4000-说明1”

    您可能需要公开另一个属性,比如
    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;
    

    这是
    \u帐户。插入(0,“空”)Account
    object上可以使用code>,您可以让List accountList=new List{new Account(){//初始化您的属性,如ID=int、Description=“string”}、new Account(){//等等……};账户acc=新账户();资产=价值;会计清单。插入(0,acc)@梅苏特:这有助于解决你的问题吗?你有什么悬而未决的问题吗?谢谢你。这非常有用。。正在为问题2而挣扎(一整天):例如Account.ID=26存储在数据库中。但现在,当页面加载时,如何在组合框中选择Account.ID=26记录?这是一个列表,因此intellisence会很好地提示所有属性。我已经设置了“comboboxAccount.ValueMember=“Id”;“comboboxAccount.DisplayMember=“DisplayText”;好啊我得到了这个,现在它工作了,非常感谢你。this.comboboxAccount.SelectedValue=Account.ID;所以它工作得很好。
    this.comboboxAccount.SelectedItem = this.comboboxAccount.Items[1];
    
    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";
    
    var account = this.comboboxAccount.SelectedItem as Account;
    
    var accountID = account.Id;