Wednesday, 21 March 2012

Adding control to gridview cell dynamically

A control can be added to a gridview using the rowdatabound event.

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Image img = new Image();
        img.ImageUrl = "~/images/logo.png";

        TableCell tcell = new TableCell();
        tcell.Controls.Add(img);
 
        //The control will be added to second cell of the gridview.
        e.Row.Cells[2].Controls.Add(img); 
    }
}

No comments:

Post a Comment