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

GridView daki Null Kısımları Doldurmak

Selam arkadaşlar. Bu makalede GridView nesnesinde Null olan alanları ItemTemplate metodunu kullanarak bu boş alanlara “Kayıt Yok!”  yazısını yazacağız. Gridview nesnesinin Columns alanına aşağıdaki kodda belirtilen ItemTemplateleri ekleyeceğiz.

Resim1

Şekil 1

Resim2

Şekil 2

Örnek olarak Ad sütunu için,

<asp:TemplateField HeaderText="Ad">

                <ItemTemplate>

                <asp:Label ID="Label1" runat="server"

           Text='<%# BoslukKontrol(Eval("Ad")) %>'>

              </asp:Label>

              </ItemTemplate>

              </asp:TemplateField>

 

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" AutoGenerateColumns="False"

            DataKeyNames="Id" DataSourceID="SqlDataSource1" CellPadding="4"

            ForeColor="#333333" GridLines="None">

            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

            <Columns>

                <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True"

                    SortExpression="Id" />

                <asp:TemplateField HeaderText="Ad">

                <ItemTemplate>

                <asp:Label ID="Label1" runat="server"

           Text='<%# BoslukKontrol(Eval("Ad")) %>'>

              </asp:Label>

              </ItemTemplate>

              </asp:TemplateField>

                 <asp:TemplateField HeaderText="Soyad">

                <ItemTemplate>

                <asp:Label ID="Label1" runat="server"

           Text='<%# BoslukKontrol(Eval("Soyad")) %>'>

              </asp:Label>

              </ItemTemplate>

              </asp:TemplateField>

                 <asp:TemplateField HeaderText="Mail">

                <ItemTemplate>

                <asp:Label ID="Label1" runat="server"

           Text='<%# BoslukKontrol(Eval("Mail")) %>'>

              </asp:Label>

              </ItemTemplate>

              </asp:TemplateField>

                 <asp:TemplateField HeaderText="Departman">

                <ItemTemplate>

                <asp:Label ID="Label1" runat="server"

           Text='<%# BoslukKontrol(Eval("Departman")) %>'>

              </asp:Label>

              </ItemTemplate>

              </asp:TemplateField>

              

            </Columns>

            <EditRowStyle BackColor="#999999" />

            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />

            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

            <SortedAscendingCellStyle BackColor="#E9E7E2" />

            <SortedAscendingHeaderStyle BackColor="#506C8C" />

            <SortedDescendingCellStyle BackColor="#FFFDF8" />

            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

        </asp:GridView>

   

    </div>

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

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

        SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>

   

    </form>

</body>

</html>

 

Default.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 Page_Load(object sender, EventArgs e)

    {

 

    }

    protected string BoslukKontrol(object gridview)

    {

        if (object.ReferenceEquals(gridview, DBNull.Value))

        {

            return "Kayıt Yok!";

        }

        else

        {

            return gridview.ToString();

        }

     }

 

}

 

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