Cannot convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.CommandField

I have a datagrid and I am trying to toggle visibility on a button based on data retrieved (Code below)
but I recieve the error on page build.  What am I missing?

 protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           if (GridView3.DataKeys[e.Row.RowIndex].Value == "1")
           {
                CommandField objComField =(CommandField)e.Row.Cells[0].Controls[0];
                objComField.Visible = false;
           }
        }
    }
mbartAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
boro_bobConnect With a Mentor Commented:
You are trying to cast a Control as a ControlField, which the compiler obviously doesn't like.

Try
Control objControl =(Control)e.Row.Cells[0].Controls[0];
 objControl.Visible = false;
0
 
mbartAuthor Commented:
Worked like a charm!
0
All Courses

From novice to tech pro — start learning today.