Formatting Fields of a GridView per Row

To format data fields of a GridView on a per row basis, you need to use the RowDataBound event.

1. Simply select your GridView and create a RowDataBound event in the property.

RowDataBound Event of a GridView

Just as the description indicated, this event is fired when a row is bounded.

2. Now you can format your cells for each rows.

Example code:

if (e.Row.RowType == DataControlRowType.DataRow) //only format if it is a datarow, exclude heading etc.
{
    if (e.Row.Cells[4].Text == "T")
    {
        e.Row.Cells[4].Text = "Active"; //set field data explicitly
        ((Button)e.Row.Cells[5].Controls[0]).Text = "De-activate"; //get a button control and set its text
    }
    else
    {
        e.Row.Cells[4].Text = "Inactive"; //set field data explicitly
        ((Button)e.Row.Cells[5].Controls[0]).Text = "Activate"; //get a button control and set its text
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *