Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building a C# WPF app. My app Contains ListView with Multiple Column. I added column and binding in Listview with XAML

XML
<ListView x:Name="receiptList" HorizontalAlignment="Left" Height="209.987" VerticalAlignment="Top" Width="645" Margin="111,135.748,0,0" IsEnabled="False"  >
                           <ListView.View>
                               <GridView>
                                   <GridViewColumn Header="ReceiptID" Width="60" DisplayMemberBinding="{Binding ReceiptID}"/>
                                   <GridViewColumn Header="ItemNo" Width="50" DisplayMemberBinding="{Binding ItemNo}"/>
                                   <GridViewColumn Header="ItemName" Width="240" DisplayMemberBinding="{Binding ItemName}"/>
                                   <GridViewColumn Header="PackingSize" Width="100" DisplayMemberBinding="{Binding PackingSize}"/>
                                   <GridViewColumn Header="Quantity"  Width="50" DisplayMemberBinding="{Binding Quantity}"/>
                                   <GridViewColumn Header="Price"  Width="50" DisplayMemberBinding="{Binding Price}"/>
                                   <GridViewColumn Header="ExpiryDate"  Width="100" DisplayMemberBinding="{Binding ExpiryDate}"/>
                               </GridView>
                           </ListView.View>
                       </ListView>


Now with help of button i add rows in Listview:
C#
private void addItems_Click(object sender, RoutedEventArgs e)
        {
            
            AddItems addItems = new AddItems(hhk,2,"ABC","36X63",3,opop,"22-11-2014");
            receiptList.Items.Add(addItems);
        }

AddItems Class:

C#
class AddItems
    {



        public int ReceiptID { get; set; }
        public int ItemNo { get; set; }

        public String ItemName { get; set; }
        public String PackingSize { get; set; }
        public int Quantity { get; set; }
        public float Price { get; set; }
        public String ExpiryDate { get; set; }

        public AddItems()
        {

        }

        public AddItems(int oid, int itNo, string itName, string itSize, int qua, float pri, String eDate)
        {
            this.ReceiptID = oid;
            this.ItemNo = itNo;
            this.ItemName = itName;
            this.PackingSize = itSize;
            this.Quantity = qua;
            this.Price = pri;
            this.ExpiryDate = eDate;
        }



    }



Now i want to add a method which counts how many items are added in AddItems and also i want to retrieve items from AddItems with the help of loop
Posted
Comments
Tomas Takac 26-Nov-14 8:45am    
I don't understand. AddItems is just one item in the list. You are adding the items to receiptList.Items - this is your collection. To get the count query receiptList.Items.Count. BTW I would encourage you to study MVVM pattern, it goes very well with WPF.

Try this

C#
//For Item Count
       public int ItemAdd()
       {
           return listView1.Items.Count;
       }

       //Get Item
       public void  GetItem()
       {
           foreach(ListViewItem lst in  listView1.Items)
               {
                   var x = lst.Content;
               }
       }
 
Share this answer
 
Comments
Burhan Ahmed 26-Nov-14 9:06am    
there is multiple rows and column, can u tell how to iterate lst.content and get each value one by one
how about you count in reCieptList like:
C#
private int Count()
 {
    var item = (from i in recieptList select i).ToList();
    return item.Count();
 }

hope that is what you want, if not then sorry.
 
Share this answer
 
I'd suggest to read it: ListView, data binding and ItemTemplate[^]
There you'll find a sample code which uses List of custom class. You need exactly the same!

Here is similar question: http://stackoverflow.com/questions/13647704/listview-binding-to-list-of-custom-class-containing-dictionnary[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900