I'm having a problem since a while now an just can't find any solution that works for me. I have a ListBox which is filled up with a DataTable like
listbox.DataSource = table;
listbox.Displaymember = "Name";
listbox.ValueMember = "ID";
If I now select an item in my listbox I can get it out like:
listbox.SelectedValue.toString();
My Problem:
What can I do if I would like to have ALL selected Values from a ListBox where multiple selection is enabled and save them all in an array or something like that?!
I can't use SelectedItems cause that is not giving me the information I need.
Try this:
var lst = listBox1.SelectedItems.Cast<DataRowView>();
foreach (var item in lst)
{
MessageBox.Show(item.Row[0].ToString());// Or Row[1]...
}
Or if you want only iterate over the selected items you can use SelectedIndices property:
foreach (int i in listbox.SelectedIndices)
{
// listbox.Items[i].ToString() ...
}
Or:
foreach (var item in listbox.SelectedItems)
{
MessageBox.Show(item.ToString());
}
Related
I'm populating a ListBox in a WinForms application, this way:
listBoxUsers.DataSource = ctx.Users.ToList();
listBoxUsers.DisplayMember = "Name";
listBoxUsers.ValueMember = "Id";
how to retrieve the selected Ids when I'm setting the SelectionMode to MultiSimple
I want to do a foreach loop on them, like this:
foreach(var itemId in listBoxUsers.SelectedValues)//unfortunately not exist
{
int id = int.Parse(itemId);
// . . .
}
Since you know the type of items, you can use such code:
var selectedValues = listBox1.SelectedItems.Cast<User>().Select(x=>x.Id).ToList();
Side Note: The ListBox control lacks a GetItemValue method. A method which should work like GetItemText, but for getting values. In the linked post I shared an extension method to get the value from an item. Using that extension method you can get selected values independent from type of items:
var selectedValues = listBox1.SelectedItems.Cast<object>()
.Select(x => listBox1.GetItemValue(x)).ToList();
If for some reason you are interested to have a text representation for selected values:
var txt = string.Join(",", selectedValues);
Have you tried with the SelectedItems property?
foreach (var item in listBoxUsers.SelectedItems)
{
}
try this:
foreach (DataRowView item in listBoxUsers.SelectedItems)
{
int id=int.parse(item[0].ToString());
}
In C#, how can I determine if an item in a CheckedListBox is checked or not if I have the text of the CheckListBoxItem?
I am needing to loop through all CheckedListBoxItems, and retrieve the text and the checked state.
Here is what I have so far:
CheckedListBox.ObjectCollection items = checkedListBoxFileNames.Items;
foreach (var item in items)
{
}
I am not sure on how to determine if an item is checked or not.
Thanks in advance.
You don't need this foreach loop.
Try this:
if(this.m_CheckedListbox.CheckedItems.Contains("Item1")
{
//make an action, if it's checked.
}
if(this.m_CheckedListbox.CheckedItems.Contains("Item2")
{
//make an action, if it's checked.
}
// etc...
// this.m_CheckedListbox should be the name of your checked list box.
You can use like
IEnumerable<int> allChecked = (from item in chkBoxList.Items.Cast<ListItem>()
where item.Selected
select int.Parse(item.Value));
more Details click here
Try like this:
foreach(object itemChecked in checkedListBox1.CheckedItems)
{
//your code
}
Also check CheckedListBox.CheckedItems Property
Collection of checked items in this CheckedListBox.
Since I haven't found anything that helped, I ask my question here:
I have a ListView where I select a whole row by click. Now I want to store these selected items into a List but don't know how this should work exactly.
List<String> itemSelected = new List<String>();
foreach (var selectedRow in listView1.SelectedItems)
{
itemSelected.Add(selectedRow);
}
That doesn't work because I need an index (selectedRow[?]) or something like that. How can I store the values of the first column when clicked the row?
EDIT: The problem is that the ListViewItems have the type "object"
The ListView gets populated this way:
using (SqlConnection connection = new SqlConnection(connectionQuery))
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
col1 = row.Cells[col1.Text].Value.ToString();
col2 = row.Cells[col2.Text].Value.ToString();
col1Cells.Add(col1);
col2Cells.Add(col2);
}
}
You can do something like:
ListViewItem listViewItem = this.listView1.SelectedItems.Cast<ListViewItem>().FirstOrDefault();
if (listViewItem != null)
{
string firstColumn = listViewItem.Text;
string secondColumn = listViewItem.SubItems[0].Text;
// and so on with the SubItems
}
If you have more selected items and only need the values of the first columns you can use:
List<string> values = listView1.SelectedItems.Cast<ListViewItem>().Select(listViewItem => listViewItem.Text).ToList();
It's common to bind a ListView to the List of non-trivial types.
Then you can handle SelectedItemChanged or something like that. You receive the whole object (in type object) which you can cast to your custom type and retrieve any properties you want
How to clear all Groups And all Items in that Groups in Listview Control
Probably ListView.Clear() will work for you. And to clear groups in ListView call ListViewGroupCollection.Clear()
if you are filling your group items with datasource then you could try something like this..
How about
DataSource = null;
DataBind();
If you want to remove only the listViewItems which are grouped, you can do following:
foreach (var group in listView.Groups)
{
var listViewItemsToDelete = listView.Items.Cast<ListViewItem>().Where(item => Equals(item.Group, group));
foreach (var itemToRemove in listViewItemsToDelete)
{
listView.Items.Remove(itemToRemove);
}
}
listView.Groups.Clear();
I've just started to use ListView in C#.net.
I got to know how to add items and subitems. Going through the listview I wanted to fetch all the data from a whole column with multiple rows.
I want to know how to do this.
I found this code to list a specific selected data from a row:
ListView.SelectedIndexCollection sel = listView1.SelectedIndices;
if (sel.Count == 1)
{
ListViewItem selItem = listView1.Items[sel[0]];
MessageBox.Show(selItem.SubItems[2].Text);
}
That was helpful but i want to list all the items in a row, may be i want to add all the column items in array?
private string[] GetListViewItemColumns(ListViewItem item) {
var columns = new string[item.SubItems.Count];
for (int column = 0; column < columns.Length; column++) {
columns[column] = item.SubItems[column].Text;
}
return columns;
}
I would recommend some caution against doing this. A ListView is really meant to display information, it is not a great collection class. Getting the data out of it is slow and crummy, it can only store strings. Keep the data in your program in its original form, maybe a List<Foo>. Now it is simple and fast.
foreach (ListViewItem item in listView1.Items) {
// Do something with item
}
you could do this by
foreach(ListViewItem item in listView1.Items)
{
foreach(var subtem in item.SubItems)
{
// Do what ever you want to do with the items.
}
}