根据另一个单元格值隐藏gridview单元格(值)

Hide gridview cell (value) base on other cell value

我有一个带有 ItemTemplate 使用一些标签和 3 个按钮(停用、删除和编辑)的 Gridview,如下图所示:

我想根据用户名(Gridview 上的标签用户名)对某些用户隐藏这些按钮,例如:

如何在 RowDataBound 事件背后的代码中执行此操作?

是的,通过使用 Gridview RowDataBound 事件你可以做到这一点

protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
        Label lbl=e.Row.FindControl("your cotrol Id")as Label;
       if(lbl!=null && lbl.Text.Trim()=="some string")
       {
           e.Row.FindControl("deactivate btn Id").Visible = false;
           e.Row.FindControl("delete btn Id").Visible = false;
           e.Row.FindControl("edit btn Id").Visible = false;
       }
   }
 }

您可以通过列索引获取行数据,在 RowDataBound 事件中您需要检查它是否是数据行,而不是页眉或页脚..等等

if(e.Row.RowType == DataControlRowType.DataRow)
{
  if(e.Row.Cells[2].Text = "some string")
  {
    Button delete = (Button)e.Row.FindControl("control id to hide");
    delete.Visisble = false;
  }
}