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?