程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> GridView和下拉菜單DropDownList結合

GridView和下拉菜單DropDownList結合

編輯:.NET實例教程

效果圖:

GridView和下拉菜單DropDownList結合

  後台代碼:

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;
    string strCon = "Data Source=(local);Database=北風貿易;Uid=sa;Pwd=sa";
    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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved