I have a DataTable with 2 columns called "ID" and "Software" that I have used as a DataSource for a lst_Software multiselect listbox.
I'm trying to gather the ID for all the selected items in that have been selected and place that in an int[] array.
Listbox setup:
lst_Software.DataSource = software; //software is a DataTable
lst_Software.DisplayMember = "Software";
lst_Software.ValueMember = "ID";
I've tried below
List<int> list = new List<int>();
for (int i = 0; i < lst_Software.SelectedItems.Count; i++)
{
list.Add(Convert.ToInt32(lst_Software.SelectedValue.ToString()));
}
int[] software = list.ToArray();
I'm finding that I'm only getting the first selected value except it will not iterate through all... I know why though. I'm not using i to get passed through inside the for loop. I'm hoping someone can give me a direction to go to iterate through all the selected values.
Thank you
You're using lst_Software.SelectedValue.ToString() here in the loop so it only returns the one item. You have a for loop but you're not using the index variable. However, all of this is unnecessary really all you need is;
var items = lst_Software.SelectItems;
As that property is already the list of selected items. From there you can cast/convert them as you please.
SelectedValue is just the first selected value. You need to use SelectedItems.
Related
I want to add the 'OrderID' in the below DataGridView to a list for each row that I select.
I have managed to get it to cycle through a foreach loop but I am trying to figure out how to specify the selected row index, as it currently pulls the same indexed 'OrderID' and not the different ones selected.
See result I am currently getting:
See current code:
List<string> orders = new List<string>();
foreach (var row in grid.SelectedRows)
{
orders.Add(grid.Rows[grid.SelectedRows[0].Index].Cells[0].Value.ToString());
}
I know I shouldn't be using SelectedRows[0], but I cant think of how to index the specific 'row'
I used a for loop instead:
for (int i = 0; i < grid.SelectedRows.Count; i++)
{
orders.Add((grid.Rows[i].Cells["Order ID"].Value).ToString());
}
C# How to prevent ListBox in MultiSimple selection mode to select first item automatically, when you unselect the last one selected item in the box - it happens only if listbox represented by my own class objects, and everything is ok when it represented by string objects. Thnx!
It seems like keeping track of the order of the list is the most important part. I would suggest maybe making an array of a struct. You can make this struct contain whatever you want for example:
struct itemList
{
public string itemName;
public int itemIndex;
//include whatever other variables that you need included in here.
}
Also, make sure you place your struct before the namespace. Then, making the array of the struct would look like this:
itemList[] items1 = new itemList[listBoxName.SelectedItems.Count];
All you would have to do then is to add the items to the array before you reorder the listBox
for (int i = 0; i < listBoxName.SelectedItems.Count; i++)
{
items1[i].itemName = listBoxName.SelectedItems[i].ToString();
items1[i].itemIndex = listBoxName.SelectedIndices[i];
}
Thank you very much, but i already use some like this. I don't understand why first item of listBox selected everytime i assign preloaded list as DataSource. I resolve this problem, by making another one temporal List of my object class, which items readed from binary file, and then push them one by one to my original List in foreach cycle by listOfObjects.Add(object) method. I know that every time after code ListOfTags.DataSource = null;
ListOfTags.DataSource = tags;
ListOfTags.DisplayMember = "Name"; if my tags (it is a List) are preloaded even in code (for example if i write code List<Tag> tags = new List<Tag> {new Tag("1"),new Tag("2"), new Tag("3")}; , this takes situation when first item of listbox selected, and starts selects everytime after that when i try do deselect last selected item in listBox.
I have a ListView with three columns and I have added 4 records in the ListView.
I would like to get the value of the 2nd column value of each record. How to implement it?
var vals = listView1.Items.Cast<ListViewItem>().Select(lvi => lvi.SubItems[1].Text);
// Convert items to an IEnumerable for LINQ usage
ListViewItem[] items = new ListViewItem[4];
listView.Items.CopyTo(items, 0);
// Use LINQ to get values
IEnumerable<string> secondColumnValues = items.Select(_ => _.SubItems[1].Text);
loop through all the rows and try this.
Subitems[columnnumber] will be the column numbr of the required field
lv.Items[i].SubItems[1].Text
foreach(ListViewItem itm in listView1.Items){ itm.Text/*first column of the particular item*/ itm.SubItem[1].Text/*second column of the particular item*/}
since foreach loop can access more fast than for loop
I am trying to retrieve the value of a dropdown list by specifying its indexid but I cant seem to find a way to accomplish this. I dont want it to be the selected value either, basically I need to go through the list, and then add it to an array. I can find all kinds of ways to get it based on the value but not by the index id anyone know how to do this? Im using c#.
string valueAtIndex = myComboBox.Items[SomeIndexValue].Value;
Have you tried lstWhatever.SelectedIndex?
Since the object supports IEnumerable, you can use foreach to loop through, if that is your intention.
Is this what you are looking for?
List<String> theList = new List<string>();
foreach (var item in comboBox1.Items)
{
theList.Add(item.ToString());
}
Where comboBox1 is your dropdown list and i assumed you are talking about a windows forms project.
Or, if you want to us Index Id:
List<string> theList = new List<string>();
for (int i = 0; i < comboBox1.Items.Count; i++)
{
theList.Add(comboBox1.Items[i].ToString());
}
String valueAtIndex = myComboBox.Items[myComboBox.SelectedIndex].ToString();
How can I select multiple items from the listbox in a Windows Phone 7 application?
e.g
listboxName.SelectedIndex = 0;
listboxName.SelectedIndex = 1;
listboxName.SelectedIndex = 2;
The above code selects 2 while I need to select all three of those.
The values I need to preselect are given to me in a array like
{true,true,true,false,false}
So I tried the using IsSelected like shown below... doesn't work.
int i = 0;
foreach (ListBoxItem currentItem in listboxName.SelectedItems)
{
if (tagindexeselected[i])
{
currentItem.IsSelected = true;
}
i++;
}
What would be the proper way to select multiple items in a listbox?
Hard to say there's a single, best way - it depends on how you're populating your list box, etc. First, be sure your list box's Selection Mode is set to Multiple or Extended.
One option is to use the ListBox's SelectedItems collection:
listBox1.SelectedItems.Add(listBox1.Items[0]);
listBox1.SelectedItems.Add(listBox1.Items[1]);
listBox1.SelectedItems.Add(listBox1.Items[2]);
Note also, in your example above, you're iterating over the SelectedItems collection - not the Items collection. If nothing is selected, that's an empty collection. Also, if your list box ItemsSource is not a series of ListBox Items (you can set your itemsSource to almost any enumeration), you will get an InvalidCastException when you go to run your foreach statement.
foreach (DataRowView item in lstServer.SelectedItems)
{
string WebServerIP = item[lstServer.DisplayMember].ToString();
string WebServerUrl = item[lstServer.ValueMember].ToString();
_WebObjIgent.Url = WebServerUrl;
}
Note : lstServer is Listbox of window application . By using Displaymember and valuemember proprty you can access value and text of listbox.