为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;

  }

解决方案 »

  1.   

    同意 ztb(多多赚钱) .你也可在数据库中写存储过程,参数为查询条件。然在在程序中调用存储过程,这样好些。
      

  2.   

    你是用DataGrid绑定的哦,更具文本框的内容遍历DataSet,取得记录。再用代码重新生成一个dataset和DataGrid绑定。
      

  3.   

    谢谢ztb(多多赚钱),我正在看 chenghaofeng(kim) :能不能说详细点啊?另:我用了sqlDataAdapter、sqlDataConnection、dataSet这三个来绑定,有人说不用,直接在代码里写更方便,是这样吗?
      

  4.   

    搞得那么麻烦,
    你先用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);
    }
    这样就行了嘛
      

  5.   

    对了,不用sqlCommand!我搞错,不好意思