Yazı Font Küçült Yazı Font Büyült

GridView Nesnesinde Satır Rengini Değiştirme

 

Merhaba arkadaşlar. Bu makalemizde ID noya göre satır arka plan renklerini değiştireceğiz. GridView1_RowDataBound  a aşağıdaki kodları yazın.

 

Resim1

Şekil 1

 

 

Default.aspx.cs

 

 

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)

    {

 

    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        try

        {

            if (e.Row.RowType.Equals(DataControlRowType.DataRow))

            {

                DataRowView drv = (DataRowView)e.Row.DataItem;

                for (int i = 0; i < drv.DataView.Table.Columns.Count; i++)

                {

 

                    //ID No 4 olan satırın background rengini sarı yapıyoruz.

                    if ((int)DataBinder.Eval(e.Row.DataItem, "ID") == 4) 

                    e.Row.Cells[i].BackColor = System.Drawing.Color.Yellow;

                    //Aynı şekilde 7 nolu satırın rengini değiştiriyoruz.

                    if ((int)DataBinder.Eval(e.Row.DataItem, "ID") == 7)

                        e.Row.Cells[i].BackColor = System.Drawing.Color.OrangeRed;

                }

            }

        }

        catch (Exception ex)

        {

            Response.Write(ex.ToString());

        }

    }

 

}

 

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" OnRowDataBound="GridView1_RowDataBound" runat="server" AutoGenerateColumns="False"

            DataKeyNames="ID" DataSourceID="SqlDataSource1">

            <Columns>

                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"

                    ReadOnly="True" SortExpression="ID" />

                <asp:BoundField DataField="Company" HeaderText="Company"

                    SortExpression="Company" />

                <asp:BoundField DataField="LastName" HeaderText="LastName"

                    SortExpression="LastName" />

                <asp:BoundField DataField="FirstName" HeaderText="FirstName"

                    SortExpression="FirstName" />

                <asp:BoundField DataField="CountryRegion" HeaderText="CountryRegion"

                    SortExpression="CountryRegion" />

                <asp:BoundField DataField="JobTitle" HeaderText="JobTitle"

                    SortExpression="JobTitle" />

                <asp:BoundField DataField="ZIPPostal" HeaderText="ZIPPostal"

                    SortExpression="ZIPPostal" />

            </Columns>

        </asp:GridView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"

            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"

           

            SelectCommand="SELECT [ID], [Company], [LastName], [FirstName], [CountryRegion], [JobTitle], [ZIPPostal] FROM [Customers]">

        </asp:SqlDataSource>

   

    </div>

    </form>

</body>

</html>

 

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek dileğiyle. Hoşçakalın. Bahadır ŞAHİN