Merhaba arkadaşlar bu makalemizde GridView nesnesinin içerisinde DropDownList nesnesinin kullanımını göstereceğim.
GridView nesnesinin AutoGenerateColumns özelliğini False yapın. Sonra GridView in EditItemTemplate kısmında DropDownList nesnesini göstereceğiz. Bu örnekte Fiyat sütunuzu DropDownList’te göstermiş olduk.
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="Fiyat" DataValueField="Fiyat"
SelectedValue='<%# Bind("Fiyat") %>'>
</asp:DropDownList>
</EditItemTemplate>
Screenshot
Şekil 1
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" AutoGenerateColumns="False" AutoGenerateEditButton="True"
AllowSorting="True" DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="Id"
runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" >
<Columns>
<asp:BoundField ReadOnly="True" HeaderText="Id"
DataField="Id" SortExpression="Id" InsertVisible="False"></asp:BoundField>
<asp:BoundField HeaderText="Yazar" DataField="Yazar"
SortExpression="Yazar"></asp:BoundField>
<asp:BoundField HeaderText="Kitap" DataField="Kitap"
SortExpression="Kitap"></asp:BoundField>
<asp:TemplateField HeaderText="Fiyat" SortExpression="Fiyat">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="Fiyat" DataValueField="Fiyat"
SelectedValue='<%# Bind("Fiyat") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Fiyat") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Table]"
UpdateCommand="UPDATE [Table] SET [Yazar] = @Yazar, [Kitap] = @Kitap, [Fiyat] = @Fiyat WHERE [Id] = @Id" DeleteCommand="DELETE FROM [Table] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Table] ([Yazar], [Kitap], [Fiyat]) VALUES (@Yazar, @Kitap, @Fiyat)" >
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Yazar" Type="String" />
<asp:Parameter Name="Kitap" Type="String" />
<asp:Parameter Name="Fiyat" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Type="String" Name="Yazar"></asp:Parameter>
<asp:Parameter Type="String" Name="Kitap"></asp:Parameter>
<asp:Parameter Type="String" Name="Fiyat"></asp:Parameter>
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</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