12

我创造了:

  • 一个母版页和一个称为详细信息的内容页。
  • 在按钮单击事件上,在网格视图中显示数据。
  • 在网格视图中,列是自动生成的。
  • 我想在网格视图中显示 11 列,但它超过了页面大小。

为此该怎么办?

我已经为数据库连接代码创建了 sql 帮助文件并调用了该方法,而不是使用 sqldatasource 进行连接。

当我尝试进行分页时,出现错误

GridView 'GridView1' 触发了未处理的事件 PageIndexChanging。

4

5 回答 5

24

您需要在处理 PageIndexChanging 事件的代码上声明一个方法。

与此类似的东西:

protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs  e)
{
    GridView1.PageIndex = e.NewPageIndex;
    bindGridView(); //bindgridview will get the data source and bind it again
}

private void bindGridView()
{
     GridView1.DataSource=getData();
     GridView1.DataBind();
}

提供示例代码:

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bindGridView(); //bindgridview will get the data source and bind it again
    }

    protected void Page_Load(object sender , EventArgs e)
    {
        if(!IsPostBack)
         bindGridView();

    }
    //this is some sample data 
    private void bindGridView()
    {
        DataTable t = new DataTable();
        t.Columns.Add("Col1");
        t.Columns.Add("Col2");
        DataRow r = null;
        for (int i = 0; i < 25; i++)
        {
            r = t.NewRow();
            r.ItemArray = new object[] { "Val" + i, " Another " + i };
            t.Rows.Add(r);
        }
        GridView1.DataSource = t;
        GridView1.DataBind();
    }

这是标记:

<asp:GridView OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" PageSize="10" ID="GridView1" runat="server" AutoGenerateColumns="true">

产生这个:

在此处输入图像描述

于 2011-10-30T15:24:48.730 回答
2

对于分页,您可以为此使用 OnPageIndexChanging ....

例如

您必须在 GridView 中使用OnPageIndexChanging="gvdetails_PageIndexChanging" ...

您必须将下面的代码写入事件后面的代码中,例如

protected void gvdetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvdetails.PageIndex = e.NewPageIndex;
    BindData();
}

有关更多详细信息,您可以在此处查看以下链接我在我的文章中使用页面索引更改...

这里我使用 PageIndexChange

我希望这会对你有所帮助....与他人分享...谢谢!

于 2013-06-27T07:32:20.970 回答
1

这是最终的答案:

Imports System.Collections.Generic ' library

Protected Sub grdEmployees_PageIndexChanging1(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdEmployees.PageIndexChanging
    grdEmployees.PageIndex = e.NewPageIndex
    LoadEmployeeList() 'FUNCTION FOR DATASET
    grdEmployees.DataBind()

End Sub
于 2012-07-31T17:44:50.500 回答
1

您只需将其添加到您的代码中:

protected void GridViewTrsEmail_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridViewTrsEmail.PageIndex = e.NewPageIndex;
    GridViewTrsEmail.DataBind();


}
于 2017-10-27T08:47:00.840 回答
0

为了解决这个问题,我必须仔细查看我的数据源和数据键。我有一组从 SQL Server 返回的记录,我正在做的是将它们绑定到 POCO。这个类有几个整数类型的公共属性。这些整数是我在网格上的数据键。我用字符串替换了它们的类型,而不是绕过转换问题。

于 2012-02-22T21:10:55.390 回答