0

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

0

3 Answers 3

4

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);


}
Sign up to request clarification or add additional context in comments.

2 Comments

hey #Nitin thanx for help. but I am still facing same problem :(
okay finally I solved it. I have to bind data again after new instance created. like in above code at the end this line must be added. DataContext=inv;
2

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);

  }

1 Comment

heyy Kayle.. should I make "_yourItemSource" in constructor or behind button?
0
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.