How to loop throw asp radiobuttonlist and set specific parameter? - c#

I find this question but it could be deprecated and is not resolve my problem at all.
I consider how to loop throw asp:radiobuttonlist which is already bound with data and set visible parameter of specific item. For example I have five items in a radiobuttonlist and I want to make visible only item 1 and item 4.

You could store the items you want to keep, then clear the items and add the stored items:
int[] keepIndexes = { 0, 3 }; // item 1 and 4
ListItem[] keepItems = keepIndexes.Select(i => rbl.Items[i]).ToArray();
rbl.Items.Clear();
rbl.Items.AddRange(keepItems);
This is the approach if you really want to make them "invisible" since there is no property Visible in ListItem. But maybe you are confusing it with Enabled then this is more appropriate:
for (int i = 0; i < rbl.Items.Count; i++)
rbl.Items[i].Enabled = keepIndexes.Contains(i);

Related

How to retain listbox selected items values on multiple clicks?

for (int i = 0; i < lboxavilableInsName.Items.Count; i++)
{
if (lboxavilableInsName.Items[i].Selected)
{
if (!arraylist1.Contains(lboxavilableInsName.Items[i]))
{
arraylist1.Add(lboxavilableInsName.Items[i]);
arrUpdatedInsValues.Add(lboxavilableInsName.Items[i].Value);
arrUpdatedInsNames.Add(lboxavilableInsName.Items[i].Text);
}
ViewState["UpdatedInsValues"] = arrUpdatedInsValues;
arrUpdatedInsValuestotal = (ArrayList)ViewState["UpdatedInsValues"];
ViewState["UpdatedInsValues2"] = `enter code here`arrUpdatedInsValuestotal;
ViewState["UpdatedInsNames"] = arrUpdatedInsNames;
}
}
Actually I have given selsectionmode="Multiple" in the listbox. That will select me multiple items when I select first time or subsequent time after page gets loaded, but I want that in a code behind saying ex: if I select 2 items 1st time that 2 items will added in the second listbox and I will get the values of those selected items.
If again I select any items after adding previous selection items in 2nd listbox, I want the item value selected at 2nd time along with first two item values. So totally 3 values I want. and I need to send that values to the stored procedure to insert.
Its because you are assigning new values each time to the 2nd listbox, you don't need to assign them, you need to add values within your 2nd listbox, in this way your 2nd listbox will retain all the previous and the new values as well.
Hope it helps.

How to get multiple selected values from listbox C# Windows Application

I am using a Listbox on my windows application. The listbox has a multi-selection option, which users select all the roles the new user will have.
My Listbox is bound to a dataset. The table is Roles, with an ID column and Description column.
When I bound the listbox, I chose the data source being the dataset, the display member to be the description, the value member being the ID and the selected value being the dataset - roles.frolesid
Now when i loop through the listbox's selecteditems, i get only the first item's values etc...
I am trying to assign an int to the current selectedvalue, to get the ID to give to the user.
int num = 0;
foreach (var item2 in lstRoles.SelectedItems)
{
num = Convert.ToInt32(lstRoles.SelectedValue);
}
So if I select 3 roles, for instance Administrator, Capturer and Assessor, I only see the Administrator value.
How do I loop and access the next item? I'm not even using
item2
in my code. Could this be utilised?
For instance:
First iteration:
And it just stays on that selected item, never moving to the next item.
To get list of selected values when the data source is a DataTable, you can use this code:
var selectedValues = listBox1.SelectedItems.Cast<DataRowView>()
.Select(dr => (int)(dr[listBox1.ValueMember]))
.ToList();
But in general Item of a ListBox control may be DataRowView, Complex Objects, Anonymous types, primary types and other types. Underlying value of an item should be calculated base on ValueMember.
If you are looking for a good solution which works with different settings and different data sources, you may find this post helpful. It contains an extension method which returns value of an item. It works like GetItemText method of ListBox.
solved it
int num = 0;
int count = fRoleIDs.Count;
while (num != count)
{
var item = lstRoles.Items[num] as System.Data.DataRowView;
int id = Convert.ToInt32(item.Row.ItemArray[0]);
num += 1;
}
This goes into the listbox's items and according to the index, gets the item as a Row. This then gets me the itemarray, which is ID and Description {0,1}

Save content from dynamically generated integer up downs to a list

My app is asking user to input a number of items, which generates that many IntegerUpDown controls in a StackPanel. The user must choose a number in each of them. Then, I want to save those values into a list. How do I construct this and save date from changing number of items?
for(int i=0; i< numberOfItems; i++)
{
iud=new IntegerUpDown();
stackPanel1.Children.Add(iud);
}
This part works fine but I don't know how to bind this changeable number of IntegerUpDowns controls to some list and get all the values at once.
<StackPanel Name="stackPanel1" Grid.Row="4" Grid.Column="1">
</StackPanel>
Example: user inputs 3 and this generates 3 IntegerUpDowns. Then the user inputs 5, 14, 38 in each IntegerUpDown. I want to save those numbers in the List<int>.
I hope this is not too confusing. I don't know terminology so well so I don't know how to look for the solution to this problem.
Get all the controls from the StackPanel that are of the type IntegerUpDown. Then, loop over them and get their value. In below code, ControlsStackPanel is the name of the StackPanel.
var values = new List<int>();
var children = ControlsStackPanel.Children.OfType<IntegerUpDown>();
foreach (var child in children)
{
values.Add(child.Value.Value);
}
Or shorter:
var values = new List<int>();
foreach (var child in ControlsStackPanel.Children.OfType<IntegerUpDown>())
values.Add(child.Value.Value);
Or even in one line using LinQs Select:
var values = ControlsStackPanel.Children
.OfType<IntegerUpDown>()
.Select(x => x.Value.Value);
Note the double .Value. This is because the value of the IntegerUpDown is a nullable integer and the second .Value gets the integer value of that nullable. If the IntegerUpDown doesn't have a value and you call this, you'll get an InvalidOperationException when trying to add it to the list!

