假设我的gridview中有模板化的下拉列表(它与所有行绑定) 下拉列表通过数组填充..
//Fill Array
private ArrayList GetDummyData()
{
ArrayList arr = new ArrayList();
arr.Add(new ListItem("Item1", "1"));
arr.Add(new ListItem("Item2", "2"));
arr.Add(new ListItem("Item3", "3"));
return arr;
}
//Fill dropdownlist
private void FillDropDownList(DropDownList ddl)
{
ArrayList arr = GetDummyData();
foreach (ListItem item in arr)
{
ddl.Items.Add(item);
}
}
我想要做的是在gridview row [0]中选择“Item1”,因此在行[1]中只剩下2个选项 - > “Item2”和Item3“
非常感谢帮助。 :)
答案 0 :(得分:2)
您可以处理RowDataBound事件。
例如(未经测试,假设DataSource是DataTable,而DropDownList的ID是ddl
):
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var thisRow = (DataRowView)e.Row.DataItem;
var source = thisRow.DataView;
var lastRowIndex = e.Row.DataItemIndex -1;
DataRowView lastRow = null;
var ddl = (DropDownList)e.Item.FindControl("ddl");
DropDownList ddlLast = null;
if(lastRowIndex>=0){
lastRow = source[lastRowIndex];
ddlLast = (DropDownList)((GridView)sender).Rows[lastRowIndex].FindControl("ddl");
//remove the items of this ddl according to the items of the last dll
}
}
}
如果您启用了分页,则应该考虑此示例不起作用,因为Rows-property仅返回当前页面的GridViewRows。
编辑:也许更好的方法是处理DropDownList的SelectedIndexChanged事件并更新以下任何下拉列表的项目列表:
protected void DdlSelected(object sender, EventArgs e)
{
var ddl = (DropDownList)sender;
var row = (GridViewRow)ddl.NamingContainer;
var grid = (GridView)row.NamingContainer;
var index = row.RowIndex + 1;
while (index < grid.Rows.Count) {
var nextRow = grid.Rows[index];
var nextDdl = (DropDownList)nextRow.FindControl("ddl");
nextDdl.Items.Clear();
foreach (ListItem item in getDllSource()) {
if (ddl.SelectedItem == null || !ddl.SelectedItem.Equals(item)) {
nextDdl.Items.Add(item);
}
}
index += 1;
}
}
getDllSource
遵循以下功能:
private List<ListItem> getDllSource()
{
List<ListItem> items = new List<ListItem>();
ListItem item = new ListItem("Item1", "1");
items.Add(item);
item = new ListItem("Item2", "2");
items.Add(item);
item = new ListItem("Item3", "3");
items.Add(item);
return items;
}