Psycho Li
Psycho Li

Reputation: 85

WPF DataGrid how to increase quantity column instead of adding duplicate row

I have this assignment of "Restaurant Bill" requires DataGrid.

If a customer adds same item, instead of adding a duplicate row, I need to update the Quantity column by 1.

DataGrid populated by combobox

In the above picture, when I click Seafood Alfredo again, the quantity change to 2, but it adds another row of Seafood Alfredo. How do I prevent that happen?

Code for adding item to datagrid

private void cbbxMainCourse_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //Populate DataGrid with item selected from combobox
        MainCourse selectedMainCourse = (MainCourse)cbbxMainCourse.SelectedItem;

        //DataGrid dgDishes add that item
        dgDishes.Items.Add(selectedMainCourse);
        selectedMainCourse.Quantity = 0;

        //Iterate through dgDishes and increase Quantity by 1 for duplicate item
        foreach (var item in dgDishes.Items)
        {
            if (item == selectedMainCourse)
            {
                selectedMainCourse.Quantity ++;
            }
        }
    }

Edit 1:

For DataGrid

<DataGrid ItemsSource="{Binding}" Width="400" Height="200" Margin="58.5, 100, 58.5, 0" x:Name="dgDishes" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Category" Binding="{Binding Category}"/>
            <DataGridTextColumn Header="Price" Binding="{Binding Price}"/>
            <DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}"/>
        </DataGrid.Columns>
    </DataGrid>

Main Course

public class MainCourse : INotifyPropertyChanged
//: INotifyPropertyChanged
{
    public string Name { get; set; }
    public string Category { get; set; }
    public double Price { get; set; }
    //public int Quantity { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }
    private int _quantity;
    public int Quantity
    {
        get { return _quantity; }
        set
        {
            _quantity = value;
            //NumOrdered = 1;
            OnPropertyChanged(new PropertyChangedEventArgs("Quantity"));
        }
    }
}

Combobox

<ComboBox x:Name="cbbxMainCourse" Margin="40,40,40,240" Width="400" Height="30" SelectionChanged="cbbxMainCourse_SelectionChanged">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Name}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

Upvotes: 0

Views: 622

Answers (2)

rawberry
rawberry

Reputation: 312

if(!dgDishes.Items.Contains(selectedMaincourse))
    dgDishes.Items.Add(selectedMainCourse);
else
    selectedMainCourse.Quantity++;

Don't add to the collection if it already exists. If the item already exists in dgDishes, simply iterate the Quantity property of selectedMainCourse

It would also be helpful to know what data structure you're using for dgDishes

Upvotes: 1

Michael Sanderson
Michael Sanderson

Reputation: 90

You are adding the selectedMainCourse to the data grid without checking if that element already exists. You might try something like this:

if (dgDishes.Items.All(dish => dish.Name != selectedMainCourse.Name)) {
    selectedMainCourse.Quantity = 1;
    dgDishes.Items.Add(selectedMainCourse);
}
else {
    dgDishes.Items.First(dish => dish.Name == selectedMainCourse.Name).Quantity++;

That way you're either adding a new dish with quantity of 1, or incrementing the quantity of the dish that is already present.

Of course, my answer is posted using LINQ, but you can create similar behavior by deconstructing the All and First calls into for loops with comparisons.

Upvotes: 1

Related Questions