ASP.NET 在GridView中加入DropDownList

  • 17042
  • 0

摘要:ASP.NET 在GridView中加入DropDownList

今天颱風假(天佑台灣),

趁著休息時間來跟大家分享

如何在GirdView的控制項中,加入DropDownList,並且將DropDownList將與資料庫繫結

1.首先我們要編寫GridView的HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnRowDataBound="MFG2_RowDataBound">
            <Columns>
                <asp:BoundField  DataField="itno" HeaderText="產品貨號"/>
                <asp:BoundField  DataField="itname" HeaderText="產品名稱"/>
                <asp:TemplateField HeaderText="產品類別">
                    <ItemTemplate>
                        <asp:DropDownList ID="CboTP2" runat="server"></asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
</asp:GridView>

重點是在這

<asp:TemplateField HeaderText="產品類別">
                    <ItemTemplate>
                        <asp:DropDownList ID="CboTP2" runat="server"></asp:DropDownList>
                    </ItemTemplate>
</asp:TemplateField>

在<ItemTemplate>的Tag裡,可以夾我們要的DropDownList    

<asp:DropDownList ID="CboTP2" runat="server"></asp:DropDownList>

當然你也可以夾其他的控制項,ex:TextBox、Button、CheckBox、RadioButtion........etc.

 

2.接下來就是我們的程式碼了,我們先要繫結GridView的資料

//宣告資料庫連線字串,請參考之前寫的[資料庫連線搭配Web.config],取得資料庫連線字串
string SqlConStr=ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString.ToString();
//SQL查詢語法,給GridView1繫結用
string sql="select top 10 itno,itname from item";
SqlConnection sqlCon=new SqlConnection(SqlConStr);
DataView dv;
SqlDataAdapter sqlAdp = new SqlDataAdapter();
SqlCommand cmd;
DataSet ds = new DataSet();
sqlCon.Open();
cmd = new SqlCommand(sql, sqlCon);
sqlAdp.SelectCommand = cmd;
sqlAdp.Fill(ds);
dv = new DataView(ds.Tables[0]);

//繫結GridView1
GridView1.DataSource = dv;
GridView1.DataBind();

sqlCon.Close();

3.接下來我們要GridView的RowDataBound的事件,將我們的DropDownList與資料庫做繫結

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
            //宣告DropDownList
            DropDownList CboTp2;
            string sql;
            DataView dv;

            //要特別注意一下這邊,如果不用這個if包起來的話,RowDataBound會跑Header,Footer,Pager
            //我們的DropDownList是放在DataRow裡,所以只有在這邊才會找到DropDownList控制項
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //用FindControl(你的DropDownList的ID),來找我們的DropDownList,記得要轉型喔!
                CboTp2 = (DropDownList)e.Row.FindControl("CboTp2");
                sql = "select tp,tpname from itemtp";
                dv = GetDV(sql);

                //DropDownList要顯示的內容
                CboTp2.DataTextField = "tpname";

                //DropDownList顯示內容對應的值
                CboTp2.DataValueField = "tp";
                //繫結DropDownList
                CboTp2.DataSource = dv;
                CboTp2.DataBind();
            }
}

private DataView GetDV(string sql)
{

                string SqlConStr=ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString.ToString();

                SqlConnection sqlCon=new SqlConnection(SqlConStr);
                DataView dv;
                SqlDataAdapter sqlAdp = new SqlDataAdapter();
                SqlCommand cmd;
                DataSet ds = new DataSet();
                sqlCon.Open();
                cmd = new SqlCommand(sql, sqlCon);
                sqlAdp.SelectCommand = cmd; 
                sqlAdp.Fill(ds);
                dv = new DataView(ds.Tables[0]);
                return dv;
                sqlCon.Close();
}

 

以上,就是今天跟大家分享的,

希望有所收穫

小弟在此下台一鞠躬!