SIGN UP MEMBER LOGIN:    
ARTICLE

CheckBox in Gridview

Posted by Syed Shakeer Articles | ASP.NET Controls in C# June 17, 2010
In this article you will know how to use the CheckBox inside Gridivew control and how to select the multiple checkboxes to get the selected CheckBoxes value will be displayed.
Reader Level:

In this article you will know how to use the CheckBox inside Gridivew control and how to select the multiple checkboxes to get the selected CheckBoxes value will be displayed. 

First Drag and Drop one Gridview Control and Button on asp Page. Later retrieve the data from a table on Gridview using bound field and add on CheckBox field in .aspx source code page as shown below

<asp:GridView id="Gridview1" runat ="server" AutoGenerateColumns ="False" ShowFooter ="True"  DataSourceID ="SqlDataSource1" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged" style="z-index: 101; left: 19px; position: absolute; top: 142px" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" Width="240px" >
<Columns >
<asp:TemplateField >
<ItemTemplate >
<asp:CheckBox ID="chk" runat ="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField  DataField ="ID" HeaderText ="ID" />
<asp:BoundField DataField="empname" HeaderText ="Name" />
<asp:BoundField DataField ="salary" HeaderText ="Salary" />
</Columns>
    <FooterStyle BackColor="Tan" />
    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
    <HeaderStyle BackColor="Tan" BorderColor="Fuchsia" BorderStyle="Solid" Font-Bold="True" />
    <AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT [ID], [empname], [salary] FROM [emp]">
</asp:SqlDataSource>
        
The output of above code is below: 

1.gif
 
Now I am writing code in GetValues Button to retrive values when the user selects one or more CheckBoxes.

protected void GetValues_Click(object sender, EventArgs e)
{
    for (int i = 0; i < Gridview1.Rows.Count; i++)
    {
        CheckBox chkb = (CheckBox)Gridview1.Rows[i].Cells[0].FindControl("chk");
        if (chkb.Checked)
        {
            string name = Gridview1.Rows[i].Cells[2].Text;
            Response.Write("<br>" + name);
        }
    }
}
 
Click on GetValues Button after selecting one or more CheckBoxes, then you will get values that you had checked in gridview.

The output is show below:

2.gif

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

Dear Hussain,
Can you advise me how to add check box column using Code. I have existing code as per below which I create temparary table and then add to grid each item by codeing. Can you give any help?

private void CreateGrid(){

GridHandle objTmpTab = new GridHandle();
grvItem.Columns.Clear();
grvItem.Columns.Add(objTmpTab.GridViewBoundField(
"SEQNO", DbType.String, "SeqNo", "Tahoma", FontSize.Small, HorizontalAlign.Left, 45));......}


Relvent settings Code to add:

public BoundField GridViewBoundField(string pFieldName, DbType pFieldType,string pHeaderText,string pFontName, FontSize pFontSize,HorizontalAlign pAllingment,int pWidth)

{BoundField objBondField = new BoundField();
objBondField.DataField = pFieldName; //"DATAFIELDNAME"
objBondField.HeaderText = pHeaderText;//HEADER NAME
.....}

Posted by Channa Edirisinghe Aug 23, 2010

Hi Chuck.snder,
First Thank you.
for your problem check the following link.
http://forums.asp.net/p/1133301/1802924.aspx
you will get your soluiton..

Thank you

Posted by Syed Shakeer Jul 03, 2010

Great example, now could you extend the example a little.  There are many cases where the check box needs to be edited, and the data is taken from sql which only has a "Bit" value which is 0/1 for true and false.

For the data to be Read/Write "Bind" must be used instead of Eval.

do you have an example of using the Bind with the checkbox???

Posted by chuck.snyder Jul 02, 2010
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor