数据库列表值

时间:2016-03-26 04:25:25

标签: c#

我用C#MVC模式开发了一个简单的系统,在模型控制器中我有一个员工列表。加载表单时,我想将员工列表添加到文本框中,用户可以通过输入员工的代码和名称来过滤员工详细信息。现在我不知道如何将员工列表值添加到文本框自动完成客户来源,请帮助..

模型控制器

    public List<Employee> Findlist()
    {
        List<Employee> emp = new List<Employee>();
        string query = "select RTRIM(code) as [Code],RTRIM(name) as [Name] from m_Employee";

        SqlCommand cmd = new SqlCommand(query);
        cmd.Connection = con;

        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            Employee e = new Employee();
            e.code = dr["code"].ToString();
            e.name = dr["name"].ToString();

            emp.Add(e);
        }

        dr.Close();
        return emp; 
    }

控制器

public List<Employee> SearchEmp()
{
        EmployeeModel md = new EmployeeModel();
        return md.Findlist();
}

查看

    private void FrmEmployeeSearchBar_Load(object sender, EventArgs e)
    {
        try
        {
            LoginControll lc = new LoginControll();
            List<Employee> emp = new List<Employee>();

            emp = lc.SearchEmp();

            AutoCompleteStringCollection collection = new AutoCompleteStringCollection();

            textBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
            textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            for (int i = 0; i < emp.Count; i++)
            {
                string result = Convert.ToString(emp[i].name);
                //collection.Add(emp[i].code);
                collection.Add(result);
            }

            textBox1.AutoCompleteCustomSource = collection;
            //txtAutocompletesearcbar.au
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
        }
}

1 个答案:

答案 0 :(得分:0)

添加以下行

 textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
 textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;

textBox1.AutoCompleteCustomSource = collection;