0
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;

        }
    }
}
6
  • What is "the error"? Commented Aug 29, 2018 at 13:08
  • Binding is case sensitive so make sure you are using the correct case. Also see msdn : learn.microsoft.com/en-us/dotnet/framework/wpf/data/… Commented Aug 29, 2018 at 13:09
  • i am not getting any error message , but i am not able to see my data Commented Aug 29, 2018 at 13:13
  • _dataTable looks 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". Commented Aug 29, 2018 at 13:18
  • @monishtickoo I have just tested this and had no problems, could you post the full code (including your DataTable property) please so I can see whats causing the issue Commented Aug 29, 2018 at 13:34

2 Answers 2

1

In the WindowLoaded method you have referenced your DataTable field as _dataTable but defined it as dataTable.

Simply update either one of these to match.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.