你可以处理RowDataBound事件。
例如(未测试,asssuming的数据源是一个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属性只返回当前页面的GridViewRows。
编辑:也许一个更好的办法是处理DropDownList的SelectedIndexChanged事件和更新任何下列下拉菜单的ITEMLIST:
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;
}
'行[ 0]'和'row [1]'是一行的列还是不同的行('rows [0]')? – 2011-12-19 09:43:23
不同的行..相同的列..(列下拉列表中的项目模板所在的位置) – rofans91 2011-12-19 09:46:10