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.
Some of my previous articles are as follows: Read
XML File in Dataset And Bind To GridView In Asp.Net Using C#.Net, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net Using C#.Net,
Comment
System OR Form and Display In GridView Using C# In Asp.Net, C#
Gridview Articles, Hide
Or Show Gridview Column By Column Index at RunTime In Asp.Net Using C#.Net,
Hide
Or Show Gridview Row By Column Name at RunTime In Asp.Net Using C#.Net, Display
Grand Total In Gridview Footer On RowDataBound In Asp.Net Using C#.Net, Detect
Header Control and Assign Value of Gridview RowDataBound In Asp.Net Using
C#.Net, Merge
Header or Apply ColumnSpan to Header of GridView In Asp.Net Using C#.Net.
So for this article first I have created a new asp.net application and add nested gridview in page.
So for this article first I have created a new asp.net application and add nested gridview in page.
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 & 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.
0 comments:
Please let me know your view