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();
Related
I'm having some issues with my programm in c#.
Basically I have a list called mainList with 3 items in it. First two items are integers, but third one is another list containing more items.
mainList[0] = 8;
mainList[1] = 1;
mainList[2] = list;
By using foreach loop I'm able to print all of those items.
foreach (var i in (dynamic)(mainList[2]))
{
Console.WriteLine(i);
}
However I don't know how to access them. The thing is that I can't use indexes, because it is not an array. I would like to do something like this:
foreach (var i in (dynamic)(mainList[2]))
{
// First item is in datetime type, so I would like to change it to int
Console.WriteLine(Convert.ToInt64(i[1]));
}
Is there a way to access items inside list like we do it in arrays with a help of indexes?
Lists support the same index-based access as arrays, so you can use
mainList[n]
to access the nth entry in mainList.
I guess you are looking for something like this, although it is really unclear what you're asking:
foreach(var item in mainList)
{
if(item is System.Collections.IEnumerable)
{
foreach(var obj in ((System.Collections.IEnumerable)item))
{
Console.WriteLine(obj);
}
}
}
I am working with a project modeled on this link: Databinding with WinForms
On the form load on the link, the tutorial set the binding to:
this.categoryBindingSource.DataSource =
_context.Categories.Local.ToBindingList();
my question is, is it possible to iterate rows and columns on DBlocal? _context.Categories.Local? If Yes, how?
Thanks in Advance.
DbSet<T>.Local property is ObservableCollection<T> and you can simply use a for/foreach loop. You can use either of these options:
foreach (Category item in context.Category.Local)
{
//MessageBox.Show(item.Name);
}
for (int i = 0; i < context.Category.Local.Count; i++)
{
var item = context.Category.Local[i];
//MessageBox.Show(item.Name);
}
context.Category.Local.ToList()
.ForEach(item =>
{
//MessageBox.Show(item.Name);
});
Don't forget to first load data to Local.
I have struggled with figuring out how to find an item in a list by checking for a property and then isolating and naming this item. I need to do this to set a value that corresponds with that specific string. It wouldn't work if I just found that there is one, or several items in the list that match the property. My code is massive, but here's a snippet of it:
List<string> diag_right = new List<string>();
diag_right.Add(b3); //Add the string b3
diag_right.Add(b5); //Add the string b5
diag_right.Add(b7); //Add the string b7
if (diag_right.Exists(a => a.Equals("")))
{
//Find the item (string name, b3, b5, etc.) that was found as matching the blank property
}
Is it possible to do this? I know I can do this by checking to see if each string matches this property individually, but is there a faster way to do this?
Many ways this can be achieved, I prefer simple approach, loop collection using index and modify matching field
for(int index=0; index< diag_right.Count(); index++)
{
if(diag_right[index] == "b3")
{
// update
diag_right[index] = "b8";
}
}
Linq approach
This approach I just used Linq to fetch all indexes for a matching string.
var indexes = diag_right.Where(e=>e.Equals("searchstring")).Select((c,i) => i).ToList();
foreach(int index in indexes) // loop through each line.
{
diag_right[index] = "newvalue"; // set new value.
}
Working Demo
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.
Is it possible in dhtmlx to do a reverse lookup on an option list, i.e. if I know the ItemText can I use it to lookup the Id?
i.e. something like this if such a function existed
var t = this.getAllListOptionText("tbOptionList").indexOf(Name);
I want to get the id, so that I can preset the selection to a specific option.
I could potentially loop through all the options and look for the ItemText myself, however if something already exists that would be more elegant.
for (var i = 0; i < lsTags.length; i++) {
if (this.getListOptionText("tbOptionList", lsTags[i]) == Name) {
lOptionID = lsTags[i];
break;
}
}
There are dhtmlx methods forEachItem and forEachListOption, you can iterate items and use getItemText with a check.