Avalonia: Improve DataGrid binding to System.Data.DataTable

Created on 28 Jul 2019  路  1Comment  路  Source: AvaloniaUI/Avalonia

I'm playing around with Avalonia, and got stuck trying to bind a DataGrid control to a DataTable.

In WPF binding the DataGrid to the table's DefaultView property causes the table's data to display. The Avalonia DataGrid just shows the properties of the row object.

It would be a nice feature to have if it would know by default how to bind to a DataTable (or really the DataView attached to the table).

I suspect there could be a possible workaround, defining a DataTemplate, but I haven't figured it out yet. Any tips would be welcome.

datagrid enhancement

Most helpful comment

For anyone struggling with the same problem, here's the workaround I came up with:

  1. Expose the DataTable's default view on the ViewModel:
        public DataView DataView
        {
            get => _dataTable.DefaultView;
        }
  1. Bind it up like this (XAML):
      <DataGrid Name="MyGrid" Items="{Binding DataView}">
      </DataGrid>
  1. In the codebehind (.xaml.cs file), add a handler for the Activated event
        public MainWindow()
        {
            Activated += MainWindow_Activated; // <-- This line

            InitializeComponent();
#if DEBUG
            this.AttachDevTools();
#endif
        }
  1. Implement the event handler like this:
        private void MainWindow_Activated(object sender, System.EventArgs e)
        {
            var grid = this.FindControl<DataGrid>("MyGrid");
            var vm = (MainWindowViewModel)DataContext;

            var cols = vm.DataView.Table.Columns;

            for (var i = 0; i < cols.Count; i++)
            {
                grid.Columns.Add(new DataGridTextColumn
                {
                    Header = cols[i].ColumnName,
                    Binding = new Binding($"Row.ItemArray[{i}]"),
                });
            }
        }

>All comments

For anyone struggling with the same problem, here's the workaround I came up with:

  1. Expose the DataTable's default view on the ViewModel:
        public DataView DataView
        {
            get => _dataTable.DefaultView;
        }
  1. Bind it up like this (XAML):
      <DataGrid Name="MyGrid" Items="{Binding DataView}">
      </DataGrid>
  1. In the codebehind (.xaml.cs file), add a handler for the Activated event
        public MainWindow()
        {
            Activated += MainWindow_Activated; // <-- This line

            InitializeComponent();
#if DEBUG
            this.AttachDevTools();
#endif
        }
  1. Implement the event handler like this:
        private void MainWindow_Activated(object sender, System.EventArgs e)
        {
            var grid = this.FindControl<DataGrid>("MyGrid");
            var vm = (MainWindowViewModel)DataContext;

            var cols = vm.DataView.Table.Columns;

            for (var i = 0; i < cols.Count; i++)
            {
                grid.Columns.Add(new DataGridTextColumn
                {
                    Header = cols[i].ColumnName,
                    Binding = new Binding($"Row.ItemArray[{i}]"),
                });
            }
        }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

RUSshy picture RUSshy  路  4Comments

GitHubington picture GitHubington  路  3Comments

MarchingCube picture MarchingCube  路  4Comments

JonathaN7Shepard picture JonathaN7Shepard  路  4Comments

khoshroomahdi picture khoshroomahdi  路  4Comments