1

Good day.

I am trying to display data from System.Data.DataTable in Microsoft.Toolkit.Uwp.UI.Controls.DataGrid. I bind the component to the table through the CollectionViewSource.

Under UWP, cannot work with Windows.UI.Xaml.Data.CollectionViewSource object.

Unable to assign CollectionViewSource.Source by System.Data.DataTable object.

When trying to assign, an error is thrown:

System.ArgumentException: 'Value does not fall within the expected range.'

Here's some sample code:

private void Button_Click(object sender, RoutedEventArgs e)
{
   System.Data.DataSet northwindDataSet = new DataSet();
   northwindDataSet.ReadXml(@"NorthwindDataSet.Xml");
   System.Data.DataTable dataTable = northwindDataSet.Tables["Customers2"];

  Windows.UI.Xaml.Data.CollectionViewSource viewSource = new CollectionViewSource();
  viewSource.Source = dataTable;
}

Under WPF, similar code works correctly, without errors.

What the mistake is. enter image description here

DmitryB
  • 455
  • 1
  • 5
  • 18

1 Answers1

1

I'm afraid you can't set viewSource.Source as DataTable within UWP platform. if you want to fill the DataTable into datagrid, For more please refer this case reply.

private ObservableCollection<object> collection = new ObservableCollection<object>();
public void FillDataGrid(DataTable table, DataGrid grid)
{
    grid.Columns.Clear();
    for (int i = 0; i < table.Columns.Count; i++)
    {
        grid.Columns.Add(new DataGridTextColumn()
        {
            Header = table.Columns[i].ColumnName,
            Binding = new Binding { Path = new PropertyPath("[" + i.ToString() + "]") }
        });
    }

    var collection = new ObservableCollection<object>();
    foreach (DataRow row in table.Rows)
    {
        collection.Add(row.ItemArray);
    }

    grid.ItemsSource = collection;
}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36