Merhaba arkadaşlar bu makalemizde GridView daki seçili checkbox’lardaki satırların arka plan rengini değiştireceğiz. İlk önce GridView nesnesinin AutoGenerateColumns="False" özelliğini False yapın. Item templateye ekleyeceğimiz checkbox ın OnCheckedChanged olayına yazılacak kod satırıyla arka plan rengi değişecek. İstenilirse GridView nesnesinin GridView1_RowDataBound olayına yazılacak kod satırıyla da arka plan rengi değiştirilebilir.
Şekil 1
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace gridview_select_row_on_checkbox
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindList();
}
}
protected void bindList()
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\person.mdf;Integrated Security=True");
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * From [dbo].[person]", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
/*if (e.Row.RowType == DataControlRowType.DataRow)
{
Change the id of checkbox here.. cbox id sini kontrol ediyoruz.
CheckBox chk = (CheckBox)e.Row.FindControl("chkSelect");
If checkbox checked then change color of row. Secili satirin
rengini degistiriyoruz
if (chk.Checked == true)
{
e.Row.BackColor = System.Drawing.Color.Red;
}
else
{
// e.Row.BackColor = System.Drawing.Color.Green;
}
} */
}
protected void chkSelect_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
GridViewRow row = (GridViewRow)cb.NamingContainer;
if (cb.Checked)
{
//cbox secili ise, secili satirin rengini degistiriyoruz.
row.BackColor = System.Drawing.Color.SkyBlue;
}
else
{
//cbox secili degilse, secili satirin rengi beyaz oluyor.
row.BackColor = System.Drawing.Color.White;
}
}
}
}
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="gridview_select_row_on_checkbox.WebForm1" %>
<!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" AutoGenerateColumns="False" DataKeyNames="Id"
OnRowDataBound="GridView1_RowDataBound" CellPadding="4" GridLines="Horizontal" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" OnCheckedChanged="chkSelect_CheckedChanged" AutoPostBack="true" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Surname" HeaderText="Surname" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
</asp:GridView>
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN
0 comments:
Yorum Gönder