Listview is not populating all columns in a vertical manner - c#

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

Related

Adding values calculated to another function to second row of listview

I have a listview with two columns. The first column is filled with items imported from an external file.
The second column is supposed to be filled with values that are calculated in another function of the software.
How is this possible?
EDIT
string[] arr = new string[4];
ListViewItem itm;
private void button3_Click(object sender, EventArgs e)
{
listView1.Columns.Add("ProductName", 100);
listView1.Columns.Add("Price", 70);
//Add items in the listview
//Add first item
arr[0] = "product_1";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
//2
arr[0] = "product_2";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
listView1.CheckBoxes = true;
The result is something like this :
Now I have another function, a simple button. Let's say that I want to press the button and fill the second column of the listview with some values. In this case it is the price of each product.
I wrote the following :
private void button4_Click(object sender, EventArgs e)
{
//1
arr[1] = "20000";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
//Add second item
arr[1] = "200";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
The result is like this :
The first column is changing as well. I only want to put the prices calculated at one function next to a list of products.
Looks like in your second button you are adding a new item when you actually want to update an existing item. Well add a child to an existing item.
Try this in your second button click.
listView1.Items[index].SubItems.Add("20,000");
Where index is the 0-based index of the item you want to modify. You may wish to use a foreach loop to update all the items. Which would look something like this.
foreach (ListViewItem item in listView1.Items)
{
//Do your calculation or whatever it is you want to per item here...
item.SubItems.Add("20,000");
}
Hopefully this helps. :)

Display multiple columns from Database in ListView Winform

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);
}

C# Select multiple values listbox

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

ListView selected items add other ListView

How to add ListView selected items in other ListView?
Example project:
ExpTreeLib
Put this form ListView2 and add lv.selectedItems.
Thanks in advance.
You can use this method to add the items
ListView list = new ListView();
ListViewItem item = new ListViewItem();
// add them
list.Items.Add(item);
This would be enough, now you can use a simple loop for each element to add it to that list.
foreach (ListViewItem item in ListView.SelectedItems) {
// create a new list, or use currently present list
// add each item to it using .Add() method.
}

Remove numbering in ListView

When I load images into ListView, the numbers appear below the images. How can I remove them?
The following is my code to load images into ListView:
DataTable dtPath = new DataTable();
dtPath = ContrPtMRD.SelectFilePaths(ObjPtMRH);
ImageList myImageList = new ImageList();
lvPtMedicalRecord.LargeImageList = myImageList;
int imageIndex = 0;
foreach (DataRow rows in dtPath.Rows)
{
myImageList.Images.Add(Image.FromFile(rows[2].ToString()));
ListViewItem lvi = new ListViewItem(new string[]{rows[0].ToString(),rows[1].ToString()});
lvi.ImageIndex = imageIndex;
imageIndex++;
lvPtMedicalRecord.Items.Add(lvi);
}
The ListView will not add any image numbers unless you explicitly add them. You are doing so in the following line:
ListViewItem lvi = new ListViewItem(new string[]{rows[0].ToString(),rows[1].ToString()});
and the number seem to be coming from
rows[0].ToString()
Which is the text property of the ListViewItem.
To remove the numbers, replace rows[0].ToString() with an empty string or use one of the ListViewItem's constructors that does not require you to provide the text property:
ListViewItem lvi = new ListViewItem();
Its not clear where are the numbers coming from but I think the numbers are in the first column of your DataTable. If you want to remove the numbers then dont add the first column values to your ListView as Items.
or else
Hide the column in the ListView which you dont want to show.
or else
Change the view of the ListView to large details.(this will not show the item value, only subItems will be visible)

Categories