[英]Filter and search DataGridView connected to a MS Access database in C#
I use this code to filter and search a DataGridView
connected to a MS Access database in C#, but when I enter anything into the textbox, every row of data in the DataGridView
disappears - any help? 我使用此代码来筛选和搜索连接到C#中MS Access数据库的DataGridView
,但是当我在文本框中输入任何内容时, DataGridView
中的每一行数据都会消失-有帮助吗?
This code to appear in datagridview 这段代码出现在datagridview中
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AMR\Desktop\ABBagain-Copy.accdb;
Persist Security Info=False;";
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select * from Query1";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt;
dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
da.Fill(dscontects);
connection.Close();
and this in textbox 这在文本框中
DataView dv = new DataView(dt);
dv.RowFilter = string.Format("SerialNumber LIKE '%{0}%'",textBox1.Text);
table1DataGridView.DataSource = dv;
Have you tried replacing % with *? 您是否尝试过将%替换为*? so your code would look like dv.RowFilter = string.Format("SerialNumber LIKE '*{0}*'",textBox1.Text);
因此您的代码应类似于dv.RowFilter = string.Format("SerialNumber LIKE '*{0}*'",textBox1.Text);
the reason i say this is because access SQL wildcard character is * and not % 我说这的原因是因为访问SQL通配符是*而不是%
also im assuming you have the textbox code on a text_changed event? 我还假设您在text_changed事件上具有文本框代码? I would consider changing that to be something that would hit the database a little less frequently. 我会考虑将其更改为使数据库访问频率降低的某种方式。 maybe have a button or even textbox exit event. 可能有一个按钮,甚至有文本框退出事件。
我使用以下解决方案修复了该问题:
string query = "select * from RecordsSheet where [SerialNumber] like('" + textBox1.Text + "%')";
put all this code inside textbox keyup and make some changes like database and field name 将所有这些代码放入文本框keyup并进行一些更改,例如数据库和字段名称
string strProvider="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Gym.accdb"; 字符串strProvider =“ Provider = Microsoft.ACE.OLEDB.12.0; Data Source = Gym.accdb”;
string strSql = "Select * from cust";
OleDbConnection con = new OleDbConnection(strProvider);
OleDbCommand cmd = new OleDbCommand(strSql, con);
con.Open();
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable scores = new DataTable();
da.Fill(scores);
dglist.DataSource = scores;
DataView dv = new DataView(scores);
dv.RowFilter = string.Format("name LIKE '%{0}%'", txtsearch.Text);
dglist.DataSource = dv;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.