我想通过extbox输入条件,然后查询出来的值绑定到gridview里,但为什么我总是显示不出来,好像textbox值没传过去一样的,但我调试的时候,看到值是传进取了,有谁能帮我看看吗? <asp:GridView ID="GridView1" runat="server" Width="100%" height="100%" 
                AllowPaging="True" AutoGenerateColumns="False" BackColor="White" 
                BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4">
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <RowStyle BackColor="White" ForeColor="#330099" />
            <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
            </asp:GridView>using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class jishijilu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            
            bangding();
        }
    }
    public void bangding()
    {
        string myStc1 = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection conn1 = new SqlConnection(myStc1);
        conn1.Open();
        SqlCommand cmd1 = new SqlCommand("select name from work where name ='" + TextBox1.Text.Trim() + "'", conn1);
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd1;
        DataSet ds = new DataSet();
        da.Fill(ds, "work");
        GridView1.DataSource  = ds.Tables["work"].DefaultView;
        GridView1.DataBind();
        conn1.Close();
        conn1.Dispose();
    }
protected void  GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex=e.NewPageIndex;
    bangding();
}protected void Button3_Click(object sender, EventArgs e)
    {
        bangding();
    }}

解决方案 »

  1.   

    我把静态代码里gridview的数据源绑定选择无了,要从代码里去给gridview值
      

  2.   


          SqlCommand cmd1 = new SqlCommand("select name from work where name=@zhi,conn1);
    cmd1.parameters.add("@zhi",sqldbtype.varchar,50);
    cmd1.parameters["@zhi"].value=TextBox1.Text.Trim(); 
      

  3.   


    将AutoGenerateColumns="False" 改为AutoGenerateColumns="True"
    或重新定义列
      

  4.   

    为什么不使用SqlDataSource呢?一步一步操作就好了。按钮也不需要写代码,按钮的作用就是刷新页面。
      

  5.   

    string myStc1 = ConfigurationManager.AppSettings["ConnectionString"].ToString();
            SqlConnection conn1 = new SqlConnection(myStc1);
            conn1.Open();
            SqlCommand cmd1 = new SqlCommand("select name from work where name =@zhi",conn1);
            cmd1.parameters.add("@zhi",sqldbtype.varchar,50); 
            cmd1.parameters["@zhi"].value=TextBox1.Text.Trim(); 
            DataSet ds = new DataSet(da);
            SqlDataAdapter da = new SqlDataAdapter(cmd1);
            da.Fill(ds);
            GridView1.DataSource  = ds;
            GridView1.DataBind();
            conn1.Close();
            conn1.Dispose();