Gridview Inside a GridView in ASP.net 2.0

Steps:

1) create the simple web application (asp.net with C#).

Description :

here we create the application which display the student information with result. the student got marks out of 100.

if student get less then 40 then fail(indicating with Red Row). if user got above 70 in each subject then distinction

(indicating with Green Row).

you can use this application as Master Detail Record. Master Is Student Table and Detail is the StudentMarks Table.

gridingrid

2) create the two tables student and studentmarks.

2.1) student

Field Name Data Types
StudentId int (Primary Key,Auto Increment)
Name varchar(50)
Age varchar(50)
Address1 varchar(50)
Address2 varchar(50)
Phone varchar(50)
Email varchar(50)

Data :

Record 1:

INSERT INTO Student
(Name, Age, Address1, Address2, Phone, Email)
VALUES

(‘Amit’, ’21’, ‘a’, ‘a’, ‘99999’, ‘amit@yahoo.com’)

StudentId =1

Record 2:

INSERT INTO Student
(Name, Age, Address1, Address2, Phone, Email)
VALUES (‘Test’, ’30’, ‘a’, ‘a’, ‘99999’, ‘test@yahoo.com’)

StudentId =2

Record 3:

INSERT INTO Student
(Name, Age, Address1, Address2, Phone, Email)
VALUES (‘Manish’, ’35’, ‘ad’, ‘ad’, ‘213234’, ‘Manish@yahoo.com’)

StudentId =3

2.2)studentmarks

Field Name Data Types
StudentMarkId int(Primary Key,AutoIncrement)
StudentID int
Phy decimal(6,2)
Chemistry decimal(6,2)
Biology decimal(6,2)
Math decimal(6,2)
Total decimal(7,2)

Data :

Record 1:

insert into StudentMarks
(studentId,phy,chemistry,biology,math,total)
values
(1,40,40,60,50,190)

Record 2:

insert into StudentMarks
(studentId,phy,chemistry,biology,math,total)
values
(2,60,25,60,40,185)

Record 3:

insert into StudentMarks
(studentId,phy,chemistry,biology,math,total)
values
(3,70,70,70,90,300)

3) put below code in to .aspx file.

Code Snippet
 <asp:GridViewID="GridView1"runat="server"Width="322px"Font-Bold="True"ForeColor="#333333" CellPadding="4"GridLines="None"AutoGenerateColumns="False"OnRowDataBound="GridView1_RowDataBound"> <FooterStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/> <RowStyleBackColor="#EFF3FB"/> <PagerStyleBackColor="#2461BF"ForeColor="White"HorizontalAlign="Center"/> <SelectedRowStyleBackColor="#D1DDF1"Font-Bold="True"ForeColor="#333333"/> <HeaderStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/> <EditRowStyleBackColor="#2461BF"/> <AlternatingRowStyleBackColor="White"/> <Columns> <asp:TemplateFieldVisible="false"> <ItemTemplate> <asp:LabelID="lblStudentId"Text='hlight7 <%ht0 # Eval("StudentId") ht7 %>hlight0 'runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateFieldHeaderText="Student Name"> <ItemTemplate> <asp:LabelID="lblStudentName"runat="server"Text='hlight7 <%ht0 # Eval("Name") ht7 %>hlight0 '></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateFieldHeaderText="Age"> <ItemTemplate> <asp:LabelID="lblAge"runat="server"Text='hlight7 <%ht0 # Eval("Age") ht7 %>hlight0 '></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateFieldHeaderText="Address1"> <ItemTemplate> <asp:LabelID="lblAddress1"runat="server"Text='hlight7 <%ht0 # Eval("Address1") ht7 %>hlight0 '></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateFieldHeaderText="Address2"> <ItemTemplate> <asp:LabelID="lblAddress2"runat="server"Text='hlight7 <%ht0 # Eval("Address2") ht7 %>hlight0 '></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateFieldHeaderText="Phone"> <ItemTemplate> <asp:LabelID="lblPhone"runat="server"Text='hlight7 <%ht0 # Eval("Phone") ht7 %>hlight0 '></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateFieldHeaderText="Email"> <ItemTemplate> <asp:LabelID="lblEmail"runat="server"Text='hlight7 <%ht0 # Eval("Email") ht7 %>hlight0 '></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateFieldHeaderText="Result Information"> <ItemTemplate> <asp:GridViewID="gvResult"runat="server"AutoGenerateColumns="false"OnRowDataBound="gvResult_RowDataBound"> <Columns> <asp:TemplateFieldHeaderText="Physic"> <ItemTemplate> <asp:LabelID="lblPhy"runat="server"Text='hlight7 <%ht0 # Eval("Phy") ht7 %>hlight0 '></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateFieldHeaderText="Chemistry"> <ItemTemplate> <asp:LabelID="lblChemistry"runat="server"Text='hlight7 <%ht0 # Eval("Chemistry") ht7 %>hlight0 '></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateFieldHeaderText="Biology"> <ItemTemplate> <asp:LabelID="lblBiology"runat="server"Text='hlight7 <%ht0 # Eval("Biology") ht7 %>hlight0 '></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateFieldHeaderText="Maths"> <ItemTemplate> <asp:LabelID="lblMath"runat="server"Text='hlight7 <%ht0 # Eval("Math") ht7 %>hlight0 '></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateFieldHeaderText="Total"> <ItemTemplate> <asp:LabelID="lblTotal"runat="server"Text='hlight7 <%ht0 # Eval("Total") ht7 %>hlight0 '></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

