我数据绑定什么都没问题,为什么运行后不显示gridView控件里面的数据???我没有在page_load事件里面
它就是不显示.

解决方案 »

  1.   

    不显示,先检查sql语句写的有问题么、看看数据库本身有数据库没?报错的话把错误贴出来!
      

  2.   

    using System;
    using System.Data;
    using System.Configuration;
    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 _Default : System.Web.UI.Page 
    {
        public SqlConnection GetConn()
        {
            string connStr = ConfigurationManager.AppSettings["ConnStr"].ToString();
            SqlConnection myConn = new SqlConnection(connStr);
            return myConn;
        }
        public void bind()
        {
            SqlConnection conn = GetConn();
            conn.Open();
            string sqlStr = "select * from YG_xxb";
            SqlDataAdapter sda = new SqlDataAdapter(sqlStr,conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataKeyNames = new string[] {"YG_ID"};
            GridView1.DataBind();
            sda.Dispose();
            ds.Dispose();
            conn.Close();
        }
          protected void  GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            this.bind();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            this.bind();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int YG_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            string C_name =((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();        string sqlStr = "update YG_xxb set YG_name='"+C_name+"'where YG_ID="+YG_ID;
            SqlConnection myConn = GetConn();
            myConn.Open();
            SqlCommand cmd = new SqlCommand(sqlStr,myConn);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            myConn.Close();
            GridView1.EditIndex = -1;
            this.bind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {    }
    }
      

  3.   

    大哥你绑定没 都没调用bind() 方法
      

  4.   

    我不要在page_load 里面显示 
      

  5.   

    好像有两个错误,
    第一:没有调用bind()方法。
    第二:public void bind() 
        { 
            SqlConnection conn = GetConn(); 
            conn.Open(); 
            string sqlStr = "select * from YG_xxb"; 
            SqlDataAdapter sda = new SqlDataAdapter(sqlStr,conn); 
            DataSet ds = new DataSet(); 
            sda.Fill(ds); 
            GridView1.DataSource = ds.Tables[0]; 
            GridView1.DataKeyNames = new string[] {"YG_ID"}; 
            GridView1.DataBind(); 
            sda.Dispose(); 
            ds.Dispose(); 
            conn.Close(); 
        } 
      

  6.   

    using System; 
    using System.Data; 
    using System.Configuration; 
    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 _Default : System.Web.UI.Page 

        public SqlConnection GetConn() 
        { 
            string connStr = ConfigurationManager.AppSettings["ConnStr"].ToString(); 
            SqlConnection myConn = new SqlConnection(connStr); 
            return myConn; 
        } 
        public void bind() 
        { 
            SqlConnection conn = GetConn(); 
            conn.Open(); 
            string sqlStr = "select * from YG_xxb"; 
            SqlDataAdapter sda = new SqlDataAdapter(sqlStr,conn); 
            DataSet ds = new DataSet(); 
            sda.Fill(ds); 
            GridView1.DataSource = ds; 
            GridView1.DataKeyNames = new string[] {"YG_ID"}; 
            GridView1.DataBind(); 
            sda.Dispose(); 
            ds.Dispose(); 
            conn.Close(); 
        } 
          protected void  GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
        { 
            GridView1.EditIndex = -1; 
            this.bind(); 
        } 
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
        { 
            GridView1.EditIndex = e.NewEditIndex; 
            this.bind(); 
        } 
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
        { 
            int YG_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()); 
            string C_name =((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();         string sqlStr = "update YG_xxb set YG_name='"+C_name+"'where YG_ID="+YG_ID; 
            SqlConnection myConn = GetConn(); 
            myConn.Open(); 
            SqlCommand cmd = new SqlCommand(sqlStr,myConn); 
            cmd.ExecuteNonQuery(); 
            cmd.Dispose(); 
            myConn.Close(); 
            GridView1.EditIndex = -1; 
            this.bind(); 
        } 
        protected void Page_Load(object sender, EventArgs e) 
        { 
    [size=14px]          if(!IsPostback)
                {
                  this.bind();    //这里要绑定
                }
    [/size]
        } 
      

  7.   

    也就是说,必须要在page_load事件中绑定才能显示控件 数据吗?