为datagrid构建数据源dataset时要书写sql语句,把你的查询条件加进去就行了,参考代码(oledb,windows应用,对于web上的datagrid,原理一样,个别语句语法不一样):
//构建dataset,注意这里连接定义及打开和关闭是使用已经定义的OleHelper类,在你的程序中,需要自己定义
private DataSet getData(string CorpName)
{
DataSet retVal =null;
OleHelper ole = null;
string sql = "select * from 表名 where 1 = 1 ";
try
{
if (CorpName != string.Empty) {sql += " and 表里的某列 = '" + CorpName + "'"; }
string msg = string.Empty;
OleDbConnection oleConn = null;
ole = new OleHelper();
if (ole.OpenOleConnection(ref oleConn, ref msg))
{
OleDbCommand oleCommand = new OleDbCommand(sql, oleConn);
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand); retVal = new DataSet("PersonalDataSet");
oleAdapter.Fill(retVal, "PersonalData");
}
else
{
MessageBox.Show(this, msg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch(Exception ex)
{
MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (null != ole)
{
ole.Dispose();
}
return retVal;
}
//按钮Button1的click事件
private void DoSearchData(object sender, System.EventArgs e)
{
string CorpName = string.Empty;
if(this.txtCorpName.Text != null){CorpName = this.txtCorpName.Text.Trim();}
//获取datset
DataSet data = this.getData(CorpName);
//绑定数据到datagrid
if (null != data)
{
this.fillGrid(data);
}
}
//绑定datagrid
private void fillGrid(DataSet data)
{
this.personGrid.DataSource = data;
}
//构建dataset,注意这里连接定义及打开和关闭是使用已经定义的OleHelper类,在你的程序中,需要自己定义
private DataSet getData(string CorpName)
{
DataSet retVal =null;
OleHelper ole = null;
string sql = "select * from 表名 where 1 = 1 ";
try
{
if (CorpName != string.Empty) {sql += " and 表里的某列 = '" + CorpName + "'"; }
string msg = string.Empty;
OleDbConnection oleConn = null;
ole = new OleHelper();
if (ole.OpenOleConnection(ref oleConn, ref msg))
{
OleDbCommand oleCommand = new OleDbCommand(sql, oleConn);
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand); retVal = new DataSet("PersonalDataSet");
oleAdapter.Fill(retVal, "PersonalData");
}
else
{
MessageBox.Show(this, msg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch(Exception ex)
{
MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (null != ole)
{
ole.Dispose();
}
return retVal;
}
//按钮Button1的click事件
private void DoSearchData(object sender, System.EventArgs e)
{
string CorpName = string.Empty;
if(this.txtCorpName.Text != null){CorpName = this.txtCorpName.Text.Trim();}
//获取datset
DataSet data = this.getData(CorpName);
//绑定数据到datagrid
if (null != data)
{
this.fillGrid(data);
}
}
//绑定datagrid
private void fillGrid(DataSet data)
{
this.personGrid.DataSource = data;
}
你先用sqlDataAdapter、sqlDataConnection、dataSet邦定了,再添个sqlCommand;
然后
private void button1_Click_1(object sender, System.EventArgs e)
{
sqlDataAdapter1.Selecommand.CommandText="select * from 表名 where 公司名字段='"+this.文本框名.Text+"'";//注意,你得事行把sqlDataAdapter1.Selecommand.CommandText的值存在一个字符串里,方便以后恢复
dataSet11.Clear();
sqlDataAdapter1.Fill(dataSet11);
}
这样就行了嘛