要实现这样的功能,SQL语句里面就不要用LIKE,不然就不是你希望的那种结果了
最好是用SUBSTRING
比如要查“A”开头的,SQL就是
SELECT * FROM Menu WHERE SUBSTRING(Coda,1,LEN('A'))='A'
比如要查“ABC”开头的,SQL就是
SELECT * FROM Menu WHERE SUBSTRING(Coda,1,LEN('ABC'))='ABC'怎么样组合SQL语句你应该会了,我就不说了,^_^
最好是用SUBSTRING
比如要查“A”开头的,SQL就是
SELECT * FROM Menu WHERE SUBSTRING(Coda,1,LEN('A'))='A'
比如要查“ABC”开头的,SQL就是
SELECT * FROM Menu WHERE SUBSTRING(Coda,1,LEN('ABC'))='ABC'怎么样组合SQL语句你应该会了,我就不说了,^_^
DataView dv=new DataView();
dv.Table=ds.Tables["TableName"];
将dv作为数据源绑定到DataGrid,在TextBox的KeyPress中:
dv.RowFilter=@"codastr like '*"+textBox.Text.Trim()+@"'*";
你要的话给我个地址,我把代码发给你。
我去看看。[email protected]
[email protected]或QQ16168903
this.TextBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TextBox1_KeyPress);
//在这里显示datagrid1中的数据
private void txtAge_KeyPress(object sender,System.Windows.Forms.KeyPressEventArgs e)
{
if((e.KeyChar<48||e.KeyChar>57)&&e.KeyChar!=8)//如果输入的是数字
{
……
string sql = "Select * from Menu where Coda like '" + textBox.Text + "%'";
……
}
else
{
MessageBox.Show("你输入的必须是数字!");
}}
其它的就不用我说了。
string sql = "Select * from Menu where Coda like '" + textBox.Text + "%'";
如果在DataSet中,
stirng condition = "字段名1 like '"+textBox.Text.Trim()+"%'";ds.Tables["TableName"].DefaultView.RowFilter = condition;这里假设DataGrid绑定的为ds.Tables["TableName"]
如果允许关键字不在开头,可以写成
string sql = "Select * from Menu where Coda like '%" + textBox.Text + "%'";
stirng condition = "字段名1 like '%"+textBox.Text.Trim()+"%'";