繁体   English   中英

组合框必须是自动完成的,并且只允许从列表中选择值

[英]combobox must be autocomplete and allow only seleted value from the list

我想要一个组合框功能,例如-

组合框显示项目并具有自动完成功能,因为项目列表非常大。

我希望用户能够通过在带有自动完成功能的框中键入内容来从项目列表中选择一个值-如果用户键入的内容不在列表中,则会自动选择一个可用值。

我不想将错误的文本发送到数据库。

此组合框是可编辑的,自动完成,但不接受可编辑的值,用户必须通过键入...在列表中进行选择。

private void FrmGroupCreation_Load(object sender, EventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["SaiCon"].ConnectionString;
        using (SqlConnection connect=new SqlConnection(con))
        {
            //data table for combox type of account 
            SqlDataAdapter da = new SqlDataAdapter("SELECT *FROM dbo.Type_of_Account",connect);

            DataTable dt=new DataTable();
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count ; i++)
            {
                cbTypeofAccount.Items.Add(dt.Rows[i]["type_Of_Acct"]);
            }
            //data table for combobox principle account type
            SqlDataAdapter da1 = new SqlDataAdapter("SELECT *FROM dbo.Principle_Account", connect);

            DataTable dt1 = new DataTable();
            da1.Fill(dt1);
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                cbPrinciple_account_type.Items.Add(dt1.Rows[i]["Principle_Account"]);
            }
            //data table for combobox Under the group
            SqlDataAdapter da2 = new SqlDataAdapter("SELECT *FROM dbo.Head_group_Account", connect);

            DataTable dt2 = new DataTable();
            da2.Fill(dt2);
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                cbUnder_the_group.Items.Add(dt2.Rows[i]["Account_name"]);
            }
        }

    }

您必须执行以下操作:

1-而不是执行for循环来填充组合框,而是将组合框数据源设置为等于数据表,并设置value成员和display成员

 cbTypeofAccount.DataSource = dt;
 cbTypeofAccount.DisplayMember = "type_Of_Acct";
 cbTypeofAccount.ValueMember = "your table id";

2-更改组合框的下拉样式以使其可编辑

 cbTypeofAccount.DropDownStyle = ComboBoxStyle.DropDown;

暂无
暂无

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

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