Add an ID to the rows of a GridView table

It can be handy to have each row have an ID for use with client side scripting. By default the asp gridview does not come with ids for the rows. Use the row databound event to add them

protected void GridViewFullView_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         GridViewRow row = e.Row;
          row.Attributes["id"] = GridViewFullView.DataKeys[e.Row.RowIndex].Value.ToString();
     }
}

Leave a Reply