这个DropDownList的item还要根据条件可以动态改变

解决方案 »

  1.   

    DropDownList是不是在DataGrid的编辑模版中
      

  2.   

    在ItemTemplate中放入一个 DropDownList控件。
    在DataGrid1_ItemDataBount()事件中针对当前处于编辑状态下的Item动态设置其中的DropDownList的数据源并 绑定。 private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.EditItem)   //或者if (e.Item.ItemType == ListItemType.SelectedItem)
    {
    DropDownList myDropDownList = (DropDownList)e.Item.FindControl("DropDownList1");
    DataSet dt;
    //查找数据并绑定到dt
    myDropDownList.DataSource = dt;
    myDropDownList.DataBind();
    }
    }
      

  3.   

    to:spring_ok(SpringDotNet):
    我在datagrid中有两个DropDownList 我要根据其中一个DropDownList的selectitem 动态生成另一个DropDownList的item
      

  4.   

    可以在DataGrid的ItemCreate事件中实现
    这个事件在DataGrid没生成新的一行时触发,你可以e.Item.Cells[i]找到第一个DropDownList,然后根据这个DropDownList的SelectItem来帮定数据到另外一个DropDownList。我用这种方法动态的帮定数据到DataGrid的DropDownList中,试一下吧,好运
      

  5.   

    你这样的情况可能要麻烦一些。因为你必须要处理每一个下拉列表框的选项改变时重新绑定第二个下拉列表框的情况。而此时必须要能确定当前要处理的是datagrid中的那一个 item,才能找到正确的下接列表框。你可以这样做:
    参照我前面回复中的代码,在datagrid_itemdatabind中绑定到第一个dropdownlist,并用其当前选择项的值绑定第二个列表框。同时为当前处理编辑状态下的item中的第一个下拉列表框添加一个事件处理句柄,指向一个名为dropdownlist_selectedindexchanged事件(请参考InitialComponent()中的代码). 再使有一个ViewState变量来保存当前的datagrid的item的索引值。
    最后生成一个dropdownlist_selectedindexchanged事件,在其中使用viewstate中的值确定datagrid中的编辑行,重新绑定dropdownlist2.
      

  6.   

    "同时为当前处理编辑状态下的item中的第一个下拉列表框添加一个事件处理句柄,指向一个名为dropdownlist_selectedindexchanged事件(请参考InitialComponent()中的代码). "
     spring_ok(SpringDotNet) 高手:
    怎么做事件的指向?
      

  7.   

    添加事件句柄, 放入 ItemDataBound中:
    DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList_SelectedIndexChanged);
    private void DropDownList_SelectedIndexChanged(Object sender, EventArgs e)
    {  
    //处理代码
    }
      

  8.   

    太谢谢了,不过
    在this.DropDownList_SelectedIndexChanged事件中
    怎么获取dropdownlist的选择的值哪?
      

  9.   

    选择的值可以是"sender.SelectedItem.Text" 问题是怎样找另一个DropDownList
      

  10.   

    这还不简单,在第一个DropDownList的SelectedIndexChanged事件中绑定另一个DropDownList不就得了。
      

  11.   

    如果你的DropDownList在row行col列,那么
    DataGrid.Tables[tablename].Rows[row][col].Controls[1]就是那个控件。
      

  12.   

    记得加(System.Web.UI.WebControls.DropDownList)强制类型转换。
      

  13.   

    我前面说“再使有一个ViewState变量来保存当前的datagrid的item的索引值。
    最后生成一个dropdownlist_selectedindexchanged事件,在其中使用viewstate中的值确定datagrid中的编辑行,重新绑定dropdownlist2.

    再详细一点:
    假设ViewState["itemindex"]中存的是当前编辑行在 DataGrid1.Items中的索引号,这个应该不难吧。
    用的时候:
    int i = (int)ViewState["itemindex"];
    DropDownList mydrop = (DropDownList)DataGrid1.Items[i].FindControl("DropDownList2");  //这就是第二个drop down list