asp.net 2.0中gridview里嵌套dropdownlist

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="Category Name">
                <ItemTemplate>
                   
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
                    
            <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)
    {
        GridView1.DataSource = GetDataSet().Tables[1];
//取第二张表Catagories的数据
        GridView1.DataBind();
    }

    //取了二张表,第一张Products 第二张Categories
    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 = ConfigurationManager.ConnectionStrings["northwindconnstr"].ToString();
        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
           
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
          
  //products表中,对应CategoryID = XX 的记录选出来,并加入MyTable
           
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();

            //处理DropDownList2           
            myTable = ds.Tables[1];
//把第二张表提出来
            DropDownList dd2 = (DropDownList)e.Row.FindControl("DropDownList2");
            dd2.DataSource = myTable;
            dd2.DataTextField = "CategoryName";
            dd2.DataValueField = "CategoryID";
            dd2.DataBind();

            for (int i = 0; i < dd2.Items.Count; i++) {
                if (Convert.ToInt32(dd2.Items[i].Value) == categoryID)
                {
                    dd2.Items[i].Selected = true;
                }
                else {
                    dd2.Items[i].Selected = false;
                }
            }
        }
    }

这里的原理大致如下:

首先,我们建立了一个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中去,最后将datatabledropdownlist绑定,就实现功能了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值