Wednesday, February 18, 2015

How To Add A CheckBox Column to GridView in ASP.Net

Posted by Gnani Palepu  |  No comments


 How To Add A CheckBox Column to GridView in ASP.Net  
 ------------------------------------------------------  
 ready each and every line   
 for example we have a existing data gridview like ashow below  
 Let’s get started. We assume that we have a GridView with existing set of columns getting populated with some data. So, we have the below definition of GridView in our ASPX page.  
 existing data grid  
 ----------------------  
 <asp:gridview ID="grdData" runat="server" AutoGenerateColumns="False" CellPadding="4" CssClass="grdData"  
       ForeColor="#333333" GridLines="None" Width="200">  
       <alternatingrowstyle BackColor="White" ForeColor="#284775"></alternatingrowstyle>  
       <columns>  
         <asp:boundfield DataField="ID" HeaderText="ID"></asp:boundfield>  
         <asp:boundfield DataField="Name" HeaderText="Name"></asp:boundfield>  
       </columns>  
       <editrowstyle BackColor="#999999"></editrowstyle>  
       <footerstyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></footerstyle>  
       <headerstyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></headerstyle>  
       <pagerstyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"></pagerstyle>  
       <rowstyle BackColor="#F7F6F3" ForeColor="#333333"></rowstyle>  
       <selectedrowstyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"></selectedrowstyle>  
       <sortedascendingcellstyle BackColor="#E9E7E2"></sortedascendingcellstyle>  
       <sortedascendingheaderstyle BackColor="#506C8C"></sortedascendingheaderstyle>  
       <sorteddescendingcellstyle BackColor="#FFFDF8"></sorteddescendingcellstyle>  
       <sorteddescendingheaderstyle BackColor="#6F8DAE"></sorteddescendingheaderstyle>  
 </asp:gridview>  
 And we have the below code to populate the GridView with some dummy data.  
     private void LoadGridData()  
     {  
       DataTable dt = new DataTable();  
       dt.Columns.Add("Id");  
       dt.Columns.Add("Name");  
       for (int i = 0; i < 10; i++)  
       {  
         DataRow dr = dt.NewRow();  
         dr["Id"] = i + 1;  
         dr["Name"] = "Student " + (i + 1);  
         dt.Rows.Add(dr);  
       }  
       grdData.DataSource = dt;  
       grdData.DataBind();  
     }  
 now  
 ------------------------------------------------  
 simple just add this  
 Now, we will be adding a CheckBox column to this Grid. For doing this, we will make use of TemplateField and define a new TemplateField column in the existing set of columns as below –  
   <asp:templatefield HeaderText="Select">  
     <itemtemplate>  
       <asp:checkbox ID="cbSelect" CssClass="gridCB" runat="server"></asp:checkbox>  
     </itemtemplate>  
   </asp:templatefield>  
 Your final GridView definition will appear like below –  
 --------------------------------------------------------  
 <asp:gridview ID="grdData" runat="server" AutoGenerateColumns="False" CellPadding="4" CssClass="grdData" ForeColor="#333333" GridLines="None" Width="200">  
       <alternatingrowstyle BackColor="White" ForeColor="#284775"></alternatingrowstyle>  
       <columns>  
         <asp:templatefield HeaderText="Select">  
           <itemtemplate>  
             <asp:checkbox ID="cbSelect" CssClass="gridCB" runat="server"></asp:checkbox>  
           </itemtemplate>  
         </asp:templatefield>  
         <asp:boundfield DataField="ID" HeaderText="ID"></asp:boundfield>  
         <asp:boundfield DataField="Name" HeaderText="Name"></asp:boundfield>  
       </columns>  
       <editrowstyle BackColor="#999999"></editrowstyle>  
       <footerstyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></footerstyle>  
       <headerstyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></headerstyle>  
       <pagerstyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"></pagerstyle>  
       <rowstyle BackColor="#F7F6F3" ForeColor="#333333"></rowstyle>  
       <selectedrowstyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"></selectedrowstyle>  
       <sortedascendingcellstyle BackColor="#E9E7E2"></sortedascendingcellstyle>  
       <sortedascendingheaderstyle BackColor="#506C8C"></sortedascendingheaderstyle>  
       <sorteddescendingcellstyle BackColor="#FFFDF8"></sorteddescendingcellstyle>  
       <sorteddescendingheaderstyle BackColor="#6F8DAE"></sorteddescendingheaderstyle>  
 </asp:gridview>  

2:25 AM Share:

0 comments:

Get updates in your email box
Complete the form below, and we'll send you the best coupons.

Deliver via FeedBurner
Proudly Powered by Blogger.
back to top