【问题标题】:How to get first and last row in Gridview?如何在 Gridview 中获取第一行和最后一行?
【发布时间】:2020-06-21 06:44:43
【问题描述】:

我有一个gridview,它在页面加载时绑定数据:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack) 
    { 
        BindCustomerData(); 
    }
}

我想根据条件显示不同的按钮。为简单起见

如果它是第一个 gridview 行,则显示“第一条记录”

如果是最后一个 gridview 行,则显示“Last Record”

任何其他行“中间记录”

所有按钮都以标记上的Visible = False 开头。

在我的RowDataBound 活动中,我有

protected void gvCusts_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    int lastIndex = gvCusts.Rows.Count -1;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Button buttonFirst = (Button)e.Row.FindControl("buttonFirst");
        Button buttonMiddle = (Button)e.Row.FindControl("buttonMiddle");
        Button buttonLast = (Button)e.Row.FindControl("buttonLast");

        // Customer Records found
        if (c.Id > 0)
        {
            if (e.Row.RowIndex == 0)
            {
                // First row so display first button only
                buttonFirst.Visible = true;
            }
            else if (e.Row.RowIndex >= 1 && e.Row.RowIndex <= lastIndex)
            {
                // Not the first row and not the last row so display the middle button only
                buttonMiddle.Visible = true;
            }
            else if (e.Row.RowIndex == lastIndex)
            {
                // Last row, so display the last button only.
                buttonLast.Visible = true;
            }
        }
    }
}

问题是我找不到正确的方法来获取中间行和最后一行,并且按钮显示在错误的阶段。 然后我创建了 int lastIndex = gvCusts.Rows.Count -1; 代码行 static ,但仍然没有得到正确的结果。

我已经阅读了很多文章,所以很明显我在某个地方遗漏了一些东西,但不确定是什么?

【问题讨论】:

  • lastIndex 计算错误,应该来自你的数据表。

标签: c# asp.net gridview


【解决方案1】:
  1. 在您的类中声明一个静态变量 rowCount。
int rowCount=0;
  1. 在您的 BindCustomerData() 方法中,将行数传递给 rowCount 变量。
rowCount= YourDataTable.Rows.Count-1;
  1. 现在,从 rowCount 变量中获取第一个和最后一个索引。
protected void gvCusts_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    int lastIndex = rowCount;//rowCount has the last index

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Button buttonFirst = (Button)e.Row.FindControl("buttonFirst");
        Button buttonMiddle = (Button)e.Row.FindControl("buttonMiddle");
        Button buttonLast = (Button)e.Row.FindControl("buttonLast");

        // Customer Records found
        if (c.Id > 0)
        {
            if (e.Row.RowIndex == 0)
            {
                // First row so display first button only
                buttonFirst.Visible = true;
            }
            else if (e.Row.RowIndex >= 1 && e.Row.RowIndex < rowCount)
            {
                // Not the first row and not the last row so display the middle button only
                buttonMiddle.Visible = true;
            }
            else if (e.Row.RowIndex == rowCount)
            {
                // Last row, so display the last button only.
                buttonLast.Visible = true;
            }
        }
    }
}

【讨论】:

  • 谢谢 - 我刚刚意识到我在 DataBind() 之后设置了总计数,之前应该有它。
猜你喜欢
  • 2019-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
  • 2014-09-29
  • 1970-01-01
相关资源
最近更新 更多