Add an Image Command Field to a GridView at runtime

If you were looking out to programmatically add an Image Command Field to a GridView, here’s how to do so:

C#

if (!Page.IsPostBack)
{
CommandField field = new CommandField();
field.ButtonType = ButtonType.Image;
field.SelectImageUrl = "~/Images/MyPic.GIF";
field.ShowSelectButton = true;
field.HeaderText = "Select";
GridView1.Columns.Add(field);
GridView1.DataBind();
}

VB.NET

If (Not Page.IsPostBack) Then
Dim
field As New CommandField()
field.ButtonType = ButtonType.Image
field.SelectImageUrl = "~/Images/MyPic.GIF"
field.ShowSelectButton = True
field.HeaderText = "Select"
GridView1.Columns.Add(field)
GridView1.DataBind()
End If
To check out some more GridView tips, look over here ASP.NET GridView Tips and Tricks

No comments:

Post a Comment