Thursday 13 February 2014

Nested GridView with wizard Concept in Asp.Net

Nested GridView in Asp.Net with wizard Concept........

Note: In this topic we will discuss How will show a GridView in another GridView,In this program we have make two table one for Employee Details and another for Contact Details for each Employee,contact detail show in another GridView whose present main GridView’s Template Field and if employee contact no is less than 10 digits then it will show in GrdView Cells with color…………………..

--Sql Query ......................
create database test
use test
--For First Table.............
create table Employee(EmpId varchar(50) primary key,Name varchar(50),Department varchar(50))

--For Second Table.................
create table EmpContactInfo(SrNo int identity(1,1) primary key,Work nvarchar(50),Home varchar(50),EmpId varchar(50) foreign key references Employee(EmpId) on delete cascade)

insert into Employee values('CS11','Somesh Katiyar','CS')
insert into EmpContactInfo values('9953760690','8826382483','CS11')

We take  Default.aspx Page with GridView which name GridView1 and we bind this GridView with table Employee using simple wizard concept after that we take another GridView which name GridView2 within GridView1 Template Field 

Follow this step when you bind GridView2 with table in GridView1 which is shown in this picture…………………………………..



Source Code for Default.aspx................

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

<!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></title>
<style type="text/css">
.style1 {
width: 100%;
}
</style>
</head>
<body>

<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td>
<b>   Nested GridView in Asp.Net with wizard Concept........</b></td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="EmpId" DataSourceID="SqlDataSource1"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="EmpId" HeaderText="EmpId" ReadOnly="True"
SortExpression="EmpId" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Department" HeaderText="Department"
SortExpression="Department" />
<asp:TemplateField>
<HeaderTemplate>
Contact&nbsp; Information
</HeaderTemplate>
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" onrowdatabound="GridView2_RowDataBound">
<Columns>
<asp:BoundField DataField="Work" HeaderText="Work" SortExpression="Work" />
<asp:BoundField DataField="Home" HeaderText="Home" SortExpression="Home" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:textcon %>"
SelectCommand="SELECT [Work], [Home] FROM [EmpContactInfo] WHERE ([EmpId] = @EmpId)">
<SelectParameters>
<asp:Parameter Name="EmpId" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:textcon %>"
SelectCommand="SELECT * FROM [Employee] ORDER BY [Name]">
</asp:SqlDataSource>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


Source Code for Default.aspx.cs…………………………………………..

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

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
//code for GridView1
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string empid = (string)DataBinder.Eval(e.Row.DataItem, "EmpId");
SqlDataSource sc = (SqlDataSource)e.Row.FindControl("SqlDataSource1");
sc.SelectParameters["empid"].DefaultValue = empid;
}

}
//Code for GridView2...........................
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
String wno = DataBinder.Eval(e.Row.DataItem, "Work").ToString();
if (wno.Length < 10)
// e.Row.ForeColor = System.Drawing.Color.Red;
e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
String hno = DataBinder.Eval(e.Row.DataItem, "Home").ToString();
if (hno.Length < 10)
e.Row.Cells[1].Style["color"] = "Blue";

}

}
}

Result:


0 comments:

Post a Comment