隐藏网格视图中最后一行的按钮

本文关键字:一行 按钮 网格 视图 最后 隐藏 | 更新日期: 2023-09-27 17:56:11

我正在使用下面的代码来隐藏/编辑和删除gridview的最后一行。

当我使用页面索引时,按钮隐藏在每个页面的最后一行?

 protected void Page_PreRender(object sender, EventArgs e)
 {
    var lastRow = gvDetails.Rows[gvDetails.Rows.Count - 1];
    lastRow.FindControl("btnEdit").Visible = false;
    lastRow.FindControl("btnDelete").Visible = false;
 } 

有什么建议可以克服这个问题吗?

此外,我还使用以下方法来绑定网格并添加一个具有列总和的新数据行。现在如何将此数据行添加为网格视图的第一行。

public void LoadData()
{
    DataTable dtDetails = DetailsDataRepository.GetDetails();
    BindDetailsGrid(dtDetails);
    DataTable dt = DetailsDataRepository.GetSum();
    DataRow dr = dtDetails.NewRow();
    dr["Total_Amount"] = Convert.ToDecimal(dt.Rows[0]["Total_Amount"].ToString().Substring(1)).ToString("C0");
    dr["Monthly_Amount"] = Convert.ToDecimal(dt.Rows[0]["Monthly_Amount"].ToString().Substring(1)).ToString("C0");
    dtDetails.Rows.InsertAt(dr,0);
    gvDetails.DataSource = dtDetails;
    gvDetails.DataBind();
    if (gvDetails.PageIndex == 0)
    {
        var lastRow = gvDetails.Rows[0];
        lastRow.FindControl("btnEdit").Visible = false;
        lastRow.FindControl("btnDelete").Visible = false;
        lastRow.FindControl("lblComments").Visible = false;
        lastRow.Height = 35;
        lastRow.Font.Size = 11;
        lastRow.Font.Bold = true;
    }        
}

页面索引:

protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvDetails.PageIndex = e.NewPageIndex;
    BindGrid();
    if (gvDetails.PageIndex == 0)
    {
        var lastRow = gvODetails.Rows[0];
        lastRow.FindControl("btnEdit").Visible = false;
        lastRow.FindControl("btnDelete").Visible = false;
        lastRow.FindControl("lblComments").Visible = false;
        lastRow.Height = 35;
        lastRow.Font.Size = 11;
        lastRow.Font.Bold = true;
    }
}

更新:

public void LoadData()
{
    DataTable dtDetails = DetailsDataRepository.GetDetails();
    BindDetailsGrid(dtDetails);
    DataTable dt = DetailsDataRepository.GetSum();
    DataRow dr = dtDetails.NewRow();
    dr["Total_Amount"] = Convert.ToDecimal(dt.Rows[0]["Total_Amount"].ToString().Substring(1)).ToString("C0");
    dr["Monthly_Amount"] = Convert.ToDecimal(dt.Rows[0]["Monthly_Amount"].ToString().Substring(1)).ToString("C0");
    dtDetails.Rows.InsertAt(dr, 0);
    gvDetails.DataSource = dtDetails;
    gvDetails.DataBind();
    if (gvDetails.PageIndex == 0)
    {
        var lastRow = gvDetails.Rows[0];
        lastRow.FindControl("btnEdit").Visible = false;
        lastRow.FindControl("btnDelete").Visible = false;
        lastRow.FindControl("lblComments").Visible = false;
        lastRow.Height = 35;
        lastRow.Font.Size = 11;
        lastRow.Font.Bold = true;
        lastRow.BackColor = System.Drawing.Color.LightGray;
    }  
}

页面索引:

protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
 gvDetails.PageIndex = e.NewPageIndex;
 BindGrid();
 if (gvDetails.PageIndex == 0)
  {
    var lastRow = gvDetails.Rows[0];
    lastRow.FindControl("btnEdit").Visible = false;
    lastRow.FindControl("btnDelete").Visible = false;
    lastRow.FindControl("lblComments").Visible = false;
    lastRow.Height = 35;
    lastRow.Font.Size = 11;
    lastRow.Font.Bold = true;
  }
}

隐藏网格视图中最后一行的按钮

试试这个

if(gvOBMDetails.PageCount == 1)//Updated
{
    var lastRow = gvOBMDetails.Rows[gvOBMDetails.Rows.Count - 1];
    lastRow.FindControl("btnEdit").Visible = false;
    lastRow.FindControl("btnDelete").Visible = false;
}
else if(gvOBMDetails.PageIndex == gvOBMDetails.PageCount -1)
{
    var lastRow = gvOBMDetails.Rows[gvOBMDetails.Rows.Count - 1];
    lastRow.FindControl("btnEdit").Visible = false;
    lastRow.FindControl("btnDelete").Visible = false;
}