Amad
Amad

Reputation: 314

How to dynamically add items in ListView in WPF

I am facing a problem when I add data in ListView. The problem is that ListView add data first time correctly but the second-time data is added it replaces the previous row with new data. Here is my list view XAML and XAMl.cs

<ListView HorizontalAlignment="Left" Name="invoiceLV" Height="207" Margin="154,88,-752,0" VerticalAlignment="Top" Width="914">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Code" DisplayMemberBinding="{Binding ProductCode}" Width="150" />
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding ProductName }" Width="150 " />
            <GridViewColumn Header=" Units" DisplayMemberBinding="{Binding Unit}" Width="150"  />
            <GridViewColumn Header=" Unit Price" DisplayMemberBinding="{Binding UnitPrice}" Width="120"  />
            <GridViewColumn Header=" Size(gm)" DisplayMemberBinding="{Binding Size}" Width="120"  />
            <GridViewColumn Header=" GST %" DisplayMemberBinding="{Binding GST}" Width="100"  />
            <GridViewColumn Header=" Price" DisplayMemberBinding="{Binding Price}" Width="120"  />
        </GridView>
    </ListView.View>
</ListView>

public partial class Invoice : UserControl
{
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        inv.calculation();
        invoiceLV.Items.Add(inv);
    }
}

And Binding class is:

class Invoice : INotifyPropertyChanged
{
    string productcode;
    string productname;
    double unitprice;
    int size;
    double gst;
    double price;
    string code;
    string name;
    int unit;

    public double Price
    {
        get { return price; }
        set
        {
            if (price != value)
            {
                price = value;
                OnPropertyChanged("Price");
            }
        }
    }

    public double GST
    {
        get { return gst; }
        set
        {
            if (gst != value)
            {
                gst = value;
                OnPropertyChanged("GST");
            }
        }
    }

    public int Size
    {
        get { return size; }
        set
        {
            if (size != value)
            {
                size = value;
                OnPropertyChanged("Size");
            }
        }
    }

    public double UnitPrice
    {
        get { return unitprice; }
        set
        {
            if (unitprice != value)
            {
                unitprice = value;
                OnPropertyChanged("UnitPrice");
            }
        }
    }

    public string ProductName
    {
        get { return productname; }
        set
        {
            if (productname != value)
            {
                productname = value;
                OnPropertyChanged("ProductName");
            }
        }
    }

    public string ProductCode
    {
        get { return productcode; }
        set
        {
            if (productcode != value)
            {
                productcode = value;
                OnPropertyChanged("ProductCode");
            }
        }
    }

    public string Code
    {
        get { return code; }
        set
        {
            if (code != value)
            {
                code = value;
                OnPropertyChanged("Code");
            }
        }
    }

    public string Name
    {
        get { return name; }
        set
        {
            if (name != value)
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    public int Unit
    {
        get { return unit; }
        set
        {
            if (unit!= value)
            {
                unit= value;
                OnPropertyChanged("Unit");
            }
        }
    }

    public void calculation()
    {
        Bussiness.Invoice i = new Bussiness.Invoice();
        ProductName = i.Name(this);
        ProductCode = code;
        Unit = Unit;
        UnitPrice = i.Unit(this);
        GST = 18;
        Size = i.Size(this);
        Price =unit* (UnitPrice + (UnitPrice * (GST / 100)));

    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string PropertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

}

}

And the screenshots are given below.

Image after one record

Image after one record

Image after two records

[image after two records2

Upvotes: 0

Views: 4708

Answers (3)

Santosh Kokatnur
Santosh Kokatnur

Reputation: 364

private void Button_Click(object sender, RoutedEventArgs e)
{
      var invoice = new Invoice();
      invoice.calculation();
      invoiceLV.Items.Add(invoice);
  //Add items to your listview by above code!
}

You can also bind the data into same way!

Upvotes: 0

Rohit
Rohit

Reputation: 10236

It is because everytime you hit the button you are replacing the same instance of inv with different value. The MVVM way to do it is by setting it's itemsource and binding it to a collection.

 ObservableCollection<Invoice> _yourItemSource= new ObservableCollection<Invoice>(); 
 invoiceLV.ItemsSource = _yourItemSource;


  private void Button_Click(object sender, RoutedEventArgs e)
  {

         var inv = new Invoice();
         inv.calculation();
         _yourItemSource.Add(inv);

  }

Upvotes: 2

Nitin Purohit
Nitin Purohit

Reputation: 18580

Ideally you should learn MVVM and bind your collection to your listview. But to solve this problem, you are using the same instance of Invoice to be added again and again. Create new instance and then add.

private void Button_Click(object sender, RoutedEventArgs e)
{
    var inv = new Invoice();
    inv.calculation();
    invoiceLV.Items.Add(inv);


}

Upvotes: 4

Related Questions