Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Find Total of DataGrid Column - WPF C#

$
0
0

Hi, 

I have a WPF DataGrid (dgvTransactionData) and I have a column that has the total item price for each item. These will always be numeric values.

What I want to do is calculate the total of that column.

Here's what I have so far:

private string CalculateTotalPrice()
        {
            var finalTotalPrice = 0;

            foreach (DataRowView rv in dgvTransactionData.Items)
            {
                var rowCount = Convert.ToInt32(((DataTable) dgvTransactionData.ItemsSource).Rows.Count.ToString(CultureInfo.InvariantCulture));
                var terms = new int[rowCount];
                for (var rownum = 0; rownum <= rowCount; rownum++)
                {
                    terms[rownum] = Convert.ToInt32(rv.Row[rownum].ToString());
                }
                var numbers = new string[rowCount];

                finalTotalPrice += numbers.Sum(num => Convert.ToInt32(num));
            }
            return finalTotalPrice.ToString("C");
        }


I'm pretty sure it doesn't work on one column because I haven't specified what column to loop through.

Anyway it throws an exception on

                var rowCount = Convert.ToInt32(((DataTable) dgvTransactionData.ItemsSource).Rows.Count.ToString(CultureInfo.InvariantCulture));


An unhandled exception of type 'System.InvalidCastException' occurred in CheckoutPOS.exe

Additional information: Unable to cast object of type 'System.Data.DataView' to type 'System.Data.DataTable'.

Any thoughts,

Thanks for the help in advance


From, Nathaniel Peiffer


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>