Total Row Count SqlDataSource GridView

谁都会走 提交于 2019-12-24 03:34:13

问题


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").

So, my questions are:

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) 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"

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!


回答1:


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;
}

So you SQLDataSource should be like this sample:

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

And you don't need to do anything for paging information in GridView1_DataBound.

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);
    }
}



回答2:


Since you are using SqlDataSource, try this code:

Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEvent Args) Handles
SqlDataSource1.Selected

  Dim cnt As Integer = e.AffectedRows

End Sub


来源:https://stackoverflow.com/questions/20432757/total-row-count-sqldatasource-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!