Click here to Skip to main content
15,662,823 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everybody..have a little simple question. How I can add item in ListView without creation special class???


Special class
public class MyItem
    public int Id { get; set; }
    public string Name { get; set; }

<ListView x:Name="listView">
            <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>

public Window1()
    this.listView.Items.Add(new MyItem { Id = 1, Name = "Davidwroxy" });
zyvpeople 13-Dec-13 15:05pm    
if I have listView with many columns - its not comfortably to create class with many fields..=(
bowlturner 13-Dec-13 15:51pm    
Where is the data coming from that you want to display in the ListView?
zyvpeople 13-Dec-13 16:27pm    
data comes from some function or received from network.. data have a structure such as in task manager - table with different columns
Sergey Alexandrovich Kryukov 13-Dec-13 16:08pm    
What make you thinking you have to "create a class with many fields"? You have some data on input, no matter if you want that much or not. You have to populate it. Do it. What's the problem? It all depends on how the input data is represented. Come from there.
zyvpeople 13-Dec-13 16:27pm    
Ok. How I can populate it? I know only one method..its create class with same number of fields as columns in listView. So if I have nearly 20 columns, its will be uncomfortably do this operation:

this.listView.Items.Add(new MyItem { Id = 1, Name = "Davidwroxy" });

Is there other method to fill the listview .. like a matrix? Like in DataTable. We create rows and columns, and then we can use it like matrix.

1 solution

You need to start by building some kind of data object to hold the '20' columns of data, 1 object holds 1 row.


You Add each row/object to a List, Collection, or some other 'list' that has the IEnumerable interface implemented.

You can fill the ListView by binding your List to the ListView.

When you get your data from the the source you fill an object and put it in the list.

You should have a methods that just takes your input and converts it to an object of your design.
Share this answer
zyvpeople 13-Dec-13 17:08pm    
Understand..list is the main part of this scheme! but I cant understand this.."'20' columns of data, 1 object holds 1 row" ?
if I have for exaple 5 strings in a row, how I must create this object?
object obj = (object)new string[5] ???
can you explain? please=)
bowlturner 13-Dec-13 17:24pm    
Kind of like your MyItem.

class My5Strings
string String1 { get; set; }
string String2 { get; set; }
string String3 { get; set; }
string String4 { get; set; }
string String5 { get; set; }
zyvpeople 13-Dec-13 17:55pm    
so if I have 5 columns - I create My5String. But if I have second listview with other number of column, 3 or 7 , I must create new classes ?

class My3Strings
string String1{get;set;}
string String2{get;set;}
string String3{get;set;}

class My7Strings
string String1{get;set;}
string String2{get;set;}
string String3{get;set;}
string String4{get;set;}
string String5{get;set;}
string String6{get;set;}
string String7{get;set;}

In result, We have three almost the same classes. We have code redundancy =( Is't it ?
bowlturner 16-Dec-13 9:21am    
yes it would be code redundancy if you did that. If String1-3 are really the same for all three objects then it might be wise to have a My3Strings object be inherited by My5Strings and My7Strings.

I would certainly hope you don't have a generic object with any of these names we are using for explanation purposes.

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