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

GridView ve DetailsView Nesnelerini Birlikte Kullanmak

 

Herkese selamlar. Bu makalemizde GridView ve DetailsView nesnesini birlikte kullanacağız. Sayfamıza GridView, DetailsView ve 2 adet SqlDataSource ekleyeceğiz. SqlDataSource1 in Configure Data Source kısmından Employes tablosunu seçiyoruz.

 

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

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

            SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title], [BirthDate], [City], [Country] FROM [Employees]">

        </asp:SqlDataSource>

 

Yine aynı şekilde  SqlDataSource2 içinde Configure Data Source kısmından aşağıda gösterildiği üzere Şekil2 deki gibi Where komutunu ekleyerek Employee  tablosunu seçiyoruz.

 

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

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

            SelectCommand="SELECT * FROM [Employees] WHERE ([EmployeeID] = @EmployeeID)">

            <SelectParameters>

                <asp:ControlParameter ControlID="GridView1" Name="EmployeeID"

                    PropertyName="SelectedValue" Type="Int32" />

            </SelectParameters>

        </asp:SqlDataSource>

 

Şimdi GridView nesnesinin Columns kısmına Add Column kısmından ButtonField alanı ekliyoruz.

 

<asp:ButtonField ButtonType="Button" CommandName="Select" Text="Seç" />

 

 Artık Seç butonumuzuda ekledik. Web sayfamızı çalıştırdığımızda GridView daki Seç butonuna tıkladığımızda o satırda kayıtlı kişinin detay bilgilerini DetailsView nesnemizde görmüş olduk.

 

.cs

 

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

            DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1" CellPadding="4"

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

            <AlternatingRowStyle BackColor="White" />

            <Columns>

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

                    InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />

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

                    SortExpression="LastName" />

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

                    SortExpression="FirstName" />

                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />

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

                    SortExpression="BirthDate" />

                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

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

                    SortExpression="Country" />

                <asp:ButtonField ButtonType="Button" CommandName="Select" Text="Seç" />

            </Columns>

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

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

            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />

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

            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />

            <SortedAscendingCellStyle BackColor="#FDF5AC" />

            <SortedAscendingHeaderStyle BackColor="#4D0000" />

            <SortedDescendingCellStyle BackColor="#FCF6C0" />

            <SortedDescendingHeaderStyle BackColor="#820000" />

        </asp:GridView>

        <br />

        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"

            CellPadding="4" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource2"

            ForeColor="#333333" GridLines="None" Height="50px" Width="388px">

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

            <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />

            <EditRowStyle BackColor="#999999" />

            <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />

            <Fields>

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

                    InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />

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

                    SortExpression="LastName" />

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

                    SortExpression="FirstName" />

                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />

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

                    SortExpression="TitleOfCourtesy" />

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

                    SortExpression="BirthDate" />

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

                    SortExpression="HireDate" />

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

                    SortExpression="Address" />

                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

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

                    SortExpression="Region" />

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

                    SortExpression="PostalCode" />

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

                    SortExpression="Country" />

               

            </Fields>

            <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" />

        </asp:DetailsView>

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

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

            SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title], [BirthDate], [City], [Country] FROM [Employees]">

        </asp:SqlDataSource>

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

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

            SelectCommand="SELECT * FROM [Employees] WHERE ([EmployeeID] = @EmployeeID)">

            <SelectParameters>

                <asp:ControlParameter ControlID="GridView1" Name="EmployeeID"

                    PropertyName="SelectedValue" Type="Int32" />

            </SelectParameters>

        </asp:SqlDataSource>

        <br />

   

    </div>

    </form>

</body>

</html>

 

Ekran Çıktısı

 

Resim1

Şekil 1

 

Resim2

Şekil 2

 

Resim3

Şekil 3

 

Resim4

Şekil 4

 

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