I have a problem with removing items from my listview. This program calculates the total value of the remaining values in the list. The problem is that when I remove an item it removes the value of the first item added in the listview.
For example:
/*I added this items in order.
item1 = 20,
item2 = 10,
item3 = 5
When I remove item2 its rtbTcost is 15 based on the program below.
Which means that the value of item1 was removed.*/
int totalRemoved = 0;
for (int i = 0; i < lvCart.SelectedItems.Count; i++)
{
totalRemoved += int.Parse(lvCart.Items[i].SubItems[1].Text);
lvCart.Items.Remove(lvCart.SelectedItems[i]);
}
_listTotal -= totalRemoved;
rtbTcost.Text = _listTotal.ToString();
you must use removeat
http://msdn.microsoft.com/en-us/library/aa983548(v=vs.71).aspx
1Start at the end and count down.
for (int i = lvCart.SelectedItems.Count - 1; i >= 0; i--)
{
ListViewItem itm = lvCart.SelectedItems[i];
lvCart.Items[i].Remove();
}
Total Removed is
lvCart.SelectedItems.Count
if you are using the selected items from the list view,as the items to delete.
Sum after the delete, by summing the values in the column.
int _listTotal = 0;
foreach (ListViewItem li in lvCart) {
_listTotal += int.Parse(li.Subitems[1].Text);
}
rtbTcost.Text = _listTotal.ToString();
Related
I need to display a Sheet so that the lines are aligned bottom and left. If there are no 10 characters in the word, the number of "+" characters is added so that the total of the line is 10 characters (see my output).
Why, when I print a List, I lose one row?
What's wrong with my piece of code? This matrix should be 10X10.
My output
List<string> filtredList = new List<string>() { "Jacuzzi", "Action", "Chinchilla", "Squeezebox", "Academic", "Abstract" };
int row = 10;
filtredList = Sorting(filtredList); //method is sorting by descending.
foreach (var item in filtredList) Console.WriteLine("Item: " + item + " length: " + item.Length);
Console.WriteLine("-------------------------------------");
//AFTER SORTING IN LIST:
//1)Squeezebox 2)Chinchilla 3)Academic 4)Abstract 5)Jacuzzi 6)Action
for (int i = 10; i > 0; i--)
{
try
{
if (filtredList[i].Length != 10)
{
Console.Write(filtredList[i]);
row = 10 - filtredList[i].Length;
Console.WriteLine(string.Concat(Enumerable.Repeat("+", row)));
}
else Console.WriteLine(filtredList[i]);
}
catch (SystemException)
{
row = 10;
Console.WriteLine(string.Concat(Enumerable.Repeat("+", row)));
}
}
Because your for loop is never gets to 0:
for (int i = 10; i > 0; i--)
i goes from 10 to 1 and that's why the first item of your list never prints.
Note:
Index of first item of an array or a list is 0
The better code would be:
for (int i = 9; i >= 0; i--)
Although you can use better alternatives for some of your code, but this will solve your problem.
EDIT:
You can use this approach (just change the for part to this) to not raise any exceptions (because its not normally a use case for try-catch)and also get faster results:
for (int i = 9; i >= 0; i--)
{
if (i < filtredList.Count)
{
if (filtredList[i].Length != 10)
{
Console.Write(filtredList[i]);
row = 10 - filtredList[i].Length;
Console.WriteLine(new string('+', row));
}
else Console.WriteLine(filtredList[i]);
}
else
{
row = 10;
Console.WriteLine(new string('+', row));
}
}
I am trying to create a string with items from a ListView in C#(Windows Forms). I have two columns and over hundreds of three digit numbers in my ListView. The values indicate which X and Y axis my mouse was on. But as soon as I try to output the values in e.g. a text box, only the last X and Y values appear, the rest are ignored.
What I have tried:
listView1.Items[a].SubItems[0].Text
int.Parse(listView1.Items[a].SubItems[0].Text)
maybe someone has a suggestion
You need to iterate over the items and subitems in this manner:
foreach (ListViewItem item in listView1.Items)
{
Debug.WriteLine($"Item: {item.Text}");
foreach (ListViewItem.ListViewSubItem subitem in item.SubItems)
{
Debug.WriteLine($"\tSubitem:{subitem.Text}");
}
}
Do you use a loop for your items? It's not clear where a comes from. You might also want to use a loop for your subitems.
you can loop through all items and subitems like this:
for (int i = 0; i < listView1.Items.Count; i++)
{
for (int k = 0; k < listView1.Items[i].SubItems.Count; k++)
{
string s = listView1.Items[i].SubItems[k].Text;
}
}
thanks for your help guys,
the only thing missing was the loop and this line Value += Environment.NewLine +
your suggestions both worked
foreach (ListViewItem item in listView1.Items)
{
Value += Environment.NewLine + item.Text;
foreach (ListViewItem.ListViewSubItem subitem in item.SubItems)
{
Value += Environment.NewLine + subitem.Text;
}
}
and
for (int i = 0; i < listView1.Items.Count; i++)
{
for (int k = 0; k < listView1.Items[i].SubItems.Count; k++)
{
Value += Environment.NewLine + listView1.Items[i].SubItems[k].Text;
}
}
When i use for (int i = 1; ..) skip the loop the first item.
How can i start with index 1 and dont skip any item?
private void buttonReadAndSort_Click(object sender, EventArgs e)
{
ReadFromFile rd = new ReadFromFile();
var fileList = rd.readFromFile();
for (int i = 0; i < fileList.Count; i++)
{
var item = (fileList[i]);
Console.WriteLine(item);
list.Add(item);
listBox1.Items.Add(item);
}
buttonReadAndSort.Enabled = false;
}
I guess you want to start with index 1 but access the item at index 0:
for (int i = 1; i <= fileList.Count; i++)
{
var item = fileList[i-1];
Console.WriteLine(item);
list.Add(item);
listBox1.Items.Add(item);
}
But you could also loop normally and add +1 where you need it 1 based:
for (int i = 0; i < fileList.Count; i++)
{
var item = fileList[i];
Console.WriteLine("item:{0} #{1}", item, i + 1);
list.Add(item);
listBox1.Items.Add(item);
}
List, like array, start at zero. Not sure why you would want it to start it at 1 and not skip any items. I do something like csvLinesPartNumber.Add("stuff") to add to the list and then I can get "stuff" from the particular index like so: csvLinesPartNumber[4]. For example, I am doing a while sqlreader dot read which will loop through the table with the particular ID. Then you can do another loop to get the data. Just make sure you put the index in the square brackets.
I am using delete row index to delete a row in datagridview.
But when one row is deleted then datagridview automatically appends next row
first time any index is deleted then itscorrect but at the second time row-1 indexed row is deleted
I am using this code:
for (int i = 0; i < path.Length; i++)
{
lpath.Add(path[i].ToString());
dataGridView1.Rows.RemoveAt(int.Parse(arr[i]));
}
you have to reduce the 'i' after you delete a row when using for loop
See the following code
for (int i = 0; i < path.Length; i++)
{
lpath.Add(path[i].ToString());
dataGridView1.Rows.RemoveAt(int.Parse(arr[i]));
i--; }
As i see it you skew the index off the dataGridView1 the first time.
lets say you have a list with element {1,2,3,4,5} if we now remove index 3 and then 4 we the end up removing 3 and 5 giving us the list {1,2,4} but we wanted to remove 3 and 4 not 3 and 5
what i think you want to do is to get all indexes you want to remove, and the order them and remove from top to bottom so instead of removing 3 an 4 in that order you remove 4 and 3 giving us the list {1,2,5}
List<int> RemoveList = new List<int>();
for (int i = 0; i < path.Length; i++)
{
lpath.Add(path[i].ToString());
RemoveList.Add(int.Parse(arr[i]));
}
//Remove highest index first.
RemoveList.OrderByDescending(a=>a);
for (int i = 0; i < RemoveList.Count; i++)
{
dataGridView1.Rows.RemoveAt(RemoveList[i]);
}
it's a bit weird to increase i within the for loop than decrease it. Why don't you add the items than clear all rows ?
for (int i = 0; i < path.Length; i++) {
lpath.Add(path[i].ToString());
}
dataGridView1.Rows.Clear();
I need to loop through a checked listbox, and for each of the items in it, I need to check them (basically like a "select all" function).
Is there a basic example you could give me to help me out please?
Use SetSelected and interate through all the Items
// Loop through and set all to selected.
for (int x = 0; x < listBox1.Items.Count; x++)
{
listBox1.SetSelected(x, true);
}
To check the items, use SetItemChecked
// Loop through and set all to checked.
for (int x = 0; x < listBox1.Items.Count; x++)
{
listBox1.SetItemChecked(x, true);
}
You can look through all the items as ListItems:
foreach (ListItem li in CheckBoxList1.Items)
{
li.Selected = true;
}