4) Include two namespace

4.1) using System.Data.SqlClient;
4.2) using System.Drawing;

5) put the below code in to CS file.

Code Snippet
 protectedvoid Page_Load(object sender, EventArgs e) { //check whether page is postback or not //if page is not postback at that time we bind the gridview if (!Page.IsPostBack) { //here write connection string string strsql = DBConnection.sqlstr; //create object for sqlconnection using System.Data.SqlClient; SqlConnection sqlcon = newSqlConnection(strsql); //here i use the query //create the object of sqlcommand SqlCommand sqlcmd = newSqlCommand("select * from student", sqlcon); //create sqldataadapter object and give the sqlcommand as parameter SqlDataAdapter adp = newSqlDataAdapter(sqlcmd); //declare the dataset DataSet ds = newDataSet(); //fill dataset using fill method of SqlDataAdapter  adp.Fill(ds); //bind the GridView1  GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } } protectedvoid GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //here write connection string string strsql = DBConnection.sqlstr; //create object for sqlconnection using System.Data.SqlClient; SqlConnection sqlcon = newSqlConnection(strsql); //find studentid from the grid string strStudentId = ((Label)e.Row.FindControl("lblStudentId")).Text; SqlCommand sqlcmd = newSqlCommand("select * from studentMarks where StudentId =" + strStudentId, sqlcon); SqlDataAdapter adp = newSqlDataAdapter(sqlcmd); DataSet ds = newDataSet(); adp.Fill(ds); //find the gridview control and assign the datasource  ((GridView)e.Row.FindControl("gvResult")).DataSource = ds.Tables[0]; ((GridView)e.Row.FindControl("gvResult")).DataBind(); } } protectedvoid gvResult_RowDataBound(object sender, GridViewRowEventArgs e) { //here define the criteria for passing //pass when user got 40 or above that other wise fail //if fail then that user row display in the red color //if user got 70 or 70 above then display in green else in normal color if (e.Row.RowType == DataControlRowType.DataRow) { decimal dblChem = Convert.ToDecimal(((Label)e.Row.FindControl("lblChemistry")).Text); decimal dblPhy = Convert.ToDecimal(((Label)e.Row.FindControl("lblPhy")).Text); decimal dblBio = Convert.ToDecimal(((Label)e.Row.FindControl("lblBiology")).Text); decimal dblMath = Convert.ToDecimal(((Label)e.Row.FindControl("lblMath")).Text); if (dblChem < 40 || dblPhy < 40 || dblBio < 40 || dblMath < 40) { //change the color of that row //using System.Drawing;  e.Row.BackColor = Color.Red; } elseif (dblChem >= 70 && dblPhy >= 70 && dblBio >= 70 && dblMath >= 70) { e.Row.BackColor = Color.Green; } } }

Thanks.
http://www.ibusiness-management.com/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s