I am still very new to c# wpf
This seems strange that I can not do this. Could someone explain this mistake to me please everything I read says this SHOULD work!
I have a class (called OSMI) which I show through a DataGrid in WPF, based on the Class property OsmiOverride
= "Y" based on the Selected Items of the Grid I want to total up the Column of the grid based on the class property InvValue
. I take the Dat\grid.SelectedItems
and put them in a List and send to my method and I loop through testing, but no matter how I do the String.Compare
or String.Equals
I can not get inside my If statement. Below is my current version of my method and class (excuse the crude MessageBox
debug method)
public static Nullable<decimal> GetOsmiSubtractions(List<OSMI> dgselecteditems)
///Get efffect of selected items subtracking to OSMI
{
Nullable<decimal> sum = 0;
String Test = @"Y";
foreach (OSMI p in dgselecteditems)
{
MessageBox.Show(p.InvValue.ToString());
MessageBox.Show(p.OsmiOverride);
if (p.OsmiOverride.Equals(Test))
{
sum += p.InvValue;
MessageBox.Show("Inside");
}
}
MessageBox.Show(sum.ToString());
return decimal.Round((decimal)sum, 2);
}
So far I have tried
if (p.OsmiOverride=="Y")
if (p.OsmiOverride==@"Y")
Then I read use the String.Equals
method
String Test = @"Y";
if (p.OsmiOverride.Equals(Test))
But nothing is working, can someone please explain
Many Thanks
Ian
For completeness my class below
(I also think I should do something with a LINQ query to reduce the need for the loop because I could use the like [here][1]
but I am not sure of the filter / where statement)
Something like
Nullable<decimal> total = dgselecteditems.Sum(item => item.InvValue where item.OsmiOverride=="Y" );
For completeness my class is
using System;
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;
public partial class OSMI:INotifyPropertyChanged
{
private string osmioverride;
private Nullable<decimal> osmi1;
public Nullable<long> Id { get; set; }
public string StockCode { get; set; }
public string Description { get; set; }
public string Warehouse { get; set; }
public Nullable<decimal> UnitCost { get; set; }
public Nullable<decimal> QtyOnHand { get; set; }
public Nullable<decimal> InvValue { get; set; }
public string OsmiOverride
{
get { return this.osmioverride; }
set
{
if (this.osmioverride != value)
{
this.osmioverride = value;
this.NotifyPropertyChanged("OsmiOverride");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propOsmiOverride)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propOsmiOverride));
}
public string NRVOverride { get; set; }
public Nullable<decimal> OSMI1
{
get { return this.osmi1; }
set
{
if (this.osmi1 != value)
{
this.osmi1 = (decimal)value;
this.NotifyPropertyChanged("Osmi1");
}
}
}
}
}