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 !






How Dyanmically bind a child gridview to the parent Grid.


Posted Date: 05 Jun 2008    Resource Type: Code Snippets    Category: ASP.NET GridView

Posted By: Bharathi       Member Level: Diamond
Rating:     Points: 15



The following code sample shows how Dyanmically bind a child gridview to the parent Grid.

Design:


<div>
<asp:GridView runat="server" ID="Grd" AutoGenerateColumns="false" OnRowDataBound="OnDataBound">
<Columns>
<asp:BoundField DataField="<FieldName>" HeaderText="<Header Text>" />
<asp:TemplateField HeaderText ="Agent Details">
<ItemTemplate>
<asp:GridView ID="ChldGrid" runat="server">
<Columns>
<asp:BoundField DataField="<FieldName>" HeaderText="<Header Text>" />
<asp:BoundField DataField="<FieldName>" HeaderText="<Header Text>" />
<asp:BoundField DataField="<FieldName>" HeaderText="<Header Text>" />
<asp:BoundField DataField="<FieldName>" HeaderText="<Header Text>" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>



Code Behind:



protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
try
{
SqlConnection conn = new SqlConnection("<Connection String>");
conn.Open();

//SqlCommand Comm = new SqlCommand("Select top 10 * from advent_client_info");
DataTable dt = new DataTable();
SqlDataAdapter ADA = new SqlDataAdapter("<Select Statement of Parent Grid>", conn);
ADA.Fill(dt);
Grd.DataSource = dt;
Grd.DataBind();
}
catch (Exception E)
{
string str = E.Message;
}
}
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>");
conn.Open();

DataTable dt = new DataTable();
SqlDataAdapter ADA = new SqlDataAdapter("Select statement of Child grid with where condition", conn);
ADA.Fill(dt);
Grd.DataSource = dt;
Grd.DataBind();
}
}


Attachments

  • Nested GridView. (18120-566-Nested GridView.txt)
  • Nested GridView. (18120-567-Nested GridView.txt)
  • Nested GridView. Dyanmically binding a child gridview to the parent. (18120-5643-Nested GridView.txt)



  • Responses

    Author: Athira Appukuttan    13 Jun 2008Member Level: Diamond   Points : 0
    Check this Attachement


    NestedGridview.zip
    Author: Bharathi    13 Jun 2008Member Level: Diamond   Points : 2
    Athira,
    Thanks.
    Can you please send me the SnapShots for the grid you have sent?
    And I think, the file you have given is specific to your project. Please send a common code which would be helpfull . ie., a sample data and not fetching from database.


    Author: Athira Appukuttan    13 Jun 2008Member Level: Diamond   Points : 1
    Ok Bharathi,

    I will send u common function.but u have change connection string in web.config file.




    Common Fuctions.zip
    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Nestedgridview  .  GridView Nested  .  

    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: Dynamically Add Controls to Grid.
    Previous Resource: How to clear a datagridview
    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

    doors in nj

    Contact Us    Privacy Policy    Terms Of Use