总行数的SqlDataSource的GridView

总行数的SqlDataSource的GridView

本文介绍了总行数的SqlDataSource的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个GridView设立一个SqlDataSource。寻呼已启用,我有一个下拉菜单,改变每页显示的行数。我能够得到总在一个单一页面显示的行,但我想要显示的行总数为好。 (这样的:显示1 - 25的315)。

So I have a GridView set up to a SqlDataSource. Paging is enabled and I have a drop down menu that changes the number of rows displayed per page. I am able get the total of rows displayed in a single page but I want to show the total number of rows as well. (Like this: "Showing 1 - 25 of 315").

所以,我的问题是:

1)如何获得行的总数为整个数据源? (不只是GridView控件)的code我有,OnSelectedData方法,不能正常工作,并返回一个零。

1) How do I get the total number of rows for the entire DataSource? (not just GridView) The code I have, OnSelectedData method, does not work and returns a zero.

2)如何获得为每个页面显示不同的数字?例如,在第二页上它需要说显示26 - 50 315

2) How do I get the numbers to display differently for each page? For example, on the second page it needs to say "Showing 26 - 50 of 315"

下面是我的code(C#):

Here's my code (C#):

public partial class UserControls_BloombergAllUsersControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
int gridViewTotalRowCount;
protected void onSelectedData(object sender, SqlDataSourceStatusEventArgs e)
{
    gridViewTotalRowCount = e.AffectedRows;
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
    int pageRowsCount = GridView1.Rows.Count;

    Total1.Text = "Showing 1 - " + pageRowsCount.ToString() + " of " + gridViewTotalRowCount.ToString();
}
private void BindGridView1()
{
    try
    {
        this.GridView1.DataBind();

        if (Convert.ToInt32(DropDownList1.SelectedValue) != null)
            GridView1.PageSize = Convert.ToInt32(DropDownList1.SelectedValue);
    }
    catch (Exception ex)
    { throw ex; }
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    BindGridView1();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    BindGridView1();
}

}

如果有人可以帮助将是巨大的。谢谢!

If anyone could help that would be great. Thanks!

推荐答案

您可以寻呼信息设置为页共文本 onSelectedData 处理程序的以下

you could set paging information to Total1 text in onSelectedData handler as the following

protected void onSelectedData(object sender, SqlDataSourceStatusEventArgs e)
{
    int startRowOnPage = (GridView1.PageIndex * GridView1.PageSize) + 1;
    int lastRowOnPage = startRowOnPage + GridView1.Rows.Count - 1;
    int totalRows = e.AffectedRows;

    Total1.Text = "Showing " + startRowOnPage.ToString() +
                  " - " + lastRowOnPage + " of " + totalRows;
}

所以,你的SqlDataSource应该是这样的例子:

So you SQLDataSource should be like this sample:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
     OnSelected="onSelectedData"
     ... >
</asp:SqlDataSource>

而你并不需要做的寻呼信息在任何 GridView1_DataBound

我的意思是,你可以删除 OnDataBound =GridView1_DataBound从你的GridView的声明。

I mean you could remove OnDataBound="GridView1_DataBound" out from your GridView declaration.

EDITED

要设置每页,你应该为它的默认值,如下面的示例:

To set the PageSize, you should set a default value for it such as the following sample:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Convert.ToInt32(DropDownList1.SelectedValue) != null)
            GridView1.PageSize = Convert.ToInt32(DropDownList1.SelectedValue);
    }
}

这篇关于总行数的SqlDataSource的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:14