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




About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: