我前面说“再使有一个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
在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();
}
}
我在datagrid中有两个DropDownList 我要根据其中一个DropDownList的selectitem 动态生成另一个DropDownList的item
这个事件在DataGrid没生成新的一行时触发,你可以e.Item.Cells[i]找到第一个DropDownList,然后根据这个DropDownList的SelectItem来帮定数据到另外一个DropDownList。我用这种方法动态的帮定数据到DataGrid的DropDownList中,试一下吧,好运
参照我前面回复中的代码,在datagrid_itemdatabind中绑定到第一个dropdownlist,并用其当前选择项的值绑定第二个列表框。同时为当前处理编辑状态下的item中的第一个下拉列表框添加一个事件处理句柄,指向一个名为dropdownlist_selectedindexchanged事件(请参考InitialComponent()中的代码). 再使有一个ViewState变量来保存当前的datagrid的item的索引值。
最后生成一个dropdownlist_selectedindexchanged事件,在其中使用viewstate中的值确定datagrid中的编辑行,重新绑定dropdownlist2.
spring_ok(SpringDotNet) 高手:
怎么做事件的指向?
DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList_SelectedIndexChanged);
private void DropDownList_SelectedIndexChanged(Object sender, EventArgs e)
{
//处理代码
}
在this.DropDownList_SelectedIndexChanged事件中
怎么获取dropdownlist的选择的值哪?
DataGrid.Tables[tablename].Rows[row][col].Controls[1]就是那个控件。
最后生成一个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