Multi value ListBox automatically selects undesired item

I have an object that has some attributes from the list selected - let's say a Promotion that can have 0 to X communication channels. To display/edit this information I am using a listbox with option SelectionMode==MultiExtended.
But in some cases it is behaving strangely
I have Promotion with 2 communication channels selected (first and last out of three channels),
I click on a second channel (that previously was the only unselected channel) and know it shows, that 1st and 2nd channels are selected (I placed a check at the beginning of the listbox SelectedIndexChanged event - and it shows that SelectedItems.Count==2, although I clicked on a single item not holding Ctrl or Shift keys) and in this case SelectedIndexChanged event is triggered twice in all other cases it is triggered just once
This happens only after the first time I open this dialogform, if I manually select 1st and 3rd item of Channels, and then click on the 2nd item - then it works properly
Screencast of a problem in action
http://screencast.com/t/lVs0e9oau
This is how I load list of all possible channels into listbox
foreach (var ct in Promotion_operations.Configuration.PromoCommunicationTypes)
{
KeyValuePair<string, PromotionCommunicationType> nct =
new KeyValuePair<string, PromotionCommunicationType>(ct.Name, ct);
communications.Add(nct);
}
PromotionCommunicationList.DataSource = communications; //Promotion_operations.Configuration.PromoCommunicationTypes;
PromotionCommunicationList.DisplayMember = "Key";
PromotionCommunicationList.ValueMember = "Value";
This is how I load selecteditems based on Promotion's data
private void LoadSelectedCommunicationsList(ListBox lstbox, List<PromotionCommunication> communications)
{
lstbox.SelectedItems.Clear();
foreach (var ct in communications)
{
for (int j = 0; j < lstbox.Items.Count; j++)
{
if (ct.CommunicationType.Id == ((KeyValuePair<string, PromotionCommunicationType>)lstbox.Items[j]).Value.Id)
{
lstbox.SelectedItems.Add(lstbox.Items[j]);
}
}
}
}
What could be the cause of this behaviour?
that clicking on one previously unselected list selects both - newly selected item and first item of the list?
Your PromotionCommunicationList and HistoryCommunicationList are sharing the same reference to your list of objects as DataSource. That said, they have the same BindingContext and share the same CurrencyManager. CurrencyManager is remembering selected items of your ListBox control and that's where your conflict is created because he's saving selected items of both of your ListBoxes. You already found the solution for your problem because new CurrencyManager is created when you set "different" list (the copy of your original one) as DataSource. Another possible solution would be the creation of new BindingContext for one of your ListBox controls.
You can try this out:
PromotionCommunicationList.DataSource = communications;
(..)
HistoryCommunicationList.BindingContext = new BindingContext(); // Add this
HistoryCommunicationList.DataSource = communications;
It should solve your problem. For more information about BindingContext check this link on MSDN.
I found the cause of the problem, though I don't really understand why it caused such a behaviour (if someone will answer that question, I will accept it as an answer to this question)
I had 2 listbox-es in my form and both of them where using the same collection as a Datasource, BUT!!! SelectedItems was selected using code (acctually it seems that in winforms it is not possible to databind listbox's selecteditems)
INITIALLY My code was:
PromotionCommunicationList.DataSource = communications;
(..)
HistoryCommunicationList.DataSource = communications;
Corrected version is:
PromotionCommunicationList.DataSource = communications.ToList();
(..)
HistoryCommunicationList.DataSource = communications.ToList();
I know that ToList() makes a copy, but I don't understand what's wrong with having the same collection as DataSource for list items of 2 listbox-es? Why does this have an impact on SelectedItems collection?

How do you select multiple items in ListView from code

I am working with the new Windows 8 ListView-control. I have a list of users which are selected, depending on another list of users stored elsewhere.
Perhaps my situation is a bit specific, but my question is quite simple:
How do I select several items in a ListView-object from code?
Yout list view has property called Items
yourList.Items
This is a Collection of your items that are bind to the List. If you know indexes (or other uniq value) you can find them. If you have a list of indexes just take each from the list
yourList.Items.ElementAt(index);
If you know an Id or Name of your item or other field you can make a loop where you`ll seek them in Items collection
You can use the .SelectedItems property. Here is a simple example that fills a listview then marks the items at index 4 and higher:
for (var i = 0; i <= 10; i++)
{
if (mylistview.Items != null) mylistview.Items.Add("Item at index "+i);
}
if (mylistview.Items != null)
{
for (var i = 0; i <= mylistview.Items.Count - 1; i++)
{
if (i > 4)
{
mylistview.SelectedItems.Add(mylistview.Items[i]);
}
}
}

Categories

Resources