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 !




Child DataGrid


Posted Date: 04 Aug 2006    Resource Type: Articles    Category: .NET Framework
Author: Pritam BaldotaMember Level: Gold    
Rating: Points: 10



<H2>Introduction</H2>
Child DataGrid in Parent Data Grid

Child datagrid is useful in daily life. It is the concept to display datagrid in datagrid. You have to just put a datagrid in item template of Datagrid and on ItemCreated event of parent datagrid assign the datasource to child datarid.



<%@ Page language="c#" Codebehind="ChildDataGrid.aspx.cs" AutoEventWireup="false" Inherits="Practicals.ChildDataGrid" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ChildDataGrid</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid ID="dgParents" runat="server" CellPadding="1" BorderWidth="0px" AutoGenerateColumns="False"
Height="136px" Width="480px">
<Columns>
<asp:BoundColumn DataField="CategoryID" HeaderText="Category ID">
<ItemStyle VerticalAlign="Top"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="CategoryName" HeaderText="Category Name">
<ItemStyle VerticalAlign="Top"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Show">
<ItemStyle VerticalAlign="Top"></ItemStyle>
<ItemTemplate>
<asp:Button id="btnShow" runat="server" Text="Show Child" CommandName="Show"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Children Categories">
<ItemStyle VerticalAlign="Top"></ItemStyle>
<ItemTemplate>
<asp:DataGrid id="dgChildren" runat="server" AutoGenerateColumns="False" BorderWidth="0" CellPadding="0"
Width="234px" Visible="False">
<HeaderStyle BackColor="#FFFFC0"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="ProductID" HeaderText="Product ID "></asp:BoundColumn>
<asp:BoundColumn DataField="ProductName" HeaderText="Product Name"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>





using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace Practicals
{
/// <summary>
/// Summary description for Child DataGrid to display chaild .
/// </summary>
public class ChildDataGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgParents;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
BindGrid();

}
private void BindGrid()
{
SqlConnection SqlCon1 = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=lemonc");

SqlCommand SqlCom1 = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories", SqlCon1);

if (SqlCon1.State != ConnectionState.Open)

{

SqlCon1.Open();

}

dgParents.DataSource = SqlCom1.ExecuteReader();

dgParents.DataBind();

SqlCon1.Close();


}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor a nd.
/// </summary>
private void InitializeComponent()
{
this.dgParents.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgParents_ItemCommand);
this.dgParents.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgParents_ItemDataBound);
this.dgParents.SelectedIndexChanged += new System.EventHandler(this.dgParents_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgParents_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

{
SqlConnection SqlCon2 = new SqlConnection("Data Source=localhost; Initial Catalog=Northwind; Persist Security Info=True;User ID=sa;Password=lemonc");

SqlCommand SqlCom2;

int CatID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "CategoryId"));



System.Web.UI.WebControls.DataGrid dgChil=(System.Web.UI.WebControls.DataGrid)e.Item.FindControl("dgChildren");


Button bt=(Button)e.Item.FindControl("btnShow");
bt.Attributes.Add("onclick","javascript:Disp(this)");



SqlCom2 = new SqlCommand("SELECT ProductID, PRoductName FROM Products WHERE CategoryId = " + CatID, SqlCon2);

if (SqlCon2.State != ConnectionState.Open)

{

SqlCon2.Open();

}

dgChil.DataSource = SqlCom2.ExecuteReader();
dgChil.DataBind();
SqlCon2.Close();

}


}

private void dgParents_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="Show")
{
System.Web.UI.WebControls.DataGrid dgChil=(System.Web.UI.WebControls.DataGrid)e.Item.FindControl("dgChildren");

if(dgChil.Visible==true)
{
dgChil.Visible=false;

}
else
{
dgChil.Visible=true;
}
}
}

private void dgParents_SelectedIndexChanged(object sender, System.EventArgs e)
{

}


}
}






Responses

Author: samita r jengathe    04 Aug 2006Member Level: Bronze   Points : 0
YA this article is really nice one. It explain all the concepts regarding DataGrid in datagrid .But it is not complete one.
Bcoz here javascript file is used which is not explain here.

Plz Try to specify detail info regarding Article.

Thanks


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Child 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: In which scenario we will use interface and an abstract class.
Previous Resource: Using Stack in c#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use