在下边的事件中
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
bindnew();
}
bindnew()为函数,重新绑定数据源,须自己写,和Page_Load你在中的一样

解决方案 »

  1.   

    <asp:datagrid id="DataGrid1" style="Z-INDEX: 106; LEFT: 13px; POSITION: absolute; TOP: 292px" runat="server" BorderStyle="Solid" OnSortCommand="sortrows" AllowSorting="True" OnPageIndexChanged="changegridpage" PagerStyle-HorizontalAlign="Right" PageSize="5" AllowPaging="True" >
    public void changegridpage(object sender, DataGridPageChangedEventArgs objargs)
    {
    //此处是datagrid分页响应过程
    DataGrid1.CurrentPageIndex = objargs.NewPageIndex;
    binddatagrid();
    }
    public void sortrows(object sender, DataGridSortCommandEventArgs objargs)
    {
    //此处是datagrid排序响应过程
    sortorder = objargs.SortExpression.ToString();
    binddatagrid();
    }
    public void binddatagrid()
    {
    //此处是gatagrid的数据绑定过程
    DataGrid1.PageSize = 5;
    DataGrid1.PagerStyle.Mode = PagerMode.NumericPages;
    string varsql = "select * from telrecord";
    myconnclass myclass =new conndbdll.myconnclass();
    string mytable="mytable";
    DataSet mydataset=myclass.DataAdapter(varsql,mytable);
    DataView mydataview = new DataView(mydataset.Tables["mytable"]);
    //mydataview.Sort = sortorder;
    DataGrid1.DataSource = mydataview;
    DataGrid1.DataBind();
    }
      

  2.   

    when you change a page,you need set datagrid's currentpageindex and bind it again
    datagrid1.currentpageindex=i;
    datagrid1.datasource=ds;
    datagrid1.databind;
      

  3.   

    响应DataGrid的PageIndexChanged事件
    private void dgResult_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    this.dgResult.CurrentPageIndex = e.NewPageIndex;
    DataGrid重新绑定代码
    }
      

  4.   

    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="PagedGrid.aspx.vb" Inherits="DataGridSamples.PagedGrid"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title></title>
    <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 21px; POSITION: absolute; TOP: 19px" runat="server" Width="480px" Height="133px" BorderStyle="None" BorderWidth="1px" BorderColor="#CC9966" BackColor="White" CellPadding="4" PageSize="3" AllowPaging="True">
    <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
    <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
    <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC" Mode="NumericPages"></PagerStyle>
    <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
    <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
    </asp:DataGrid>
    </form>
    </body>
    </HTML>Imports System
    Imports System.Data
    Imports System.Data.SqlClientPublic Class PagedGrid
        Inherits System.Web.UI.Page
        Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid#Region " Web Form Designer Generated Code "    'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()    End Sub    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub#End Region    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not Page.IsPostBack Then
                BindGrid()
            End If
        End Sub    Private Sub BindGrid()
            Dim cnn As SqlConnection
            Dim da As SqlDataAdapter
            Dim ds As New DataSet()        cnn = New SqlConnection(ConfigurationSettings.AppSettings().Item("ConnectionString"))
            da = New SqlDataAdapter("select lastname,firstname,city from employees", cnn)
            da.Fill(ds, "employees")        DataGrid1.DataSource = ds
            DataGrid1.DataMember = "employees"
            DataGrid1.DataBind()    End Sub    Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexChanged    End Sub    Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
            DataGrid1.CurrentPageIndex = e.NewPageIndex
            BindGrid()
        End Sub
    End Class