後台代碼:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClIEnt;
public partial class Default4 : System.Web.UI.Page
{
SqlConnection sqlcon;
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddl;
if (!IsPostBack)
{
string sqlstr = "select top 5 * from 飛狐工作室";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "飛狐工作室");
GridVIEw1.DataSource = myds;
GridVIEw1.DataBind();
for (int i = 0; i <= GridVIEw1.Rows.Count - 1; i++)
{
DataRowView mydrv = myds.Tables["飛狐工作室"].DefaultVIEw[i];
if (Convert.ToString(mydrv["員工性別"]).Trim() == "True")
{
ddl = (DropDownList)GridVIEw1.Rows[i].FindControl("DropDownList1");
ddl.SelectedIndex = 0;
}
if (Convert.ToString(mydrv["員工性別"]).Trim() == "False")
{
ddl = (DropDownList)GridVIEw1.Rows[i].FindControl("DropDownList1");
ddl.SelectedIndex = 1;
}
}
sqlcon.Close();
}
}
public SqlDataReader ddlbind()
{
string sqlstr = "select distinct 員工性別 from 飛狐工作室";
sqlcon = new SqlConnection(strCon);
SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
return sqlcom.ExecuteReader();
}前台主要代碼:
<ASP:GridView ID="GridVIEw1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" Border BorderWidth="1px">
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<ASP:BoundField DataFIEld="SFZ號碼" HeaderText="用戶ID" SortExpression="SFZ號碼" />
<ASP:BoundField DataFIEld="姓名" HeaderText="用戶姓名" SortExpression="姓名"/>
<ASP:TemplateFIEld HeaderText="員工性別">
<ItemTemplate>
<ASP:DropDownList ID="DropDownList1" runat="server" DataSource='<%# ddlbind()%>' DataValueField="員工性別" DataTextFIEld="員工性別">
</ASP:DropDownList>
</ItemTemplate>
</ASP:TemplateFIEld>
<ASP:BoundField DataFIEld="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</ASP:GridVIEw>