private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string ACconnStr="server=XCH;uid=sa;pwd=;database=aaa";//定义数据库连接字符串
SqlConnection ACconn=new SqlConnection(ACconnStr); //创建连接对像
      SqlCommand ACcomm=new SqlCommand(ACsql,ACconn);//创建Command对像
try
{
ACconn.Open();//打开连接
mysql = "Select id,name,sex,class,tel ,email from Table1";
//设置SQL语句,即查询数据库中所有内容
mycmd.Fill(dt);
//填充数据,即在内存中生成DataSet模型数据库
DataGrid1.DataSource = dt.Tables(0);
//为DataGrid1控件指定数据源
DataGrid1.DataBind();
//执行绑定
}
catch 
{
         Response.Write("程序出错,信息描述如下:<br>" & ex.Message);
          Finally
ACconn.Close();
End Try;
End Sub;
}
}private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Handles DataGrid1.PageIndexChanged;
DataGrid1.CurrentPageIndex = e.NewPageIndex;
getdata();}
帮我改改 我刚学2天 谢谢啦

解决方案 »

  1.   

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
           DataGrid1.PageIndex   =   e.NewPageIndex; 
           getdata(); 
        }
      

  2.   

       protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
        { 
          DataGrid1.PageIndex  =  e.NewPageIndex; 
          getdata(); 
        }
       public void getdata()
    {
    string   ACconnStr= "server=XCH;uid=sa;pwd=;database=aaa ";//定义数据库连接字符串 
    SqlConnection   ACconn=new   SqlConnection(ACconnStr);   //创建连接对像 
                SqlCommand   ACcomm=new   SqlCommand(ACsql,ACconn);//创建Command对像 
    try 

    ACconn.Open();//打开连接 
    mysql   =   "Select   id,name,sex,class,tel   ,email   from   Table1 "; 
    //设置SQL语句,即查询数据库中所有内容 
    mycmd.Fill(dt); 
    //填充数据,即在内存中生成DataSet模型数据库 
    DataGrid1.DataSource   =   dt.Tables(0); 
    //为DataGrid1控件指定数据源 
    DataGrid1.DataBind(); 
    }
      

  3.   

    dt 得提前声明: DataSet dt = new DataSet();
    其他的没什么问题;
      

  4.   

    我用SQL数据库 .net为2003版本 
      

  5.   

    private   void   Page_Load(object   sender,   System.EventArgs   e) 


    public void getdata() 

    string  ACconnStr= "server=XCH;uid=sa;pwd=;database=aaa ";
    SqlConnection  ACconn=new  SqlConnection(ACconnStr);  
     SqlCommand  ACcomm=new  SqlCommand(ACsql,ACconn);
    try 

    ACconn.Open();
    mysql  =  "Select  id,name,sex,class,tel  ,email  from  Table1 "; 
    SqlDataAdapter sda = new SqlDataAdapter(s_strsql, conn);
    DataSet Dts_Job = new DataSet();
    sda.Fill(Dts_Job, "Table1");
    DataGrid1.DataSource  =  Dts_Job.Tables(0); DataGrid1.DataBind(); 
    }  protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
        { 
          DataGrid1.PageIndex  =  e.NewPageIndex; 
          getdata(); 
        } 
      

  6.   

    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.Sql;
    using System.Data.SqlClient;
    using System.Text;public partial class Infomation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            getdata(); 
        }
    public void getdata() 

    string  ACconnStr= "server=XCH;uid=sa;pwd=;database=aaa "; 
    SqlConnection  ACconn=new  SqlConnection(ACconnStr);  
    SqlCommand  ACcomm=new  SqlCommand(ACsql,ACconn); 
    try 

    ACconn.Open(); 
    mysql  =  "Select  id,name,sex,class,tel  ,email  from  Table1 "; 
    SqlDataAdapter sda = new SqlDataAdapter(s_strsql, conn); 
    DataSet Dts_Job = new DataSet(); 
    sda.Fill(Dts_Job, "Table1"); 
    DataGrid1.DataSource  =  Dts_Job.Tables(0); DataGrid1.DataBind(); 
    }   protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
        { 
          DataGrid1.PageIndex  =  e.NewPageIndex; 
          getdata(); 
        } 
    }
    直接覆盖吧……
      

  7.   


    private   void   Page_Load(object   sender,   System.EventArgs   e) 

          string   ACconnStr= "server=XCH;uid=sa;pwd=;database=aaa ";
          SqlConnection   ACconn=new   SqlConnection(ACconnStr);
          SqlCommand   ACcomm=new   SqlCommand(ACsql,ACconn);
         DataTable dt = new DataTable();
         try 
        { 
             ACconn.Open(); 
              mysql   =   "Select   id,name,sex,class,tel   ,email   from   Table1 "; 
              mycmd.Fill(dt); 
              DataGrid1.DataSource   =   dt.Tables(0); 
              DataGrid1.DataBind(); 
         } 
        catch   
        { 
             Response.Write( "程序出错,信息描述如下: <br> "   &   ex.Message); 
             Finally 
             ACconn.Close(); 
             End   Try; 
             End   Sub; 
        } 
    } private   void   DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e) 

         DataGrid1.CurrentPageIndex   =   e.NewPageIndex; 
         getdata(); 

      

  8.   

    这是前台
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 144px; POSITION: absolute; TOP: 112px"
    runat="server" Width="331px" Height="152px" PageSize="5" AllowPaging="True" BorderColor="#CCCCCC"
    BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3">
    <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
    <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
    <ItemStyle ForeColor="#000066"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"></HeaderStyle>
    <PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
    </asp:DataGrid>
      

  9.   

    把你后台贴出来 所有的(包括上面的using
      

  10.   

    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.Sql; 
    using System.Data.SqlClient; 
    using System.Text; namespace WebApplication7
    {
    /// <summary>
    /// WebForm2 的摘要说明。
    /// </summary>
    public class WebForm2 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    getdata(); 
    }
    public void getdata() 

    string  ACconnStr= "server=XCH;uid=sa;pwd=;database=aaa "; 
    SqlConnection  ACconn=new  SqlConnection(ACconnStr);  
    SqlCommand  ACcomm=new  SqlCommand(ACsql,ACconn); 
    try 

    ACconn.Open(); 
    mysql  =  "Select  id,name,sex,class,tel  ,email  from  Table1 "; 
    SqlDataAdapter sda = new SqlDataAdapter(s_strsql, conn); 
    DataSet Dts_Job = new DataSet(); 
    sda.Fill(Dts_Job, "Table1"); 
    DataGrid1.DataSource  =  Dts_Job.Tables(0);  DataGrid1.DataBind(); 
    }
    catch{}
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 

    DataGrid1.PageIndex  =  e.NewPageIndex; 
    getdata(); 
    }  }
    }
      

  11.   

    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.Sql; 
    using System.Data.SqlClient; 
    using System.Text; namespace WebApplication7 

    public class WebForm2 : System.Web.UI.Page 

    protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load(object sender, System.EventArgs e) 

    getdata(); 

    public void getdata() 

    string  ACconnStr= "server=XCH;uid=sa;pwd=;database=aaa "; 
    SqlConnection  ACconn=new  SqlConnection(ACconnStr);  ACconn.Open(); 
    string mysql  =  "Select  id,name,sex,class,tel  ,email  from  Table1 "; 
    SqlDataAdapter sda = new SqlDataAdapter(mysql, ACconn); 
    DataSet Dts_Job = new DataSet(); 
    sda.Fill(Dts_Job, "Table1"); 
    DataGrid1.DataSource  =  Dts_Job;DataGrid1.DataBind(); 
    } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 

    DataGrid1.PageIndex  =  e.NewPageIndex; 
    getdata(); 
    } } 
    }
      

  12.   

    "protected System.Web.UI.WebControls.DataGrid DataGrid1;"去掉。 
      

  13.   

      private void Page_Load(object sender, System.EventArgs e)
      {
        DataGrid1.DataSource = GetSql();
        DataGrid1.DataBind();
      }
      protected DataTable  GetSql()
      {
        string sqlConnStr = "server=XCH;uid=sa;pwd=;database=aaa ";//定义数据库连接字符串 
        string strSql = "select [id],[name],[sex],[class],[tel],[email] from Table1";
        DataTable aTable = new DataTable();
        using (SqlConnection sqlConn = new SqlConnection(sqlConnStr))//using 自动会关闭连接
        {
          using (SqlCommand sqlComm = new SqlCommand(strSql, sqlConn))
          {
            try
            {
              sqlConn.Open();
              SqlDataAdapter sda = new SqlDataAdapter(sqlComm);
              sda.Fill(aTable);
            }
            catch (SqlException e)
            {
              throw new Exception(string.Format("程序出错,信息描述如下: <br> {0}", e.Message));
            }
          }
        }
        return aTable;
      }  protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
      {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        GetSql();
      }
      

  14.   

    事件 DataGrid1_SelectedIndexChanged  改成  DataGrid1_PageIndexChanged
    这才是分页。  从新点击一下事件。
      

  15.   

    报错 WebParts不存在
    Sql在System.Data不存在
    找不到类型或命名空间
    GridViewPageEventArgs
      

  16.   

    webparts那一行引用那个去掉 那是我的项目要用到的 我给贴上去了 不好意思把system.data.sql也删了
      

  17.   

    “System.Web.UI.WebControls.DataGrid”并不包含对“PageIndex”的定义
      

  18.   

    项目不大的话 打包发我邮箱 帮你调试 [email protected]