c# .net listview with grouping - c#

New to c# and .net and I'm trying to throw together a listview that has three columns, Quantity, Item description and Price. I know how to fill the rows with data and I can even add the subitem below but what I can't figure out is how I can group them together as a primary item with "children" items tied to it...
Sorry for my most likely incorrect verbiage, but this is what I am shooting for:
Qty Description Price
1 Widget 2.95
Widget add on .25
Widget insurance 1.25
1 Sprocket 4.95
Sprocket add on .50
I am trying to figure out how to group the subitems below the primary item to make it where when I select, for instance, "Widget insurance", it highlights "Widget add on" and "Wiget" as one entity. So for example if I need to go back and remove "Widget insurance" from the purchase of the "Widget" I can click on any item connected to "Widget", ex. "Widget", "Widget add on" or "Widget insurance" and it will pull up another form that allows me to deselect "Widget insurance", hit OK and it would update the list accordingly...
I've (poorly) thrown together code that visually gives me what I am looking for, but I think I am completely confusing the use of subitem and it's purpose:
string[] newItem = new string[3];
ListViewItem itm;
newItem[0] = "1";
newItem[1] = "Widget";
newItem[2] = "2.95";
itm = new ListViewItem(newItem);
listView1.Items.Add(itm);
string[] newSubItem = new string[3];
ListViewItem sub;
newSubItem[0] = "";
newSubItem[1] = "Widget add on";
newSubItem[2] = ".25";
sub = new ListViewItem(newSubItem);
listView1.Items.Add(sub);
Any help would be greatly appreciated.

Here you are. "Qty" will be ListViewItem's text. "Description" and "Price" will be ListViewItem's SubItems.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.Columns.Add("Qty", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Description", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Prie", 100, HorizontalAlignment.Left);
var details = new[] {"Widget", "2.95"};
var item = new ListViewItem("1");
item.SubItems.AddRange(details);
listView1.Items.Add(item);
details = new[]{ "Widget add on",".25"};
item = new ListViewItem("");
item.SubItems.AddRange(details);
listView1.Items.Add(item);
}
}
Hope this help.

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. :)

How add many buttons to TabbedPanel?

I am developing a solution in C # and I need to add a few buttons to TabbedPanel. Happens that debugger adds only one and even a single button.
public void AddDrinkstoTabbedpanel(TabPage tp)
{
List<string> drinks = new List<string>();//New list empty
food foo = new food();//Call food to get drinks
string []product = foo.name;//string product [] = food.name (drink)
foreach (string value in product)//(string value in product)//for any "value" in product
{
Button bt = new Button();
bt.Size = new Size(100, 100);
drinks.Add(value);
tp.Controls.Add(bt);
//listofButtons.Add(bt);
}
}
I previously define an array with some items and now i need to add a button for each item founded, how can I modify this code given above to get what I want.
Change the Location for all buttons. They are being created but at the same position and look like one.

Display group header when there are no items in the group in a listview?

I would like to group my items in a list view (winform) with the group header showing the number of items in the group (including 0 item).
My problem is that, when I clear the items from the group or from the list view, the group header disappears.
How can I make sure the group headers don't disappear? I can't see any property to set in the ListView or ListViewGroup classes.
Thank you
Consider the code:
ListViewGroup groupA;
ListViewGroup groupB;
private void button1_Click(object sender, EventArgs e)
{
//Create group A and B and add them to the groups collection
groupA = new ListViewGroup("A");
listView1.Groups.Add(groupA);
groupB = new ListViewGroup("B");
listView1.Groups.Add(groupB);
//Add 2 items in the list view and update the group headers
listView1.Items.Add(new ListViewItem("Item1", groupA));
groupA.Header = "A - 1 item";
listView1.Items.Add(new ListViewItem("Item2", groupB));
groupB.Header = "B - 1 item";
}
private void button2_Click(object sender, EventArgs e)
{
//Clear the items
listView1.Items.Clear();
//Update header
//The problem here is that the headers are not displayed any more
groupA.Header = "A - 0 item";
groupB.Header = "B - 0 item";
}
I am replying to my own question! It seems not possible to do it with the normal ListView. I have also looked at the underlying Win32 messages with no success. In the end, I gave up and I bought a third party control.

Add multiple items to a listbox on the same line

