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

1
  • Any updates for this thread? Commented May 10, 2021 at 8:04

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

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.