C# 使用SqlDataAdapter分页SqlDataReader源

C# 使用SqlDataAdapter分页SqlDataReader源,c#,gridview,sqldatareader,sqldataadapter,page-index-changed,C#,Gridview,Sqldatareader,Sqldataadapter,Page Index Changed,这个问题似乎很常见,我已经讨论过了 不幸的是,我的页面仍然没有被分页。下面是我在C中的代码: 不幸的是,当我这样做时,我的分页没有显示。我做错什么了吗 谢谢在调用Databind()方法之前设置所有与分页相关的属性。使用自定义分页时,必须处理GridView1\u PageIndexChanging事件。您需要更改当前页面索引,然后像这样重新绑定GridView: void bindGridview() { SqlCommand command = new SqlCommand("(SE

这个问题似乎很常见,我已经讨论过了

不幸的是,我的页面仍然没有被分页。下面是我在C中的代码:

不幸的是,当我这样做时,我的分页没有显示。我做错什么了吗


谢谢

在调用
Databind()
方法之前设置所有与分页相关的属性。使用自定义分页时,必须处理
GridView1\u PageIndexChanging
事件。您需要更改当前页面索引,然后像这样重新绑定
GridView

void bindGridview()
{
    SqlCommand command = new SqlCommand("(SELECT ......", Connection);
    SqlDataAdapter myAdapter = new SqlDataAdapter(command);
    DataTable dt = new DataTable();
    myAdapter.Fill(dt);

    command.Connection = connection;
    command.Connection.Open();
    GridView1.AllowPaging = true;
    GridView1.PageSize = 15;
    GridView1.DataSource = dt;
    GridView1.DataBind();


    command.Connection.Close();
    command.Connection.Dispose();
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    bindGridview();
}
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        bindGridview();
}
如果您还在
页面上绑定GridView\u Load
,请执行以下操作:

void bindGridview()
{
    SqlCommand command = new SqlCommand("(SELECT ......", Connection);
    SqlDataAdapter myAdapter = new SqlDataAdapter(command);
    DataTable dt = new DataTable();
    myAdapter.Fill(dt);

    command.Connection = connection;
    command.Connection.Open();
    GridView1.AllowPaging = true;
    GridView1.PageSize = 15;
    GridView1.DataSource = dt;
    GridView1.DataBind();


    command.Connection.Close();
    command.Connection.Dispose();
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    bindGridview();
}
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        bindGridview();
}

您需要添加
GridView
PageIndexChanging
事件以启用
paging

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

您的select是否确实有超过15张唱片?另外,在调用Databind()方法之前设置所有与分页相关的属性。哇,这很简单。我只需要在databind()之前设置它。谢谢哦,OnPageIndexchange和OnPageIndexchange之间有什么区别?PageIndexchange发生在单击其中一个寻呼机按钮时,但在GridView控件处理寻呼操作之前,而PageIndexchange发生在单击其中一个寻呼机按钮时,但在GridView控件处理寻呼操作之后。我的第一个数据绑定()是在pageLoad上。但是,当我将if(!IsPostBack)添加到我的GridView1_PageIndexChanging内容中时,当我单击转到gridview的下一页时,第一次什么也没有发生。当我再次单击时,它将转到下一页。如果没有if语句,它会在第一次单击时执行此操作不要添加!我将返回到PageIndexChanging,正如我在回答中所说的那样,只是在页面加载:)我将用事件更新它以避免混淆:PAh好的,非常感谢!出于好奇,这有什么用?因为它似乎在没有压力的情况下工作!我怀疑如果没有
,索引是否可以工作!iPostback
…删除它并试用它…设置它将确保您的
GridView
pageload
期间只加载一次,而不是在
回发中加载所有内容(例如,任何单击事件都是回发
)…因此,当您单击页码时..这是一个
回发