2013-03-15 42 views
1

我的Gridview有一個類別列。 此列編輯模板有一個DropDownList。 的下拉列表數據源:gridview奇數行索引不顯示數據

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(); 
    } 
} 

所以問題是奇數行索引下拉列表爲空,但偶數行的索引工作正常。什麼是問題?我看不到它。

回答

1

您會注意到DataControlRowState上設置了FlagsAttribute。如果設置了AlternatingRowStyle,則交替行將包含RowState,包括標記DataControlRowState.Alternate

拉昇DataControlRowState.Edit檢查標誌不管其他標誌的,通過改變你的if聲明:

if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit && e.Row.RowType == DataControlRowType.DataRow) 

添加位運算符檢查只爲Edit標誌值。

+0

我改變,如果語句那麼它的作品。感謝很多 – ir2d2 2013-03-15 20:39:50