实现:

一、绑定数据库

点击查询按钮,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[];
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[];
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[];
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "name";
this.comboBox1.ValueMember = "id";
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;  //自动补全后面剩余字段
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;  //自动补全后面剩余字段
} } }
												

comboBox绑定数据库、模糊查询的更多相关文章

  1. silverlight中 ComboBox绑定数据库,并获取当前选定值

    silverlight中 ComboBox绑定数据库,并获取当前选定值 在silverlight中 用combobox下拉菜单绑定数据库的方法和用DataGrid绑定数据库的方法类似. page.xa ...

  2. 老生常谈combobox和combotree模糊查询

    FIRST /** * combobox和combotree模糊查询 * combotree 结果显示两级父节点(手动设置数量) * 键盘上下键选择叶子节点 * 键盘回车键设置文本的值 */ (fun ...

  3. python 操作mongodb数据库模糊查询

    # -*- coding: utf-8 -*-import pymongoimport refrom pymongo import MongoClient #创建连接#10.20.66.106clie ...

  4. ext.net中ComboBox空间实现模糊查询

    ComboBox中的属性添加Mode="Local"可以实现第一个字的模糊查询但是搜索中间的字无法实现 现提供一下方法使用正则表达式实现全模糊查询 <ext:ComboBox ...

  5. 2019-2-14sql server数据库模糊查询语句

    sql server数据库模糊查询语句   确切匹配: select * from hs_user where ID=123 模糊查询 select * from hs_user where ID l ...

  6. SQL Server数据库————模糊查询和聚合函数

    ***********模糊查询*********/ 关键字: like (!!!!字符串类型) in (,,)  匹配()内的某个具体值(括号里可以写多个值) between... and.. 在某两 ...

  7. WPF中ComboBox绑定数据库自动读取产生数据

    前台端 <ComboBox HorizontalAlignment="Name="cmb_SSBM" DisplayMemberPath="NAME&qu ...

  8. easyui combobox实现本地模糊查询

    直接上代码 $("#combobox1").combobox({ valueField : "value", textField : "text&qu ...

  9. combobox实现模糊查询自动填充

    利用winform设计软件界面时,经常用到combobox控件,但有时需要绑定数据表中的数据,更进一步,需要实现对数据表中数据的模糊查询功能.本文就讲讲述如何用C#实现combobox下拉列表的模糊查 ...

随机推荐

  1. Maven核心概念之依赖,聚合与继承

    一.依赖 我们项目中依赖的jar包可以通过依赖的方式(dependencies元素下添加dependency子元素)引入. <dependency> <groupId>juni ...

  2. Differences between volume, partition and drive

    A drive is a physical block disk. For example: /dev/sda. A partition A drive can be divided into som ...

  3. C# winform解决解决窗体第一次设置为最大化后,点击最大化按钮窗体无法居中问题

    public frmMain() { InitializeComponent(); //解决窗体第一次设置为最大化后,点击最大化按钮窗体无法居中问题 int x = Convert.ToInt32(( ...

  4. git123

    一:安装git和配置 1.下载 Git for windows,双击开始安装,一路采取默认选项,一路next,即可安装完毕.为了在你的电脑和github之间建立安全连接,需要SSH keys,所以你需 ...

  5. 转:The Knuth-Morris-Pratt Algorithm in my own words

    The Knuth-Morris-Pratt Algorithm in my own words For the past few days, I’ve been reading various ex ...

  6. 最大堆的插入/删除/调整/排序操作(图解+程序)(JAVA)

    堆有最大堆和最小堆之分,最大堆就是每个节点的值都>=其左右孩子(如果有的话)值的完全二叉树.最小堆便是每个节点的值都<=其左右孩子值的完全二叉树. 设有n个元素的序列{k1,k2,..., ...

  7. Java多线程同步的方法

    一 synchronized关键字 1.synchronized实现原理: ---基于对象监视器(锁) java中所有对象都自动含有单一的锁,JVM负责跟踪对象被加锁的次数.如果一个对象被解锁,其计数 ...

  8. hiho_1059_string matching content length

    题目大意 两个字符串strA和strB(长度最大为2100),他们中按照顺序有一些公共的子串,且公共子串的长度大于等于3,否则不认为是合法的,比如 abcdef 和 abcxcdef, 按照顺序有合法 ...

  9. iOS开发 差间距滚动

    CGFloat fView_Height(UIView *aView) { return aView.frame.size.height; } CGFloat fView_Width(UIView * ...

  10. ftp断点续传

    有时候ftp的文件太大了 容易断掉 使用shell下载 1 #!/bin/bash 2 cd /data2/GATK2/refSeqDB/1000genomePhase3 3 ftp -v -n 19 ...