add checkbox in gridview

add checkbox in gridview

In we need to add checkbox in gridview to select the row in gridview. To add checkbox in gridview we have to add ItemTemplate inside the asp:TemplateField. And inside ItemTemplate we can add checkbox control. We will how to add checkbox in gridview in using C#.

Example to add checkbox in gridview

<html xmlns="">
<head runat="server">
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="1px"
CellPadding="3" BorderStyle="None" Font-Names="Arial">
<PagerStyle HorizontalAlign="Left"></PagerStyle>
<HeaderStyle Font-Bold="True"></HeaderStyle>
<asp:TemplateField HeaderText="Select">
<asp:CheckBox ID="CheckBoxPurchase" runat="server" Enabled="true" />
<asp:BoundField HeaderText="Item No." DataField="ItemNo">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
<asp:BoundField HeaderText="Item Name" DataField="ItemName">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>

Below code just binds the data to gridview.

using System.Data;

namespace aspnetcontrol
    public partial class GridviewImages : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e) 
            GridView1.DataSource = BindImageData();

       public DataTable BindImageData()     
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ItemNo", typeof(int)));
            dt.Columns.Add(new DataColumn("ItemName", typeof(string)));
            // Create the four records
            DataRow dr = dt.NewRow();
            dr["ItemNo"] = 1;
            dr["ItemName"] = "Tea";
            dr = dt.NewRow();
            dr["ItemNo"] = 2;
            dr["ItemName"] = "Coffie";
            dr = dt.NewRow();
            return dt;


Post a Comment