Gridview changing text in a button column

Bob Avallone picture Bob Avallone · Jun 17, 2010 · Viewed 9.7k times · Source

I am working in asp.net using the gridview control. I have a button column that I create dynamically as such:

            ButtonField bfSelect = new ButtonField();
            bfSelect.HeaderText = "View";
            bfSelect.ButtonType = ButtonType.Link;
            bfSelect.CommandName = "View";
            bfSelect.Text = "View";

            grdAttachments.Columns.Add(bfSelect);

The text on the button is the same for every row. I was wondering if there was any way to have the text be different for different rows depending on a condition. When I try to look at the text property of a particular row it is blank, if I try to set it, it does not change.

Thanks in advance.

Bob

Answer

Kris van der Mast picture Kris van der Mast · Jun 17, 2010

in the RowDataBound event you can check the current row. For example:

String text = ((Label)e.Row.FindControl("aLabel")).Text;
if(text == "Yes")
{
    ((LinkButton)e.Row.FindControl("bfSelect").Text = "Good to go!";
} 

For this you also need to set the ID of the button to bfSelect.

Grz, Kris.