Search code examples
c#asp.netgridviewrowdatabound

Hide gridview cell (value) base on other cell value


I have a Gridview with ItemTemplate using some Labels and 3 Button (Deactivate, Delete and Edit) as seen in picture below:

enter image description here

I want to hide these buttons from some users base on their username (Label UserName on Gridview), for example:

  • If UserName == "some string" then hide the Deactivate, Delete and Edit buttons

How do I do this in code behind RowDataBound event?


Solution

  • Yes by using Gridview RowDataBound event you can do that

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