隐藏gridview中最后一行的按钮

时间:2014-04-02 07:46:58

标签: c# asp.net .net gridview

我使用下面的代码来隐藏/编辑和删除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;
 } 

有任何建议可以克服这个问题吗?

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

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

1 个答案:

答案 0 :(得分:1)

试试这个

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