C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Adding checkox to a datagrid.


Posted Date: 15 Nov 2004    Resource Type: Articles    Category: Web Applications

Posted By: JAS       Member Level: Silver
Rating:     Points: 10



There are many instances when we require a web control like a checkbox to be added to a datagrid for performing different operations like adding or deleting records available in a datagrid(from a shopping cart or a mailbox).

This articles demonstrates how to add a checkbox to a datagrid and read whether the checkbox is checked for each of the records present in the datagrid and perform specific action on them.

Code below shows a datagrid which contains a checkbox column and a checkbox column is added to the datagrid within the TemplateColumn tag and precisely in the ItemTemplate tag of the datagrid. It is added as a normal checkbox server control.



<form id="DG_Controls_Example" method="post" runat="server">
<asp:DataGrid id="DGSample" runat="server" AllowPaging="True" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Contactname" HeaderText="Customer Name"></asp:BoundColumn>
<asp:BoundColumn DataField="CompanyName" HeaderText="Company Name"></asp:BoundColumn>
<asp:BoundColumn DataField="ContactTitle" HeaderText="Title"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkbox" Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>


In this example we have added a button to read all the records which have been selected by checking the respective boxes in each record and display the checked records.

In the code behind, we bind the data to the datagrid as shown below:

 
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
adp = new SqlDataAdapter("Select * from customers",con);
ds= new DataSet();
adp.Fill(ds);
DGSample.DataSource=ds;
DGSample.DataBind();
}
}


And in the button click event for the button, we write the code for processing the records having checked boxes.

In a loop for each item present in the datagrid, we find the check box control which we have added in the ItemTemplate tag and assign to an instance of a checkbox variable. For each of this checkbox we check whether the box is checked and if so we perform the required action on the checked records.

 
private void Button1_Click(object sender, System.EventArgs e)
{
CheckBox chkbox = new CheckBox();
TextBox txtbox= new TextBox();
foreach(DataGridItem item in DGSample.Items )
{
chkbox=(CheckBox)item.FindControl("chkbox");
txtbox=(TextBox)item.FindControl("txtbox");
if(chkbox.Checked)
{
Response.Write(item.Cells[0].Text);
// write the required code for processing the checked
// records depending on the requirement.
}

}
}




Responses

Author: Rajeev     16 Nov 2004Member Level: Silver   Points : 0
Please check Your article(Displaying CheckBox in a DataGrid)
Wether it give Full information or Not
Regards
rajeev


Author: Ravi Patil    22 Dec 2004Member Level: Bronze   Points : 0
Hi
Thank you. The explaination is verynice. Please check the code again.I think, there are some mistakes. The same code you will get from here. Please check this also.
http://support.microsoft.com/kb/321881/EN-US/
Thanks


Author: sarala    29 Sep 2008Member Level: Bronze   Points : 1
in this article....while we are executing..it is giving error like:

Error: 'System.Web.UI.WebControls.GridView' does not contain a definition for 'Items'

in the fllowing line:

foreach(DataGridItem item in DGSample.Items )

check it out..
thanks and regards,
sarala.s


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Adding CheckBox to a Datagrid  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Display total in datagrid.
Previous Resource: RSA Encryption in C#
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

conference call

Contact Us    Privacy Policy    Terms Of Use