日期:2014-05-18  浏览次数:20698 次

在DateGridView中显示查找结果
根据输入的教师名字查找该教师的信息 在DateGridView中呈现出来。在执行查找前打开这个教师列表窗体通过SqldateAdapter DateSet 已经实现将所有教师信息呈现在DateGridView中,但是查找这一步只知道怎么弄了,
C# code

         if (tBT.Text.Trim() != "")
            {
                string sql = "select TeacherId,LoginId,LoginPwd,TeacherName,Sex,Birthday from Teacher where TeacherName='"+tBT.Text.Trim()+"'";
                //dataAdapter = new SqlDataAdapter(sql, DBHelper.conn);
                //dataAdapter.Fill(ds, "Teacher");
                //ds.Select(sql) in ds.Tables["Teacher"];
            }
            else
            {
                MessageBox.Show("请输入教师姓名");
            }


请前辈们指教

------解决方案--------------------
C# code
 private void BindData()
    {
        SqlConnection sqlcon = new SqlConnection();
        try
        {
            sqlcon.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_CON"].ConnectionString;
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand();
            sqlcom.Connection = sqlcon;
            sqlcom.CommandText = "select ...";
                        sqlcom.CommandType = CommandType.Text;
            SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
            ds = new DataSet();
            sqlda.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            sqlcon.Close();
        }
    }

 protected void Page_Load(object sender, EventArgs e)
    {
     if (!IsPostBack)
        {
     BindData();
        }
}