gpt4 book ai didi

c# - SqlDataSource GridView 的总行数

转载 作者:行者123 更新时间:2023-12-01 21:25:27 27 4
gpt4 key购买 nike

所以我有一个 GridView 设置为 SqlDataSource。分页已启用,我有一个下拉菜单,可以更改每页显示的行数。我可以获得单个页面中显示的总行数,但我也想显示总行数。 (像这样:“显示 315 中的 1 - 25”)。

所以,我的问题是:

1) 如何获取整个数据源的总行数? (不仅仅是 GridView)我的代码 OnSelectedData 方法不起作用并返回零。

2) 如何让每个页面的数字显示不同?例如,在第二页上需要显示“显示 26 - 50,共 315 个”

这是我的代码(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();
}

}

如果有人能提供帮助那就太好了。谢谢!

最佳答案

您可以在 onSelectedData 处理程序中将分页信息设置为 Total1 文本,如下所示

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 应该像这个示例:

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

并且您不需要对 GridView1_DataBound 中的分页信息执行任何操作。

我的意思是您可以从 GridView 声明中删除 OnDataBound="GridView1_DataBound"

已编辑

要设置PageSize,您应该为其设置一个默认值,例如以下示例:

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

关于c# - SqlDataSource GridView 的总行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20432757/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com