简体   繁体   English

gridview控件和分页asp.net

[英]gridview control and paging asp.net

I have a gridview which gets populated in the backend code. 我有一个gridview填充在后端代码中。 I am trying to implement paging now, but when I am trying my way, I am getting nothing. 我现在尝试实现分页,但是当我尝试自己的方式时,什么也没得到。 Here is my piece of code: 这是我的一段代码:

public void generateTable()
{
    conn.ConnectionString = connString;
    SqlCommand comm = new SqlCommand("ViewBusinessInfo", conn);
    comm.CommandType = CommandType.StoredProcedure;
    comm.CommandTimeout = 2;
    try
    {
        conn.Open();
        SqlDataReader rdr = comm.ExecuteReader();

        if (rdr.HasRows)
        {
            gvAssociation.DataSource = rdr;
            gvAssociation.DataBind();
            gvAssociation.AllowPaging = true;
            gvAssociation.PageSize = 10;
            rdr.Close();
        }
        else
        {
            lblResult.Text = "No businesses found.";
            lblResult.Visible = true;
        }

    }
    catch
    {
    }
    finally
    {
        conn.Close();
    }
}

Can anyone advice what am I doing wrong and I can't get the paging in the gridview? 谁能告诉我我在做什么错,而我无法在gridview中获得分页? Thx in advance, Laziale 先谢谢,Laziale

The allowPaging and pagesize property of the gridview can be added in the .aspx, where the gridview tag is present. 可以在存在gridview标记的.aspx中添加gridview的allowPaging和pagesize属性。

<asp:GridView ID="gridView" OnPageIndexChanging="gridView_PageIndexChanging"  AllowPaging="True" pagesize="10" runat="server" />

Additionally, to make the paging links work, you have to add the following code in the gridview_PageIndexChanging event of the gridview: 此外,要使分页链接正常工作,必须在gridview_PageIndexChanging事件中添加以下代码:

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   gridView.PageIndex = e.NewPageIndex;
   gridView.DataBind();
}

Hope this is helpful. 希望这是有帮助的。

You cannot use paging with a DataReader. 您不能使用DataReader进行分页。 You should fill your data into a Dataset or a Datatable using a DataAdapter. 您应该使用DataAdapter将数据填充到数据集或数据表中。 Something like this: 像这样的东西:

 SqlCommand myCommand = new SqlCommand("ViewBusinessInfo", conn);
 SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand))
 DataTable dt = new DataTable();
 myAdapter.Fill(dt);
 ...

设置AllowPagingPageSize声明,或对别人打电话之前DataBind()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM