Hello friends, I've been working on a program that extracts the data from a database, then when I query the database the data is displayed on a listview, and then only the items that are selected are added into another listview(checkbox), now my problem is that i need to calculate the total price of the items, in the last listview the headers are items and price, so i need when i click in a button all the data displayed on the column price get sumarized.

This is my code, i have tried usinglistview.items[].subitems[]; but it seems that it no longer exists.

This is the code that add the selected items in the main listview to the checkout listview:

private void lstData_MouseDoubleClick(object sender, MouseButtonEventArgs e)//Add item to lstCart with DoubleClick
        {
            lstCart.Items.Add(lstData.SelectedItem);
        }

And this is the code that i tried to use to get the data from the listCart

ListView.SelectedItemProperty precio =
              this.lstData.SelectedItems;

           double price = 0.0;
                     foreach (lstCart.SelectedItem in precio)
                    {
                     price += Double.Parse(item.SubItems[1].Text);
               }

            //Output the price to TextBox1.
             TextBox1.Text = price.ToString();

BTW i'm using WPF with VS Studio 2010

Thanks in advanced!

Recommended Answers

All 9 Replies

Let me try and understand this right.

You add things to ListView called lstCart from a ListView called lstData?
and you add price to lstCart then why don't you just do this?

double getTotal()
        {
            double x=0;
            if (lstCart.Items.Count > 0)
            {
                foreach (ListViewItem temp in lstCart.Items)
                    x = x + double.Parse(temp.SubItems[1].Text);
            }
            return x;
        }

I haven't used .net 2010 so I am not sure if this is going to work, I don't see Microsoft changing this functionality doesn't make sense to me.

Let me try and understand this right.

You add things to ListView called lstCart from a ListView called lstData?
and you add price to lstCart then why don't you just do this?

double getTotal()
        {
            double x=0;
            if (lstCart.Items.Count > 0)
            {
                foreach (ListViewItem temp in lstCart.Items)
                    x = x + double.Parse(temp.SubItems[1].Text);
            }
            return x;
        }

I haven't used .net 2010 so I am not sure if this is going to work, I don't see Microsoft changing this functionality doesn't make sense to me.

Thank you for the answer but it still gives me the same error, sorry for the bad explanation, as you figured it out, yup i have a listview called lstData (the main listview), and the lstCart that displays something like this:

Item | Price
==========
Halo $30.00
Gow $65.00

etc. I obtain the data directly from the main lstData, is binded with XAML.

I figured it out another way that works to obtain the data from the lstCart, the code is:

double prc = 0;
int countItems = 0;
string prtPrice = "";
if (lstCart.Items.Count > 0)
            {
                foreach (DataRowView chkOutRow in lstCart.Items)
                {
                    countItems++;
                    prtPrice = chkOutRow.Row["precio"].ToString(); 
                    prc = prc + double.Parse(prtPrice);
                }
}

Now my problem is(i don't know if i need to open a new thread for this) that i would like to print all the items individually in message box and then print the total of items, subtotal, and total
i already now how to make the last part but i don't know how to print each item individually, i created a list and the message box displays allt the items one by one(in different message boxes)and not at once in a single message box, how can i fix that, this is my code:

private void btnChkout_Click(object sender, RoutedEventArgs e)//Creates checkout
        {
            double prc = 0;
            int countItems = 0;
            string div = "============================";
            string format = "Quantity\tSubtotal\tTotal ";
            string prtPrice = "";
            string prtName = "";
            List<string> list = new List<string>();//list of items, to be implemented
            if (lstCart.Items.Count > 0)
            {
                foreach (DataRowView chkOutRow in lstCart.Items)
                {
                    countItems++;
                    prtPrice = chkOutRow.Row["precio"].ToString();//individual price
                    prc = prc + double.Parse(prtPrice);//total price
                }

                foreach (DataRowView Name in lstCart.Items)//this prints only the last item name, to be implemented
                {
                    prtName = Name.Row["titulo"].ToString();//individual item name, only the last one is showed
                    list.Add(prtName);

                }
               //this message box works, prints the total and subtotal price and # of items
               // MessageBox.Show("Product(s):\n" + div + ("\n\n") + format + ("\n") + "(x " + countItems.ToString() + ")\t" + prc.ToString() + "\t"+(prc*.16 + prc),"Ticket"); 

                for (int i=0; i < list.Count;i++ )
                {
                    MessageBox.Show(list[i]);//prints items one by one, to be implemented
                }
                
            }
        }

Any help will be very appreciated,

Thanks in advanced!

I think I know what you are trying to do:

so basically you want this as result in messagebox:

Item | Price
==========
Halo $30.00
Gow $65.00


Right?

private void button1_Click(object sender, EventArgs e)
        {

            string list;//list of items, to be implemented
            if (listView1.Items.Count > 0)
            {
                list = "============================"+"\nQuantity\tSubtotal\tTotal\n";
                foreach (ListViewItem chkOutRow in listView1.Items)
                {
                    list += chkOutRow.Text + "\t"+chkOutRow.SubItems[1].Text + "\t"+(int.Parse(chkOutRow.Text) *double.Parse(chkOutRow.SubItems[1].Text))+"\n";
                }
                MessageBox.Show(list);

            }
        }

Enjoy

attached code. to understand what I did. The code looks a little different than the one post dw about it.

Thank so much for the code man, it seems that it works, but i don't know what's wrong with visual 2010 or maybe with wpf that displays the following errors:

Error 1 'System.Windows.Controls.ListViewItem' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Windows.Controls.ListViewItem' could be found (are you missing a using directive or an assembly reference?)

Error 2 'System.Windows.Controls.ListViewItem' does not contain a definition for 'SubItems' and no extension method 'SubItems' accepting a first argument of type 'System.Windows.Controls.ListViewItem' could be found (are you missing a using directive or an assembly reference?)

in this part of the code:

list += chkOutRow.Text + "\t"+chkOutRow.SubItems[1].Text + "\t"+(int.Parse(chkOutRow.Text) *double.Parse(chkOutRow.SubItems[1].Text))+"\n";

The weird thing is that when i open your app, it works fine and the code is the same, so i'm afraid that the problem is WPF, can you try to build the same app with wpf, to see if that's the problem?.

Thanks man!!

hmm try

list += chkOutRow[B].SubItems[0].Text [/B]+ "\t"+chkOutRow.SubItems[1].Text + "\t"+(int.Parse(chkOutRow.Text) *double.Parse(chkOutRow.SubItems[1].Text))+"\n";

http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-1

I am using VS 2005. Haven't touched 2010.

Try this:

list += chkOutRow.Row["precio"].ToString() + "\t"+chkOutRow.Row["titulo"].ToString() + "\t"+(int.Parse(chkOutRow.Row["precio"].ToString() ) *double.Parse(chkOutRow.Row["titulo"].ToString()))+"\n";

OMG thank you so much, this code helped me a lot I just modified a little bit to make it work but now everything is fine. Here's the modified code:

if (lstCart.Items.Count > 0)
                {
                    list = "Item(s)\t\tSubtotal\tTotal\n" + div;
                    foreach (DataRowView chkOutRow in lstCart.Items)
                {
                    countItems++;
                    list += chkOutRow.Row["titulo"].ToString() + "\t" + chkOutRow.Row["precio"].ToString() + "\t" + (double.Parse(chkOutRow.Row["precio"].ToString()) * 1.16) + "\n";//prints items individually
                    prtPrice = chkOutRow.Row["precio"].ToString();//Gets price from row Price
                    prcSub = prcSub + double.Parse(prtPrice);//Subtotal price
                    prcTot = prcTot + double.Parse(prtPrice)*1.16;//Total price(TAX)
                    total = "(x" + countItems.ToString()+")\t\t" + prcSub + "\t" + prcTot;//Prints the Total String
                }
                    MessageBox.Show(list + "\n"+ "Total\n" + div + total,"Ticket");//PrintsTicket
                }

Thank you again man!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.