I'm trying to display 2 columns from database side by side in a listview on windows form. When I use this it doesn't display pic.AddedBy. It does only Display pic.FileName
How should I do to get both columns to display side by side . I have already Created those ColumnHeaders in my ListView.
Here is my code and yhank you in advance!
List<Photos> list;
list = db.Photos.Where(x => x.RegNr == rgNr).ToList();
if (list != null)
{
foreach (Photos pic in list)
{
ListViewItem item = new ListViewItem(pic.FileName, pic.AddedBy);
listView.Items.Add(item);
}
}
The listView.View should be set to Details, create two columns in designer and modify your code to:
foreach (Photos pic in list)
{
ListViewItem item = new ListViewItem(pic.FileName);
item.SubItems.Add(pic.AddedBy)
listView.Items.Add(item);
}
Related
I have a listbox on a form set to allow multiple selections. I want to loop through each selected item, store the selected value in a variable and do some work. I've tried many different variations of code to do this, but so far nothing has worked. Any help would be greatly appreciated! My code is below:
foreach (var item in systemList.Items)
{
string systemName = systemList.SelectedItems.ToString();
//do some work//
}
You can get all SelectedItems using below code:
var items = systemList.Items.Cast<ListItem>().Where(item => item.Selected);
You can then loop through the items
foreach (var item in items)
{
//Access value of each item by calling item.Value
}
foreach (var item in systemList.SelectedItems)
{
string systemName = item.ToString();
//do some work//
}
make sure listbox Selection mode is set to other than single!
I've a Listbox but when I say :
txtSelectedTables.Text += lbxTafels.GetItemText(lbxTafels.SelectedValue);
It only shows one thing in my textbox (even though I've selected multiple rows). So if I want to select multiple rows it doesn't put the values in txtSelectedTables.Text (it only shows one item).
So how can I select multiple rows and show it in a textbox.
Like RadioSpace mentioned you probably want to look at the selectedItems property
Here is an example
foreach (var item in lbxTafels.SelectedItems)
{
txtSelectedTables.Text += item.ToString();
}
Here is my full test function. If you are seeing a DataViewRow type name in the text box then maybe you have more going on than a ListBox and TextBox.
ListBox lbxTafels = new ListBox();
System.Windows.Forms.TextBox txtSelectedTables = new TextBox();
//Add 2 items
lbxTafels.Items.Add("Hello");
lbxTafels.Items.Add("World");
//Select both items
lbxTafels.SetSelected(0,true);
lbxTafels.SetSelected(1, true);
//Set the textbox
foreach (var item in lbxTafels.SelectedItems)
{
txtSelectedTables.Text += item.ToString();
}
Console.WriteLine(txtSelectedTables.Text);
The above function prints out
HelloWorld
Background:
I have lists which store parsed data from sqlite columns. I want to show these lists in a listview to the user. They click a button and the list view should show these 3 lists and their contents.
The three lists are called, SeqIrregularities, MaxLen, and PercentPopList.
Problem: The listview is being populated in a horizontal manner (Shown below) which only contains contents of Seqirregularities list.The other 2 don't even show up. I want the listview to have contents of each list, vertically in the corresponding list column.
Side note: The listview is skipping the first column, another problem, but I can fix that.
Here's the code:
ListViewItem lvi = new ListViewItem();
foreach (object o in SeqIrregularities )
{
lvi.SubItems.Add(o.ToString());
}
foreach (object a in MaxLen )
{
lvi.SubItems.Add(a.ToString());
}
foreach (object b in PercentPopList)
{
lvi.SubItems.Add(b.ToString());
}
listView1.Items.Add(lvi);
If you are adding items to ListViewItem object and assigning it to ListView it will create a single row because ListViewItem represents one row in ListView. from your code you are adding ListViewItem only one time, hence you will get only one Row that is Horizontally.
if you need to get the Multiple Rows (Vertically) you need to add the multiple ListViewItem objects to ListView .
ListViewItem lvi = new ListViewItem();
foreach (object o in SeqIrregularities )
{
lvi.SubItems.Add(o.ToString());
}
listView1.Items.Add(lvi);//Adds a new row
lvi = new ListViewItem();
foreach (object a in MaxLen )
{
lvi.SubItems.Add(a.ToString());
}
listView1.Items.Add(lvi);//Adds a new row
lvi = new ListViewItem();
foreach (object b in PercentPopList)
{
lvi.SubItems.Add(b.ToString());
}
listView1.Items.Add(lvi);//Adds a new row
I want to know if there is a possibility to filter a listbox. I mean it in such a way that is i add an item and the name is already in the listbox that you get a messagebox.show that tells you"Item already in the listbox". And that it won't be added twice.
You don't need to iterate throug the items as the Items collection of the ListBox implements the "Contains" method.
if (listBox1.Items.Contains(Item))
{
MessageBox.Show("ListBox already contains Item");
}
"Item" is in this case the Item from the other ListBox
Update. You could write:
if (listBox1.Items.Contains(listBox2.SelectedItem))
{
MessageBox.Show("ListBox already contains Item");
}
else
{
listBox1.Items.Add(listBox2.SelectedItem);
}
Use data binding might be one of the solutions:
List<string> SomeData=...
var filtered=SomeData.Where(...); // <-- Your filtering condition here
listBox1.DataSource = new BindingSource(choices, null);
Inside the event/method which adds list items inside your listbox you can add something like:
// search for list item in the listbox which has the text
ListItem li = theListBox.Items.FindByText("yourListItemName");
if (li != null)
{
// if list item exists display message
MessageBox.Show("ListBox already contains item with the name");
}
else
{
theListBox.Items.Add("yourListItemName");
}
here is a sample code try and implement it in you code
ListBox.ObjectCollection ListItem1= ListBox1.Items;
if(!string.IsNullOrEmpty(SearchBox.Text))
{
foreach (string str in ListItem1)
{
if (str.Contains(SearchBox.Text))
{
msgbox;
}
}
}
I want to display tag and subitem in my listview , those items come by using while statement. here the code
int id = 0;
while ((line = sr.ReadLine()) != null)
{
id++;
string[] columns = line.Split(',');
ListViewItem item = new ListViewItem();
item.Tag = id;
item.SubItems.Add(columns[1]);
lv_Transactions.Items.Add(item);
}
subitems cab be displayed but the tag just appear blanks. Someone know to fix this please help me
To make the item have text, which I assume you want to show the 'id', you will want this:
item.Text = id.ToString();
The tag field is ignored by the control, and exists as a way of 'tagging' the source data to a control, so it can be retrieved later on (for example, when processing an event that was trigged by the control).
The Tag property is not displayable. You will need to add the contents of the tag as a subitem or otherwise embed it in the data you are showing to the user.
You have three choices on how to implement this:
1) ListViewItem item = new ListViewItem(id.ToString());
2) item.Text = id.ToString(); (this is effectively the same as 1)
3) item.SubItems(id.ToString()); if you want the id to appear in the list of subitems.
Update
SubItems will only work correctly if you have defined Columns in the ListView and have set the ListView's View to View.Details.
Assuming that you have not done this, the following line:
item.SubItems.Add(columns[1]);
should be modified to be:
item.Text = columns[1];