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

WPF DataGrid C# - Check if certain column has a value even if it has rows - WPF DataGrid C#

$
0
0

Hi,

I have a DataGrid on a WPF window.

The window code contains a method that calculates the total value of a column.

The only problem is if the DataGrid has rows but one or more of the cells in that column have no value, the application throws an error.

Here is the method that I am using:

private string CalculateTotalPrice()
        {
            decimal finalTotalPrice = 0;
            try
            {
                finalTotalPrice =
                        dgvTransactionData.ItemsSource.Cast<DataRowView>()
                            .Sum(rowView => (decimal)rowView["Total Item Price"]);
            }
            catch (Exception exception)
            {
                Dal.ErrorMessage(exception);
            }
            return finalTotalPrice.ToString("C");
        }


The error message is:

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

Additional information: Specified cast is not valid.

and it occurs on the LINQ statement in the try block.

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>