How to use RowDataBound with Grid
View wizard in Asp.Net
-- Execute this query with SqlServer
create database GridViewPractice
use GridViewPractice
-- create table Student……………………
create table Student(RollNo int primary key,Name nvarchar(50),Subject nvarchar(50),Marks int,Age int)
First we take a Web Page Default.aspx with GridView
and bind this grid with wizard concept then
we take template field which name is Result ……….
<%@ 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>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="d1"><b><center>How to
use RowDataBound with GridView wizard in Asp.Net</b><br /></center></div>
<div id="d2"><center>
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False" DataKeyNames="RollNo"
DataSourceID="SqlDataSource1" BackColor="White"
BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px"
CellPadding="4"
onrowdatabound="GridView1_RowDataBound" ShowFooter="True">
<Columns>
<asp:BoundField DataField="RollNo"
HeaderText="RollNo"
ReadOnly="True"
SortExpression="RollNo" />
<asp:BoundField DataField="Name"
HeaderText="Name"
SortExpression="Name"
/>
<asp:BoundField DataField="Subject"
HeaderText="Subject"
SortExpression="Subject" />
<asp:BoundField DataField="Age"
HeaderText="Age"
SortExpression="Age"
/>
<asp:BoundField DataField="Marks"
HeaderText="Marks"
SortExpression="Marks"
/>
<asp:TemplateField HeaderText="Result"></asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC"
ForeColor="#003399"
/>
<HeaderStyle BackColor="#003399"
Font-Bold="True"
ForeColor="#CCCCFF"
/>
<PagerStyle BackColor="#99CCCC"
ForeColor="#003399"
HorizontalAlign="Left"
/>
<RowStyle BackColor="White"
ForeColor="#003399"
/>
<SelectedRowStyle BackColor="#009999"
Font-Bold="True"
ForeColor="#CCFF99"
/>
<SortedAscendingCellStyle BackColor="#EDF6F6"
/>
<SortedAscendingHeaderStyle BackColor="#0D4AC4"
/>
<SortedDescendingCellStyle BackColor="#D6DFDF"
/>
<SortedDescendingHeaderStyle BackColor="#002876"
/>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$
ConnectionStrings:GridViewPracticeConnectionString %>"
DeleteCommand="DELETE FROM [Student] WHERE
[RollNo] = @RollNo"
InsertCommand="INSERT INTO [Student]
([RollNo], [Name], [Subject], [Marks], [Age]) VALUES (@RollNo, @Name, @Subject,
@Marks, @Age)"
SelectCommand="SELECT * FROM [Student] ORDER
BY [RollNo]"
UpdateCommand="UPDATE [Student] SET [Name] =
@Name, [Subject] = @Subject, [Marks] = @Marks, [Age] = @Age WHERE [RollNo] =
@RollNo">
<DeleteParameters>
<asp:Parameter Name="RollNo"
Type="Int32"
/>
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="RollNo"
Type="Int32"
/>
<asp:Parameter Name="Name"
Type="String"
/>
<asp:Parameter Name="Subject"
Type="String"
/>
<asp:Parameter Name="Marks"
Type="Int32"
/>
<asp:Parameter Name="Age"
Type="Int32"
/>
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name"
Type="String"
/>
<asp:Parameter Name="Subject"
Type="String"
/>
<asp:Parameter Name="Marks"
Type="Int32"
/>
<asp:Parameter Name="Age"
Type="Int32"
/>
<asp:Parameter Name="RollNo"
Type="Int32"
/>
</UpdateParameters>
</asp:SqlDataSource>
</center></div>
</div>
</form>
</body>
</html>
Code with
Default.aspx.cs…………………………………………..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
public partial class _Default :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs
e)
{
}
int total = 0;
int pass = 0;
int fail = 0;
int exec = 0;
protected void
GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int m = (int)DataBinder.Eval(e.Row.DataItem, "Marks");
total++;
// e.Row.Cells[5].Text = m
> 40? "Pass" : "<blink>Fail</blink>";
if (m>=0 && m<=35 )
{
e.Row.Cells[5].Text
= "<blink> fail</blink>";
e.Row.Cells[5].BackColor
= Color.Lime;
e.Row.Cells[5].ForeColor
= Color.Red;
fail++;
}
else if (m >= 36
&& m <= 74)
{
e.Row.Cells[5].Text
= "<blink> pass</blink>";
e.Row.Cells[5].BackColor
= Color.Yellow;
e.Row.Cells[5].ForeColor
= Color.Green;
pass++;
}
else if (m >=75
&& m<=100)
{
e.Row.Cells[5].Text
= "Excellent";
e.Row.Cells[5].BackColor
= Color.Violet;
e.Row.Cells[5].ForeColor
= Color.Black;
exec++;
}
}
//go to GridView Property
Show Footer=True
// code forvhow to merge
cells of Footer in GridView
if (e.Row.RowType == DataControlRowType.Footer)
{
//int c = e.Row.Cells.Count;
//e.Row.Cells[0].ColumnSpan = c;
//for (int i = 1; i < c ; i++)
//{
// e.Row.Cells[i].Visible
= false;
//}
int c = e.Row.Cells.Count;
e.Row.Cells.Clear();
e.Row.Cells.Add(new TableCell());
e.Row.Cells[0].ColumnSpan
= c;
e.Row.Style["color"] = "Red";
e.Row.Cells[0].BackColor
= Color.White;
e.Row.Cells[0].Text
= "Total :=" + total + " Pass :=
" + pass + " Fail =" + fail + "Excellent =" + exec;
}
}
}
0 comments:
Post a Comment