This has been troubling me for the past few hours and after some hardcore Googling returning nothing I think this is something that would benefit the developer community. The situation was as follows. I wanted to be able to check if a user had met a certain condition and based on the returned result, I would either make the Delete or Select button visible, while making the other equal to false. To do this I found that RowDataBound came in quite handy. Check out the code below and see if it helps you with future GridView development. There will be some tweaking that needs to be done based on what Text you give the buttons and where they are positioned. Let us know if you need help or have any recommendations in the comments below. Until the next piece of useful code, Happy Hacking!

 protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow) {
            if (CONDITION_TO_MEET_IF_TRUE)
            {
                // if true turn of the control we are looking for by text below
                int lastControl = e.Row.Cells[1].Controls.Count - 1;
                LinkButton b = (LinkButton)e.Row.Cells[1].Controls[lastControl];
                if (b.Text == "TEXT_TO_SEARCH_FOR") b.Visible = false;
            }
            else
            {
                // else turn off the other control we are looking for
                // through a manual search
                LinkButton b = (LinkButton)e.Row.Cells[1].Controls[2];
                if (b.Text == "TEXT_TO_SEARCH_FOR") b.Visible = false;
            }
        }


    }
Share This