gpt4 book ai didi

c# - 隐藏 gridview 中最后一行的按钮

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:42 24 4
gpt4 key购买 nike

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

有什么解决这个问题的建议吗?

我还使用下面的方法来绑定(bind)网格并添加一个包含列总和的新数据行。现在如何将此数据行添加为 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;
}
}

最佳答案

试试这个

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

关于c# - 隐藏 gridview 中最后一行的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22804744/

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