Hey guys, i figured out how to add items to a listbox one line at a time:
try
{
if (nameTxtbox.Text == "")
throw new Exception();
listBox1.Items.Add(nameTxtbox.Text);
nameTxtbox.Text = "";
textBox1.Text = "";
nameTxtbox.Focus();
}
catch(Exception err)
{
MessageBox.Show(err.Message, "Enter something into the txtbox", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
But I wont to be able to add multiple items to the same line. Like have first_name |
last_name | DoB all on the same line. When I do
listBox1.Items.Add(last_name.Text);
It adds the last name to a new line on the listbox, I need to add it to the same line as the first name.
It sounds like you still want to add one "item", but you want it to contain more than one piece of text. Simply do some string concatenation (or using string.Format), eg.
listBox1.Items.Add(string.Format("{0} | {1}", first_name.Text, last_name.Text));
Usually you don't want to include multiple columns into a ListBox, because ListBox is meant to have only one column.
I think what you're looking for is a ListView, which allows to have multiple columns. In a ListView you first create the columns you need
ListView myList = new ListView();
ListView.View = View.Details; // This enables the typical column view!
// Now create the columns
myList.Columns.Add("First Name", -2, HorizontalAlignment.Left);
myList.Columns.Add("Last Name", -2, HorizontalAlignment.Left);
myList.Columns.Add("Date of Birth", -2, HorizontalAlignment.Right);
// Now create the Items
ListViewItem item = new ListViewItem(first_name.Text);
item.SubItems.Add(last_name.Text);
item.SubItems.Add(dob.Text);
myList.Items.Add(item);
Here has a solution to add multiples items at the same time.
public enum itemsEnum {item1, item2, itemX}
public void funcTest2(Object sender, EventArgs ea){
Type tp = typeof(itemsEnum);
String[] arrItemEnum = Enum.GetNames(tp);
foreach (String item in arrItemEnum){
ListBox1.Items.Add(item);
}
}
Hope this can help.

Unbale to identity WinForm ListView subitems in Coded UI test VSTS 2010

This is my window application code for listview :-
// Create three items and three sets of subitems for each item.
ListViewItem item1 = new ListViewItem("item1", 0);
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2", 1);
item2.SubItems.Add("4");
item2.SubItems.Add("5");
item2.SubItems.Add("6");
ListViewItem item3 = new ListViewItem("item3", 0);
// Place a check mark next to the item.
item3.Checked = true;
item3.SubItems.Add("7");
item3.SubItems.Add("8");
item3.SubItems.Add("9");
// Create columns for the items and subitems.
// Width of -2 indicates auto-size.
listView1.Columns.Add("Item Column", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Column 3", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Column 4", 100, HorizontalAlignment.Center);
//Add the items to the ListView.
listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });
Then i add the lisview control in Coded UI test :-
In the coded UI test i used below code
WinList wkList = this.UIForm1Window.UIListView1Window.UIListView1List;
string[] strVal = CommonExtensions.GetValuesOfControls(wkList.Items);
foreach(WinControl control in this.UIForm1Window.UIListView1Window.UIListView1List.Items)
{
int count = control.GetChildren().Count;
object objVal = CommonExtensions.GetValue(control);
WinListItem lstItem = (WinListItem)objVal;
}
in the "strVal " variable gives only values of first column not the subitems.
in add watch window i get below mentioned value:-
strVal[0] = "item1"
strVal[1] = "item2"
strVal[2] = "item3"
I have also used the http://blogs.msdn.com/b/gautamg/archive/2010/02/19/useful-set-of-utility-functions-for-coded-ui-test.aspx?CommentPosted=true#commentmessage
WinListItem listItem = new WinListItem(control);
string[] strSubItems = WinExtensions.GetColumnValues(listItem);
In the above statement i am getting Invalid Io expection stating "The control passed is not a list view item control. This operation is valid only for list view item control."
Please suggest any other alternative ?.
I usually use smth like that:
WinList list;
UITestControlCollection columns = websitesList.Columns; // Get Columns names
// Get values
foreach (WinControl item in list.Items)
{
WinListItem listItem = new WinListItem(item);
string[] subItems = WinExtensions.GetColumnValues(listItem); // So you get value = subitem[i] in column[i]
}

Categories