我的 Gridview 有一个类别列。此列编辑模板有一个 DropDownList。下拉列表数据源:
protected void grdProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit && e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ds = (DropDownList)e.Row.FindControl("ddlCategory");
int ids = Convert.ToInt16(((Label)e.Row.FindControl("LID")).Text);
ds.DataTextField = "Name";
ds.DataValueField = "ID";
ds.DataSource = model.Categories.ToList();
ds.DataBind();
var idBynames = model.Products.Where(s => s.ID == ids).FirstOrDefault();
string names = idBynames.CategoryName;
var categorys = model.Categories.Where(s => s.Name == names).FirstOrDefault();
ds.SelectedValue = categorys.ID.ToString();
}
}
所以问题是奇数行索引下拉列表为空,但偶数行索引工作正常。什么是问题?我看不到它。