作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有一个 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/
如何在表格底部做一行求和(TreeTableView - JavaFX 或 TableView)? (对不起我的英语不好)请写一个例子。 以图片(总计)为例 http://i.stack.imgur.
我是一名优秀的程序员,十分优秀!