GridView da RowDataBound Olayları
Merhaba arkadaşlar, bu makalemizde GridView da RowDataBound olayına yazacağız kodlarla GridView daki sütunlarda bulunan verileri italik, bold ve renkli olarak yazdıracağız. GridView nesnenize OnRowDataBound="GridView1_RowDataBound" i ekleyin. GridView in AllowPaging özelliğini True yapın.
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 GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Ekrandaki customer id yi kalın yazdırıyoruz.
e.Row.Cells[0].Text = "<b>" + e.Row.Cells[0].Text + "</b>";
//Ekrandaki company name i italik olarak yazıyoruz.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
//Ekrandaki contact name i renkli olarak yazıyoruz.
e.Row.Cells[2].Text = "<font color='#0000FF'>" + e.Row.Cells[2].Text + "</font>";
}
}
}
aspx kodu:
<%@ 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>
<asp:GridView ID="GridView1" runat="server"
OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False"
DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" AllowPaging="True">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName] FROM [Customers]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>