asp.net 2.0中gridview裡嵌套dropdownlist

來源:互聯網
上載者:User
在asp.net 2.0中,在一個gridview裡,可以嵌套進一個dropdownlist,這是十分容易的事情,而這裡講的是,
在每個dropdownlist裡,都綁定的是不同的內容,比如在northwind資料庫中,可以用GRIDVIEW顯示出
每個category類別,同時每一行的category類別裡可以已dropdonwlist下拉框的形式,列出該分類下的所有
產品.下面介紹實現的方法

首先是頁面部分的代碼
 <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="Products">

<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集合了,最後,使用迴圈
,將某類別下的產品全部添加到datatable中去,最後將datatable和dropdownlist綁定,就實現功能了

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.