简体   繁体   中英

Assigning Values to DropDownList in Grid

I have a DropDownList in a Grid and I have a foreach loop that loops through a Query and matches data and fills the drop down list for each row on that. But It is putting the data all in the same dropdownlist. So row 1, row 2, row 3, etc. all get combined into the dropdownlist. I am trying to make it so in row 1 dropdlownlist has the info for row 1 and row 2 the dropdownlist has the info for row 2 and so on.

I have this so far:

protected void grdExceptions_ItemDataBound(object sender, GridItemEventArgs e)
  {
    var linqContext = new DataClassesDataContext();
    var sessionleads = from q in linqContext.Leads where q.SessionID == SessionId orderby q.RowIndex select q;

    if (e.Item is GridDataItem)
      {
        foreach(var p in sessionleads)
          {
            var lQuery = (from a in gServiceContext.CreateQuery("account") where (a["name"].Equals(p.AccountName) &&
                                         a["address1_postalcode"].Equals(p.ZipCode) &&
                                         a["address1_stateorprovince"].Equals(p.State)) ||
                                        (a["address1_line1"].Equals(p.Address1) &&
                                         a["address1_postalcode"].Equals(p.ZipCode) &&
                                         a["address1_city"].Equals(p.City))
          select new
          {
            Name = !a.Contains("name") ? string.Empty : a["name"]
          });
          foreach (var a in lQuery)
            {
              ((e.Item as GridDataItem)["Account"].FindControl("AccountList") as DropDownList).Items.Add(new ListItem(a.Name.ToString()));
            }
          }
    }
  }

I have a DropDownlist in the ItemTemplate of the Grid. So for the first row I would want whatever matches are found assigned to the drop downlist in the first row, for the second row I would want whatever matches the dropdownlist in the second row, etc.

What am I doing wrong?

Thanks!

It seems like you're expecting SessionId to somehow be different when ItemDataBound fires for each new row, but it's just staying the same. Do you have some other filter criteria you could add to your where clause? Or some way to update SessionId for each new row?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM