Monday, 24 August 2015

Nested GridView Using c#.Net In Asp.Net

8/24/2015 - By Pranav Singh 0

This article will show you how you can bind nested grid view using c#.net in asp.net. In this I have used GridView RowDataBound to bind the nested gridview.




Here is the html code of the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm22.aspx.cs" Inherits="WebApplication2.WebForm22" %>

<!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>Nested GridView Using c#.Net In Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onrowdatabound="a" Width="100%">
            <Columns>
                <asp:TemplateField HeaderText="Roll No">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("RollNo") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Subject &amp; Marks">
                    <ItemTemplate>
                        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
                            <Columns>
                                <asp:BoundField DataField="Marks" HeaderText="Marks" />
                                <asp:BoundField DataField="Subject" HeaderText="Subject" />
                            </Columns>
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

In this we will add the .cs page code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication2
{
    public partial class WebForm22 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable objDataTable = new DataTable();
                objDataTable = GetDataInDataTable();
                GridView1.DataSource = objDataTable;
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblRollNo = (Label)e.Row.FindControl("Label1");
                GridView GridView2 = (GridView)e.Row.FindControl("GridView2");
                DataTable objDataTable = new DataTable();
                objDataTable = GetDataInDataTableMarks().AsEnumerable().Where(m => m.Field<int>("RollNo") == Convert.ToInt32(lblRollNo.Text)).CopyToDataTable();
                if (objDataTable.Rows.Count > 0)
                {
                    GridView2.DataSource = objDataTable;
                    GridView2.DataBind();
                }
            }
        }
        /// <summary>
        /// Function to prepate data of dataset
        /// </summary>
        /// <returns></returns>
        protected DataTable GetDataInDataTable()
        {
            DataTable objDataTable = new DataTable();
            objDataTable.Columns.Add("RollNo", typeof(int));
            objDataTable.Columns.Add("Name", typeof(string));

            objDataTable.Rows.Add("1", "Rajesh");
            objDataTable.Rows.Add("2", "Suresh");
            objDataTable.Rows.Add("3", "Pranav");

            return objDataTable;
        }
        /// <summary>
        /// Function to prepate data of dataset
        /// </summary>
        /// <returns></returns>
        protected DataTable GetDataInDataTableMarks()
        {

            DataTable objDataTable = new DataTable();
            objDataTable.Columns.Add("RollNo", typeof(int));
            objDataTable.Columns.Add("Subject", typeof(string));
            objDataTable.Columns.Add("Marks", typeof(string));

            objDataTable.Rows.Add("1", "Maths", "89");
            objDataTable.Rows.Add("1", "Physics", "70");
            objDataTable.Rows.Add("2", "Maths", "65");
            objDataTable.Rows.Add("2", "Physics", "78");
            objDataTable.Rows.Add("3", "Maths", "99");
            objDataTable.Rows.Add("3", "Physics", "78");

            return objDataTable;
        }
    }
}

In above code I have created two data table. It will act as table. You can use your sql table. In above I have a master table of student and other is the sub table which is student marks table.
Now we have done run the application to check the output.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top