国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

asp.net 2.0中gridview里嵌套dropdownlist

2019-11-18 17:16:24
字體:
來源:轉載
供稿:網友

asp.net 2.0中,在一個gridview里,可以嵌套進一個dropdownlist,這是十分容易的事情,而這里講的是,
在每個dropdownlist里,都綁定的是不同的內容,比如在northwind數據庫中,可以用GRIDVIEW顯示出
每個category類別,同時每一行的category類別里可以已dropdonwlist下拉框的形式,列出該分類下的所有
產品.下面介紹實現(xiàn)的方法

首先是頁面部分的代碼
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>

<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />

<asp:BoundField DataField="CategoryName" HeaderText="Category Name" />

<asp:TemplateField HeaderText="

<ItemTemplate>

<asp:DropDownList ID="DropDownList1" runat="server">

</asp:DropDownList>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

在codebehind部分,
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            // This is because Table[1] contains Categories

            GridView1.DataSource = GetDataSet().Tables[1];

            GridView1.DataBind();

        }


    }
    private DataSet GetDataSet()
    {

        string query = @"SELECT p.CategoryID,p.ProductID, p.ProductName FROM Products p

SELECT c.CategoryID,c.CategoryName FROM Categories c";

        string connectionString = "Server=localhost;Database=Northwind;user id=sa;passWord=123456";

        SqlConnection myConnection = new SqlConnection(connectionString);

        SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);

        DataSet ds = new DataSet();

        ad.Fill(ds);

        return ds;

    }
    在上面的代碼中,首先我們通過典型的dataset返回,綁定到gridview上去,注意這里sql語句里有兩句,第一句是返回產品,第二句是返回所有的類別category,而在綁定gridview時,我們用
  GridView1.DataSource = GetDataSet().Tables[1];,將第一個table表里的category記錄綁定到gridview里去,接下來,我們要處理模版列中的dropdownlist了,這個可以在row_databound事件中寫入代碼,如下
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataTable myTable = new DataTable();

        DataColumn productIDColumn = new DataColumn("ProductID");

        DataColumn productNameColumn = new DataColumn("ProductName");

        myTable.Columns.Add(productIDColumn);

        myTable.Columns.Add(productNameColumn);

        DataSet ds = new DataSet();

        ds = GetDataSet();

        int categoryID = 0;

        string expression = String.Empty;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            categoryID = Int32.Parse(e.Row.Cells[0].Text);

            expression = "CategoryID = " + categoryID;

            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");

            DataRow[] rows = ds.Tables[0].Select(expression);

 

            foreach (DataRow row in rows)
            {

                DataRow newRow = myTable.NewRow();

                newRow["ProductID"] = row["ProductID"];

                newRow["ProductName"] = row["ProductName"];

                myTable.Rows.Add(newRow);

            }

            ddl.DataSource = myTable;

            ddl.DataTextField = "ProductName";

            ddl.DataValueField = "ProductID";

            ddl.DataBind();

        }

  }
  這里的原理大致如下:
首先,我們建立了一個datatable,包含了productid,productname列,然后將其與 gridview綁定,之后再用
 categoryID = Int32.Parse(e.Row.Cells[0].Text);

            expression = "CategoryID = " + categoryID;
構造一個篩選表達式,這里指定為categoryID,然后通過
 DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
           DataRow[] rows = ds.Tables[0].Select(expression);
  ,找出符合其類別等于某一categoryID的所有產品,這里構造出datarow集合了,最后,使用循環(huán)
,將某類別下的產品全部添加到datatable中去,最后將datatable和dropdownlist綁定,就實現(xiàn)功能了


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿城市| 伊春市| 商水县| 乐清市| 云梦县| 乌兰县| 娄烦县| 石门县| 中牟县| 出国| 鞍山市| 疏勒县| 镇坪县| 宜都市| 河源市| 大邑县| 双江| 通道| 衡阳市| 南开区| 鲁甸县| 济南市| 高碑店市| 油尖旺区| 遵义市| 商都县| 平顶山市| 宝清县| 庆云县| 板桥市| 浪卡子县| 鹤岗市| 棋牌| 丘北县| 南和县| 呼玛县| 南澳县| 台南县| 土默特左旗| 威远县| 若尔盖县|