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.