I'm trying to select values in a CheckBoxList control based on a data source. I have five items in the CheckBoxList and three items in the data source, but in the loop I only get one item selected.
if (ddlUserId.SelectedIndex != 0)
{
RoleDetails rd;
rd = CatalogAccess.GetSingleUserRole(ddlUserId.SelectedValue.ToString());
for (int i = 0; i < cblRoles.Items.Count; i++)
{
cblRoles.Items.FindByValue(rd.RoleID.ToString()).Selected = true;
}
}
I tried this, but it still selects only one item:
RoleDetails rd;
for (int i = 0; i < cblRoles.Items.Count; i++)
{
rd = CatalogAccess.GetSingleUserRole(ddlUserId.SelectedValue.ToString());
if (cblRoles.Items[i].Value == rd.RoleID.ToString())
cblRoles.Items[i].Selected = true;
}
CheckboxList bind code
cblRoles.DataSource = CatalogAccess.GetRoles();
cblRoles.DataTextField = "RoleDetails";
cblRoles.DataValueField = "RoleId";
cblRoles.DataBind();
When you use for loop you need to use index value (Here it is "i"), like
for (int i = 0; i < cblRoles.Items.Count; i++)
{
if(cblRoles.Items[i].Value == rd.RoleID.ToString())
cblRoles.Items[i].Selected = true;
}
Or you can use foreach as below:
Here i have created looping through items of checkbox list using foreach & item will be made selected id its value will match RoleId .
foreach (ListItem li in cblRoles.Items)
{
if (rd.RoleID.ToString() == li.Value)
{
li.Selected = true;
}
else
{
li.Selected = false;
}
}
Related
I'm trying to modify this code. I need to check for a value in a specific element of FilteredCheckObservable and if true, change the value of another part of that element.
Basically something like
if (FilteredCheckObservable items.Lang = 'ENG')
{items.check = newcheckname;}
Then this will update the sourceGroups Collection.
if (string.IsNullOrEmpty(departmentLine.LineID) || string.IsNullOrEmpty(departmentLine.LineName))
continue;
bool discovered = false;
foreach (var group in sourceGroups)
{
if (!group.Key.Line.IsEqual(departmentLine))
continue;
group.Key.IsDiscovered = true;
discovered = true;
group.Key.ScheduleStatusCount = group.CountGroupItem;
break;
}
if (discovered == false)
{
var _ScheduleItemObservable = new ScheduleItemObservable(departmentLine, MainViewViewModel, Shift.ToString());
var item = new Grouping<ScheduleItemObservable, FilteredCheckObservable>(_ScheduleItemObservable);
if (IsShiftValid)
{
item.Key.Shift = Shift.ToString();
item.Key.IsHistoryEnabled = true;
}
sourceGroups.Add(item);
}
for (int index = 0; index < sourceGroups.Count; index++)
{
if (sourceGroups[index].Key.IsDiscovered == true)
{
foreach (var group in sourceGroups)
{
foreach (FilteredCheckObservable items in group)
{
if (items.Lang_ID == LanguageService.Instance.LanguageType.ToString())
{
sourceGroups.Clear();
sourceGroups.Add(item);
}
}
}
}
}
Welcome Travis,
I would wager the guilty culprit is your foreach loop.
https://stackoverflow.com/a/759985/3403999
You aren't allowed to modify a collection inside of a foreach loop.
I don't know if your collection has an indexer, but if it does, you can convert to a for loop:
for (int i = 0; i < sourceGroups.Count; i++)
{
var group = sourceGroups[i];
// The rest of your code.
I could be wrong. You do say you are trying to modify some existing code. Is it some code that is online? Maybe you could link to it to provide a full context.
Based on your new snippet, you need two for loops:
for (int index = 0; index < sourceGroups.Count; index++)
{
if (sourceGroups[index].Key.IsDiscovered == true)
{
//foreach (var group in sourceGroups)
for (int j = 0; j < sourceGroups.Count; j++)
{
var group = sourceGroups[j];
foreach (FilteredCheckObservable items in group)
{
if (items.Lang_ID == LanguageService.Instance.LanguageType.ToString())
{
sourceGroups.Clear();
sourceGroups.Add(item);
}
}
}
}
}
^ Although that might still be a bad loop. Primarily because you have sourceGroups.Clear();.
What you might be better off doing is creating an internal collection called say 'results'. Do your loop looking for your conditions, and if they meet, add that item to the results collection.
Once your loops terminate, then call sourceGroups.Clear(), and then sourceGroups.AddRange(results). If sourceGroups doesn't have an AddRange, then one final loop of:
foreach (var group in results) { sourceGroups.Add(group); }
using winform I am trying to display from database the fooditems that are grouped under categories into a radlistview using that below code; but the Problem that it is showing the main Groups but not showing the items under it.
ZalaqEntities1 zlq=new ZalaqEntities1();
List<cat> cats = zlq.cats.ToList();
radListView1.Items.Clear();
radListView1.Groups.Clear();
radListView1.EnableGrouping = true;
radListView1.ShowGroups = true;
for (int i = 0; i < cats.Count(); i++)
{
radListView1.Groups.Add(new ListViewDataItemGroup());
radListView1.Groups[i].Value = cats[i].catname;
radListView1.Groups[i].Key = cats[i].catid;
}
List<fooditem> fooditems = zlq.fooditems1.ToList();
for (int j = 0; j < fooditems.Count(); j++)
{
ListViewDataItem myitem = new ListViewDataItem();
myitem.Value = fooditems[j].itemname;
myitem.Key = fooditems[j].itemid;
// myitem.Group = radListView1.Groups[1];
radListView1.Items.Add(myitem);
myitem.Group = radListView1.Groups.Where(i => i.Key == fooditems[j].catid).FirstOrDefault();
//radListView1.Items[j].Group = radListView1.Groups[1];
}
radListView1.Refresh();
I am trying to add the Item under its category using the where condition to find the correct group by its Key.
If I do this outside for Loop it was working for adding several Items.but from database inside the for loop non.
The Problem was just by casting the i.Key.ToString() inside the Where Clause ..
the Final Code :
ZalaqEntities1 zlq=new ZalaqEntities1();
List<cat> cats = zlq.cats.ToList();
radListView1.Items.Clear();
radListView1.Groups.Clear();
radListView1.EnableGrouping = true;
radListView1.ShowGroups = true;
for (int i = 0; i < cats.Count(); i++)
{
ListViewDataItemGroup grp = new ListViewDataItemGroup();
grp.Value = cats[i].catname;
grp.Key = cats[i].catid.ToString();
radListView1.Groups.Add(grp);
radListView1.Groups[i].Key = cats[i].catid;
}
radListView1.Refresh();
List<fooditem> mfooditems = zlq.fooditems1.ToList();
for (int j = 0; j < mfooditems.Count(); j++)
{
ListViewDataItem myitem = new ListViewDataItem();
myitem.Value = mfooditems[j].itemname;
myitem.Key = mfooditems[j].itemid;
radListView1.Items.Add(myitem);
radListView1.Refresh();
radListView1.Items[j].Group = radListView1.Groups.Where(i => i.Key.ToString() == mfooditems[j].catid.ToString()).FirstOrDefault();
}
radListView1.Refresh();
What I am trying to do is that I want to update the status of any checked item from gridview.
My Error:
Server Error in '/' Application.
Input string was not in a correct format.
protected void btnSubmit_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
DropDownList drpDwnStat = (DropDownList)GridView1.Rows[i].FindControl("drpDwnStatus");
CheckBox chkBox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists.TryGetList("List");
if (list != null)
{
if (chkBox.Checked == true)
{
GridViewRow row = (GridViewRow)chkBox.NamingContainer;
int index = row.RowIndex;
SPListItem newStat = list.Items.GetItemById(int.Parse(GridView1.Rows[index].Cells[1].Text));
{
web.AllowUnsafeUpdates = true;
newStat["Status"] = drpDwnStat.SelectedItem.Text;
newStat.Update();
web.AllowUnsafeUpdates = false;
}
}
}
}
}
}
}
This line:
SPListItem newStat = list.Items.GetItemById(int.Parse(GridView1.Rows[index].Cells[1].Text))
You seem to get the int value from Cells[1]. But according to your picture, the int text ID is in Cells[0] (which is the first column in the row).
Cells[1] refer to the second column:
Diryas
Soban
Attiq
test
Cells[0] refer to the first column:
3
4
5
8
Okay with the help of Mr.Ian, I found my error and here is the correct code to get item ID.
//NOTE: Rows[i] this i is from the loop (int i = 0; i < GridView1.Rows.Count; i++)
int justtocheck = Convert.ToInt32(GridView1.Rows[i].Cells[0].Text);
SPListItem newStat = list.Items.GetItemById(justtocheck);
{
web.AllowUnsafeUpdates = true;
newStat["Status"] = drpDwnStat.SelectedItem.Text;
newStat.Update();
web.AllowUnsafeUpdates = false;
}
I have a list of postcodeitems and I bind this to a listbox.
It contains two properties : postcode, area.
I want to loop through the listbox items and if the item is selected, add the postcodeitem object to another list.
List<Valueobjects.postcodeitem> temp = _BL.GetPostCodeAreasFromZones();
PCList.AddRange(temp);
ListBox1.DataSource = PCList;
ListBox1.DataBind();
List<Valueobjects.postcodeitem> postcodecollection = new List<Valueobjects.postcodeitem>();
foreach (ListItem listitem in ListBox1.Items)
{
if (listitem.Selected)
{
i = i + 1;
//Run at 20 to speed up query
if (i == 20)
{
//Get data
CustList.AddRange(BL.SearchCustomerByPostcodeArea(postcodecollection,2));
i = 0;
}
else
{
//add the post code to temp list
postcodecollection.Add(listitem);
}
}
}
if (i > 0)
{
//get data
CustList.AddRange(BL.SearchCustomerByPostcodeArea(postcodecollection,2));
}
}
Obviously where I am trying to add (listitem) isnt going to work as this is a list item and not a postcodeitem. My question is how do I get the postcodeitem object within the list where the list item is selected?
thanks
Try something like following.
IList<Employee> boundList = ListBox1.DataSource
var obj = boundList[ListBox1.SelectedIndex]
Update => I have not tested the code but something like following. Using for loop to track the element index.
for (int i = 0; i< ListBox1.Items.Length; i++)
{
if (ListBox1.Items[i].Selected)
{
i = i + 1;
//Run at 20 to speed up query
if (i == 20)
{
//Get data
CustList.AddRange(BL.SearchCustomerByPostcodeArea(postcodecollection,2));
i = 0;
}
else
{
//add the post code to temp list
postcodecollection.Add(ListBox1.DataSource.ToList().ElementAt(i));
}
}
}
if (i > 0)
{
//get data
CustList.AddRange(BL.SearchCustomerByPostcodeArea(postcodecollection,2));
}
}
Anyways this is not recommended way. You should get selected value and use that selected value(unique field) to fetch relevant data from any persistant storage like database.
I have these checkboxlist each cell of a gridview. Now, I am trying to get the selected items each checkboxlist but it failed. Any help please! Thanks!
foreach (GridViewRow gvRow in gvReg.Rows)
{
for (int ctr = 0; ctr <= 4 - 1; ctr++)
{
if (ctr == 0)
{
szCheckBoxListName = "cblMultiSelect";
szRegionName = "lblRegionName";
}
else
{
szCheckBoxListName = "cblMultiSelect" + ctr;
szRegionName = "lblRegionName" + ctr;
}
cbl=(CheckBoxList)gvRow.Cells[ctr].FindControl(szCheckBoxListName);
if (cbl.Items.Count > 0)
{
foreach (ListItem li in cbl.Items)
{
if (li.Selected)
{
iItemCount = iItemCount + 1;
}
}
}
}
}
itemCount returns zero always even if I have selected several items on those checkboxlists.
Are you data binding on Page_Load method? If yes, you must do this:
if(!IsPostBack)
{
GridView1.DataSource = Your Datas;
}