• comboBox绑定数据库、模糊查询


    实现:

    一、绑定数据库

    点击查询按钮,comboBox显示从数据库查到的某字段的一列数据

    方法:在按钮的点击事件绑定数据库

    private void button1_Click(object sender, EventArgs e)
            {
                using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
                {
                    con.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand())
                    {
                        cmd.Connection = con;
                        cmd.CommandText = string.Format("select * from  test t");
                        int rows = cmd.ExecuteNonQuery();
                        SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        sda.Fill(ds);
                        //con.Close();
                        DataTable dt = ds.Tables[0];
                        this.comboBox1.DataSource = dt;
                        this.comboBox1.DisplayMember="name";  //要显示的数据库中某字段的一列数据
                        this.comboBox1.ValueMember = "id";  //设置了ValueMember=‘id’
    
                    }
                }
    
    
            }

     设置了ValueMember=‘id’,然后前台页面就可以根据comboBox选中的项,获取其id,

    根据选中的comboBox的项获取其id值:string str=comboBox1.SelectedValue.ToString();

    根据选中的comboBox的项获取其文本值:string str=comboBox1.Text;

    二、模糊查询(在上面代码的基础上加两句)

    在comboBox上输入的字传给str,sql语句模糊查询这个str即可

            string str = this.comboBox1.Text;
                using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
                {
                    con.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand())
                    {
                        cmd.Connection = con;
                        cmd.CommandText = string.Format("select * from  test t where t.name like '%"+str+"%'");
                        int rows = cmd.ExecuteNonQuery();
                        SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        sda.Fill(ds);
                        //con.Close();
                        DataTable dt = ds.Tables[0];
                        this.comboBox1.DataSource = dt;
                        this.comboBox1.DisplayMember = "name";
                        this.comboBox1.ValueMember = "id";
                this.comboBox1.DroppedDown = true;  //点击查询,让comboBox下拉列表展开显示得到的结果,
                    }
                }

    三、自动补全后面剩余字段

    需要绑定数据源到Load方法里,而且打开页面就能显示所有数据,

    private void Form1_Load(object sender, EventArgs e)
            {
                using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
                {
                    con.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand())
                    {
                        cmd.Connection = con;
                        cmd.CommandText = string.Format("select * from  test t");
                        int rows = cmd.ExecuteNonQuery();
                        SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        sda.Fill(ds);
                        //con.Close();
                        DataTable dt = ds.Tables[0];
                        this.comboBox1.DataSource = dt;
                        this.comboBox1.DisplayMember = "name";
                        this.comboBox1.ValueMember = "id";
    this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;  //自动补全后面剩余字段 this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;  //自动补全后面剩余字段
    } } }
    
    
    
  • 相关阅读:
    《30天自制操作系统》17_day_学习笔记
    《30天自制操作系统》18_day_学习笔记
    湖大OJ-实验E----可判定的DFA的空问题
    湖大OJ-实验C----NFA转换为DFA
    《30天自制操作系统》16_day_学习笔记
    《30天自制操作系统》19_day_学习笔记
    《30天自制操作系统》15_day_学习笔记
    《30天自制操作系统》14_day_学习笔记
    [Leetcode Week11]Kth Largest Element in an Array
    [Leetcode Week10]Minimum Time Difference
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/6006680.html
Copyright © 2020-2023  润新知