Merhaba arkadaslar bu makalemizde GridView nesnesinin OnRowDataBound özelligine yazilan kodla; Fiyat sütunumuzunda ki fiyat deger araligina göre sorgulama yapacagiz. Sonrasinda sorgulama kriterine göre Yazar sütunundaki satirlarin arka plan rengini ve yazi rengini degistirecegiz.
GridView in AutoGenerateColumns="false" yapin.
Sekil 1
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
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)
{
if (!IsPostBack)
{
bindData();
}
}
protected void bindData()
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\kategori.mdf;Integrated Security=True");
{
DataTable dt = new DataTable();
con.Open();
SqlCommand cmd = new SqlCommand("select * from [dbo].[Table]", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//fiyati 15 tl den dusuk olan hucreleri arka plan rengini ve yazi rengini degistiriyoruz.
if (Convert.ToInt32(e.Row.Cells[3].Text) < 15)
{
e.Row.Cells[1].BackColor = Color.OrangeRed;
e.Row.Cells[1].ForeColor = Color.White;
}
}
}
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<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" CellPadding="6" AutoGenerateColumns="false" OnRowDataBound = "OnRowDataBound" >
<Columns>
<asp:BoundField DataField="id" HeaderText="Id" ItemStyle-Width = "50"/>
<asp:BoundField DataField="yazar" HeaderText="Yazar" ItemStyle-Width = "200"/>
<asp:BoundField DataField="kitap" HeaderText="Kitap" ItemStyle-Width = "200"/>
<asp:BoundField DataField="fiyat" HeaderText="Fiyat" ItemStyle-Width = "50"/>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüsmek üzere. Bahadir SAHIN
0 comments:
Yorum Gönder