using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp3
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public DataTable DataTable
{
get { return dataTable; }
set { dataTable = value; }
}
public DataTable dataTable { get; private set; }
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.DataTable = new DataTable("table");
this.DataTable.Columns.Add("name");
this.DataTable.Columns.Add("age");
this.DataTable.Columns.Add("gender");
this.DataTable.Rows.Add("ddddd", "22", "male");
this.DataTable.Rows.Add("dfff", "11", "male");
this.DataTable.Rows.Add("data", "11", "female");
this.grid1.DataContext = this;
}
}
}
2 Answers
Since you can only bind to an IEnumerable, you should bind to the DefaultView property of the DataTable:
<DataGrid x:Name="grid1" ItemsSource="{Binding _dataTable.DefaultView}" />
You may want to remove _dataTable (a property name should not begin with an underscore) and initialize and bind to the DataTable property instead but the above should work given your current implementation.
_dataTablelooks like it might be a field instead of a property. It should be a property. Also, as jdweng has mentioned, "DataTable" is not the same as "_dataTable".DataTableproperty) please so I can see whats causing the issue