How to use RowDataBound Event in GridView
RowDataBound in GridView event fires when the data row is bound to
the data of the GridView. Can be used to do data manipulation based on the
custom condition, or do logical function based on the data for each GridViewRow
or to alter the UI of each row based on the condition.
// Execute sql query in Sql Server
use
test
create table items(SrNo int identity(1,1) primary key,ItemName nvarchar(50),Price int,Quantity int )
insert into items values('Watch',500,5)
insert into items values('Maruti',500000,15)
Select * from
items
This 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>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%"
>
<tr><td><center><font color="red"><b><i>How to use
RowDataBound Event in GridView</i></b></font></center></td></tr>
<tr><td><center> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333"
GridLines="None"
onrowdatabound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White"
/>
<Columns>
<asp:TemplateField HeaderText="SrNo">
<ItemTemplate>
<asp:Label ID="lblsrno"
runat="server"
Text='<%# Eval("SrNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ItemName">
<ItemTemplate>
<asp:Label ID="lblitemname"
runat="server"
Text='<%# Eval("ItemName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price/Quantity">
<ItemTemplate>
<asp:Label ID="lblprice"
runat="server"
Text='<%# Eval("Price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:Label ID="lblquantity"
runat="server"
Text='<%# Eval("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total
Price">
<ItemTemplate>
<asp:Label ID="lbltotal"
runat="server"
Text="Label"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000"
Font-Bold="True"
ForeColor="White"
/>
<HeaderStyle BackColor="#990000"
Font-Bold="True"
ForeColor="White"
/>
<PagerStyle BackColor="#FFCC66"
ForeColor="#333333"
HorizontalAlign="Center"
/>
<RowStyle BackColor="#FFFBD6"
ForeColor="#333333"
/>
<SelectedRowStyle BackColor="#FFCC66"
Font-Bold="True"
ForeColor="Navy"
/>
<SortedAscendingCellStyle BackColor="#FDF5AC"
/>
<SortedAscendingHeaderStyle BackColor="#4D0000"
/>
<SortedDescendingCellStyle BackColor="#FCF6C0"
/>
<SortedDescendingHeaderStyle BackColor="#820000"
/>
</asp:GridView>
</center></td></tr>
</table>
</div>
</form>
</body>
</html>
This 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;
using System.Data.SqlClient;
using System.Data;
public partial class _Default :
System.Web.UI.Page
{
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
//code for GridView Binding on Page Load
protected void Page_Load(object sender, EventArgs
e)
{
con = new SqlConnection("data source=KUSH-PC\\KUSH;Initial
Catalog=test;Integrated Security=True");
da = new SqlDataAdapter("Select * from Items", con);
ds = new DataSet();
da.Fill(ds);
GridView1.DataSource
= ds;
GridView1.DataBind();
}
//How to bind a runtime column GridView with RowDataBound
Event..................Like (Price*Quantity=Toatal Price)
protected void
GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label price, quantity;
Label total1 = ((Label)e.Row.FindControl("lbltotal"));
if (e.Row.RowType == DataControlRowType.DataRow)
{
price =
((Label)e.Row.FindControl("lblprice"));
quantity
= ((Label)e.Row.FindControl("lblquantity"));
int total = Convert.ToInt32(price.Text)*
Convert.ToInt32(quantity.Text);
total1.Text
= total.ToString();
}
}
}
How
to apply bold and color on particular Cell in Grid View.................
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow grv in GridView2.Rows)
{
grv.Cells[1].Style["font-weight"] = "bold";
grv.Cells[1].Style["Color"] = "Red";
}
}
0 comments:
Post a Comment