GridView模板列中添加DropDownList

  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>GridView模板列中添加DropDownList</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="10" ForeColor="#333333" PageSize="3" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" Font-Size="9pt" BorderStyle="None" BorderWidth="1px"> 
  11.             <FooterStyle BackColor="White" ForeColor="#000066" />
  12.             <Columns>
  13.                 <asp:BoundField DataField="ID" HeaderText="用户ID" ReadOnly="True" />
  14.                 <asp:BoundField DataField="Name" HeaderText="用户姓名" />
  15.                 <asp:TemplateField HeaderText="性别">
  16.                     <ItemTemplate>
  17.                         <asp:DropDownList ID="DropDownList1" runat="server" DataSource='<%# ddlbind()%>' DataValueField="Sex" DataTextField="Sex">
  18.                         </asp:DropDownList>
  19.                     </ItemTemplate>
  20.                 </asp:TemplateField>
  21.                 <asp:BoundField DataField="Address" HeaderText="家庭住址" />
  22.             </Columns>
  23.             <RowStyle ForeColor="#000066" />
  24.             <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
  25.             <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
  26.             <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
  27.         </asp:GridView>
  28.     </div>
  29.     </form>
  30. </body>
  31. </html>
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class _Default : System.Web.UI.Page 
  12. {
  13.     SqlConnection sqlConn;
  14.     SqlCommand sqlComm;
  15.     string strConn = "Data Source=(local);Database=Exercise;Uid=sa;Pwd=sa";
  16.     protected void Page_Load(object sender, EventArgs e)
  17.     {
  18.         if (!IsPostBack)
  19.         {
  20.             databind();
  21.         }
  22.     }
  23.     private void databind()
  24.     {
  25.         string strSql = "select * from myDt";
  26.         sqlConn = new SqlConnection(strConn);
  27.         SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConn);
  28.         DataSet ds = new DataSet();
  29.         sqlConn.Open();
  30.         sqlDa.Fill(ds, "myDt");
  31.         GridView1.DataSource = ds;
  32.         GridView1.DataBind();
  33.         DropDownList ddl;
  34.         for (int i = 0; i < GridView1.Rows.Count; i++)
  35.         {
  36.             DataRowView drv = ds.Tables["myDt"].DefaultView[i];
  37.             if (Convert.ToString(drv["Sex"]).Trim() == "True")
  38.             {
  39.                 ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
  40.                 ddl.SelectedIndex = 0;
  41.             }
  42.             if (Convert.ToString(drv["Sex"]).Trim() == "False")
  43.             {
  44.                 ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
  45.                 ddl.SelectedIndex = 1;
  46.             }
  47.         }
  48.         sqlConn.Close();
  49.     }
  50.     protected SqlDataReader ddlbind()
  51.     {
  52.         string strSql = "select distinct sex from mydt";
  53.         sqlConn = new SqlConnection(strConn);
  54.         sqlComm = new SqlCommand(strSql, sqlConn);
  55.         sqlConn.Open();
  56.         return sqlComm.ExecuteReader();
  57.     }
  58. }

你可能感兴趣的:(server,C#,database)