I'm trying to Count up and delete the duplicates rows when a new row is added, and if the row does not exist it should make a new row for it. If a new row is added and is the same as one of the existing ones is would add it to the count.
It should look for if: 'Code' is the same and if the 'Part' is the same.
What I'm getting:
Updated code now: CodeCombox has been replaced with textBoxScanner.
private void addcodes()
{
if (!int.TryParse(CountTextBox.Text, out var count)) return;
var product = _bindingList
.FirstOrDefault(prod =>
prod.Code == Convert.ToString(textBoxScanner.Text) &&
prod.Parts == PartsComboBox.Text);
if (product == null)
{
_bindingList.Add(new Product()
{
Code = Convert.ToString(textBoxScanner.Text),
Parts = PartsComboBox.Text,
Count = count
});
}
else
{
product.Count = product.Count + count;
}
textBoxScanner.Focus();
textBoxScanner.Clear();
}
Class has been updated to a string:
public class Product : INotifyPropertyChanged
{
private string _code;
private string _parts;
private int _count;
public string Code
{
get => _code;
set
{
_code = value;
OnPropertyChanged();
}
}
public string Parts
{
get => _parts;
set
{
_parts = value;
OnPropertyChanged();
}
}
public int Count
{
get => _count;
set
{
_count = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}