0

My MainWindow Class have a property called "DataTable":

'Table To import the Excel Sheet
Public Property DataTable As DataTable

An Excel Sheet sets the DataTable property. My question is why I cannot bind that property to a DataGrid. The following code doesn't work:

<DataGrid Name="TestGrid" Visibility="Visible" ItemsSource="{Binding Path=DataTable}">
</DataGrid>

The answer to this question seems to be unclear and doesn't explain too much about how to use the ObjectDataProvider Class.

fpuglap
  • 109
  • 1
  • 2
  • 15
  • Does this answer your question? [How to bind DataTable to Datagrid](https://stackoverflow.com/questions/20770438/how-to-bind-datatable-to-datagrid) – Keith Stein May 01 '20 at 02:16
  • @KeithStein I read that question too. There is no accepted answer and the approach is from code-behind. I want to make it from XAML. Maybe it can't be done with XAML only? – fpuglap May 01 '20 at 02:24
  • What if instead of `{Binding DataTable}`, you tried `{Binding DataTable.DefaultView}`? – Keith Stein May 01 '20 at 02:36
  • @KeithStein I tried that too before to post this question. Didn't work. – fpuglap May 01 '20 at 03:24
  • 2
    Wow, this is actually an interesting thing. I did some testing. In all cases `ItemsSource` gets set to the same, correct value. The only difference is *when* the value gets set. If you take your code as you have it, and then manually change `AutoGenerateColumns` to `false` and then back to `true`, the correct columns come up. This seems like a bug in `DataGrid`. – Keith Stein May 01 '20 at 04:33

0 Answers0