■38765 / inTopicNo.8) |
Re[7]: asp.netでのデータグリッド 扱い方 |
□投稿者/ gtk2k (16回)-(2009/07/24(Fri) 17:34:10)
|
SelectedValueでだめなん?って思うんだが。 とりあえず、2つのテーブルが以下のように作成
Table_1 name | sex_id --------------- dameeo | 0 dameko | 1
Table_2 sex_id | sex --------------- 0 | 男 1 | 女
んで、aspxに以下のソースをコピペ <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._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" DataSourceID="SqlDataSource1" DataKeyNames="name"> <Columns> <asp:CommandField ShowEditButton="True" /> <asp:TemplateField HeaderText="sex" SortExpression="sex"> <EditItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="sex" DataValueField="sex_id" SelectedValue='<%# Bind("sex_id") %>'> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("sex") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="name" HeaderText="name" ReadOnly="True" SortExpression="name" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:hogeConnectionString %>" SelectCommand="SELECT [Table_2].[sex], [Table_1].[sex_id], [Table_1].[name] FROM [Table_1] LEFT JOIN [Table_2] ON [Table_1].sex_id = [Table_2].sex_id" DeleteCommand="DELETE FROM [Table_1] WHERE [name] = @name" InsertCommand="INSERT INTO [Table_1] ([sex_id], [name]) VALUES (@sex_id, @name)" UpdateCommand="UPDATE [Table_1] SET [sex_id] = @sex_id WHERE [name] = @name"> <DeleteParameters> <asp:Parameter Name="name" Type="String" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="sex_id" Type="String" /> <asp:Parameter Name="name" Type="String" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="sex_id" Type="String" /> <asp:Parameter Name="name" Type="String" /> </InsertParameters> </asp:SqlDataSource> </div> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:hogeConnectionString %>" SelectCommand="SELECT [sex_id], [sex] FROM [Table_2]"></asp:SqlDataSource> </form> </body> </html>
web.configファイルに以下の項目を追加 <configuration> <connectionStrings> <add name="tanakalogConnectionString" connectionString="接続文字列" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
以上 sex列がテンプレート列でドロップリストとなっている。
このような動作をさせたいと理解してんだが、違うのか?
|
|