Click here to Skip to main content
12,253,371 members (50,548 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# WPF
Hello everybody..have a little simple question. How I can add item in ListView without creation special class???

Example:

Special class
public class MyItem
{
    public int Id { get; set; }
    public string Name { get; set; }
}
 
XAML
<ListView x:Name="listView">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
        </GridView>
    </ListView.View>
</ListView>
 
Code
public Window1()
{
    this.InitializeComponent();
    this.listView.Items.Add(new MyItem { Id = 1, Name = "Davidwroxy" });
}
Posted 13-Dec-13 8:19am
Comments
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.
—SA
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.
???
Sergey Alexandrovich Kryukov 13-Dec-13 18:05pm
   
Of course you should not create a class and bind it. You need to use Items directly:
http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items%28v=vs.110%29.aspx
It's a considerable work, but the idea is to create type-agnostic population. Again, it all totally depends on how your input data is organized.
—SA
zyvpeople 13-Dec-13 18:25pm
   
my input data is string-array, every element must be in separate column, one array is one row..and if I will add data directly it display many rows but one column...

I have listview with gridview that have columns...and my problem is filling a grid correctly
Sergey Alexandrovich Kryukov 13-Dec-13 18:35pm
   
Is it all pure string data? It would be unfortunate, what a trend to work with strings representing data instead of data itself?
Anyway, not clear: items themselves have items (per column), so input data should be 2D array...
—SA
zyvpeople 14-Dec-13 2:55am
   
"items themselves have items (per column)"..and how I can do it? I dont know?
can you help me, write a code to explain how its work ?
if I have for example 2 string-arrays: string[] a = new string[]{"1","2","3"} and string[] b = new string[]{"4","5","6"}. What I must do to add this arrays in the rows of listview, and arrays elements in a columns ????
Sergey Alexandrovich Kryukov 14-Dec-13 13:17pm
   
I don't understand. It depends on which element do you want in each cell...
—SA
zyvpeople 14-Dec-13 17:47pm
   
string elements..

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

THEN

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.
  Permalink  
Comments
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 13 Dec 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100