3

I need help with a simple project I am doing in Visual Studio just to learn coding a little bit. I would like to show a table containing objects from a class called Car in a WPF, and this table should be done programmatically.

So, I added a ListView in MainWindow.xaml and set the name for GridView to GridView1

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView HorizontalAlignment="Center" Height="100" Margin="0,10,0,0" VerticalAlignment="Top" Width="100">
            <ListView.View>
                <GridView x:Name="GridView1">
                    <GridViewColumn/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

In the MainWindow.xaml.cs I did this

using System;
...
//Add this to use DataTable
using System.Data;

namespace Test
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //create some objects
            Car ferrari = new Car("Ferrari","Front-Wheel");
            Car mercedes = new Car("Mercedes","Rear-Wheel");

            //create a DataTable
            DataTable dt = new DataTable();
            //add 2 columns
            dt.Columns.Add("Vehicle Name");
            dt.Columns.Add("Vehicle Drive");
            //add 2 rows
            dt.Rows.Add(ferrari.Name, ferrari.Drive);
            dt.Rows.Add(mercedes.Name, mercedes.Drive);
            //bind it to GridView
            GridView1.DataSource = dt;
            GridView1.DataBind();          
        }
    }
}

The problem are the last 2 lines, Visual Studio says it does not contain a definition nor extension method for 'DataSource' and 'DataBind', but all the examples I saw online make use of them. Where is the mistake?

Emmet
  • 198
  • 4
  • 13

1 Answers1

6

Change the xaml so that it looks like this :

    <ListView HorizontalAlignment="Center" Height="100" Margin="0,10,0,0" 
              VerticalAlignment="Top" Width="250" ItemsSource="{Binding dt}">
        <ListView.View>
            <GridView x:Name="GridView1">
                <GridViewColumn DisplayMemberBinding= "{Binding Path=Name}" 
                                Header="Vehicle Name" Width="100"/>
                <GridViewColumn DisplayMemberBinding= "{Binding Path=Drive}" 
                                Header="Vehicle Drive" Width="100"/>
            </GridView>
        </ListView.View>
    </ListView>

Change your code behind :

using System.Windows;
using System.Windows.Controls;
namespace SOWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var carViewModel = new CarViewModel();
            carViewModel.LoadCars();
            this.DataContext = carViewModel;
        }
    }
}

Add a View Model and instead of using a datatable, you are using an ObservableCollection.

using System.Collections.ObjectModel;
using System.Data;
namespace SOWPF
{
    public class CarViewModel
    {
        public ObservableCollection<Car> dt { get; set; }
        public void LoadCars()
        {
            dt = new ObservableCollection<Car>();
            Car ferrari = new Car("Ferrari", "Front-Wheel");
            Car mercedes = new Car("Mercedes", "Rear-Wheel");
            dt.Add(ferrari);
            dt.Add(mercedes);
        }

    }

    public class Car
    {
        public Car(string name, string drive)
        {
            Name = name;
            Drive = drive;
        }
        public string Name { get; set; }
        public string Drive { get; set; }
    }
}
Walt Ritscher
  • 6,977
  • 1
  • 28
  • 35
cvraman
  • 1,687
  • 1
  • 12
  • 18
  • Thank you very much for your answer! Is there any specific reason to use an ObservableColletion instead of a DataTable? – Emmet Mar 09 '14 at 12:14
  • Observable collection is used when you want to be notified of changes to your collection. See [this](http://stackoverflow.com/questions/4279185/what-is-the-use-of-observablecollection-in-net) – cvraman Mar 09 '14 at 12:29