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 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }
    protected void BindData()
    {
        string str = "server=localhost;database=Xkk;user=xiaopang;pwd=123456";
        SqlConnection conn = new SqlConnection(str);
        SqlDataAdapter ad = new SqlDataAdapter("select * from Student", conn);
        DataSet myds = new DataSet();
        ad.Fill(myds, "Student");
        DataGrid1.DataSource = myds.Tables["Student"].DefaultView;
        //DataGrid1.DataBind();
    }
    protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
    {
        //编辑
        DataGrid1.EditItemIndex = e.Item.ItemIndex;
        BindData();
    }
    protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
    {
        //取消
        DataGrid1.EditItemIndex = -1;
        BindData();
    }
    //protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
    //{
    //    //删除
    //    if (DataGrid1.Items.Count == 1)
    //    {
    //        if (DataGrid1.CurrentPageIndex != 0)
    //            DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1;
    //    }
    //    string strSql = "delete from Student where StuNo="+e.Item.Cells[0].Text+"";
    //    ExecuteSql(strSql);
    //    BindData();
    //}
    protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        //分页
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        BindData();
    }
    protected void ExecuteSql(string strSql)
    {
      try
      {
         string strconn ="server=localhost;database=Xkk;user=xiaopang;pwd=123456";
 SqlConnection conn1 =new SqlConnection(strconn);
 SqlCommand com = new SqlCommand(strSql,conn1);
 conn1.Open();
 com.ExecuteNonQuery();
 conn1.Close();
      }
        catch(Exception e)
      {
           Response.Write("<script language=javascript>alert('"+e.Message+"')</script>");
      }
    }
   
}
   数据库中有数据,运行也能,没报错,但是,没数据!!!

解决方案 »

  1.   

     protected void BindData() 
        { 
            string str = "server=localhost;database=Xkk;user=xiaopang;pwd=123456"; 
            SqlConnection conn = new SqlConnection(str); 
            SqlDataAdapter ad = new SqlDataAdapter("select * from Student", conn); 
            DataSet myds = new DataSet(); 
            ad.Fill(myds, "Student"); 
            DataGrid1.DataSource = myds.Tables["Student"].DefaultView; 
            //DataGrid1.DataBind();   //给注释掉了
        } 
      

  2.   

    //DataGrid1.DataBind();   不应该注释掉
      

  3.   

    //DataGrid1.DataBind(); 
    这句话应该要的吧!不像WINFORM!
      

  4.   

    //DataGrid1.DataBind(); 
    给注释掉了。asp.net里面这一句不能少。
      

  5.   

      DataGrid1.DataBind();不应该注释··另外  protected void Page_Load(object sender, EventArgs e) 
        { 
          if(!IsPostBack)
            {
              BindData(); 
            }
        }