In this post I am going to answer some questions concerning my previous WPF post on:ListViewItem in a ListView
In my previous post, I had a WPF ListView that displayed two items, and each item occupied one row and one column.

But what if I would like to add an item to a WPF ListView but this time that one row contains more than one column. In this way we can answer questions such as: “How to add a ListView item in two columns using c# code”. If this is the question you are looking to answer, you have found just what you were looking for. I will show you how to do the following in Visual Studio 2008:
Window1.xaml
<Window x:Class=”ListViewProj.Window1″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”Window1″ Height=”220″ Width=”285″>
<Grid>
<ListView Margin=”11,16,35,17″ Name=”listView1″ />
</Grid>
</Window>
Window1.xaml.cs
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
// The constructor accepts a first name, last name, major
Student student = new Student("Daniel","Isaacs","Computer Science");
ObservableCollection
// add a student
studentObj.Add(student);
// displays data items in columns for a ListView
GridView gView = new GridView();
// set the first gridview column
GridViewColumn firstCol = new GridViewColumn();
// set the data item to bind to for this column
firstCol.DisplayMemberBinding = new Binding(”FirstName”);
firstCol.Header = “First Name”;
// set the second gridview column
GridViewColumn secondCol = new GridViewColumn();
secondCol.DisplayMemberBinding = new Binding(”LastName”);
secondCol.Header = “Last Name”;
// set the third gridview column
GridViewColumn thirdCol = new GridViewColumn();
thirdCol.DisplayMemberBinding = new Binding(”Major”);
thirdCol.Header = “Major”;
// add gridview columns
gView.Columns.Add(firstCol);
gView.Columns.Add(secondCol);
gView.Columns.Add(thirdCol);
// set the collection that is going to be used to generate content
listView1.ItemsSource = studentObj;
// sets how data is organized and styled
listView1.View = gView;
}
}
In the XAML code I only added a ListView. In the code behind I created an instance of the Student class (a class I created for this example). Then I add the instance of the student class to the collection that is going to be used to generate content.
I also created an instance of the GridView class, because the gridview is how we are going to organized the data. After this I create three instances of the GridViewColumn class(you can created as many instances of the GridViewColumn as you need). Each GridViewColumn needs a binding (DisplayMemberBinding) which sets the data item to bind to for this column.
Don’t forget to add the GridViewColumn to the GridView. At the end assign to the listView ItemSource Property the collection with the content, and assign to the listview View Property the Gridview instance.

Here’s a copy of my files.
If you have any comments or questions just let me know.
April 10th, 2008 at 9:20 am
I’ve followed this example, and tried to make a loop with insertion of several lines into the listView. Basically I just create new instances of the student class and add them with studentObj.Add(student);
This results in a listview with the headers ok, but each added line is empty. Do you happen to have a working example of this?
April 25th, 2008 at 12:21 pm
Hello,
Alright this work perfectly.But Why don’t you style your listview using xaml in the property?
you could then create datatemplate for the cells like this :
and that would make much less lines in your code behind, just setting the ItemSource property (and even that you could do it in xaml in fact…)
April 25th, 2008 at 12:23 pm
uff, It seems like I can’t post code lines in the comment.. But I hope you got my idea =)
May 10th, 2008 at 4:04 pm
Hi.
This looks great.
But how to bind from an unknown array, at runtime. From example you have an string[] array and need to create as many columns as needed and add those items to the ListView. If you know please learn me how to do it.
Thanks.
March 16th, 2009 at 3:03 pm
Hi Daniel
I found the solution for my listview problem in your blog
All other examples i found based on XAML.
After solving the problem I want more but I don’t know how.
The feature I need is a tooltip per item in the Listview. eg the propertie major as
a tooltip in your example.
Could you give me a hint?
mfg
Sven