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 !






Nested Gridview with Checkbox Column


Posted Date: 20 Jun 2008    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: BharathiMember Level: Diamond    
Rating: Points: 7



In this code sample we have a Check box column in a child GridView control in ASP.NET. On clicking the textbox's header the entire row of the grid column should get selected.



using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class NestedGridView1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("<Connection String>");
conn.Open();

DataTable dt = new DataTable();
SqlDataAdapter ADA = new SqlDataAdapter("<Select Query for Parent Grid>", conn);
ADA.Fill(dt);
grd.DataSource = dt;
grd.DataBind();
}
protected void OnDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView Grd = (GridView)e.Row.FindControl("ChldGrid");
SqlConnection conn = new SqlConnection("<Connection String for Child Grid>");
conn.Open();

DataTable dt = new DataTable("TEST");
SqlDataAdapter ADA = new SqlDataAdapter("<Select Query for Child Grid> where <Column Name> = " + e.Row.Cells["<Cell Index>"].Text, conn);
ADA.Fill(dt);
Grd.DataSource = dt;
Grd.DataBind();
}
}
protected void OnChidDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
CheckBox chk = (CheckBox)e.Row.FindControl("chkHeader");
chk.Attributes.Add("onclick", "javascript:SelectAll('" + chk.ClientID + "')");
}
}
}



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NestedGridView1.aspx.cs" Inherits="NestedGridView1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<script type="text/javascript">
function SelectAll(ID)
{
var chk = document.getElementById(ID);

var chks = document.form1.elements;
for (i=0; i<chks.length; i++)
{
var type = chks[i].type;
if (type=="checkbox")
if (chk.checked)
{
chks[i].checked = true;
}
else chks[i].checked = false;
}
}
</script>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="grd" AutoGenerateColumns="false" OnRowDataBound="OnDataBound">
<Columns>
<asp:BoundField DataField="<Column Name>" HeaderText="Header Text" />
<asp:TemplateField HeaderText="Child Grid">
<ItemTemplate>
<asp:GridView runat="server" ID="ChldGrid" AutoGenerateColumns="false" OnRowDataBound="OnChidDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat='server' ID="chk" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox runat='server' ID="chkHeader" />
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Header Text" DataField="<Column Name>" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>


Attachments

  • Nested GridView Design (19019-2060-NestedGridView1.aspx.txt)
  • Code Behind File (19019-2061-NestedGridView1.aspx.cs.txt)



  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Nested Gridview with Checkbox column  .  Nested Gridview  .  Checkbox column in nested girdview  .  Checkbox column in child gridview  .  

    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: Binding XML Data to the GridView
    Previous Resource: Creating dynamic bound columns
    Return to Discussion Resource Index
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers   BizTalk Adaptors    Web Design


    Contact Us    Privacy Policy    Terms Of Use