Need to create RadDataGrid in Xamarin Forms programmatically

1 Answer 188 Views
DataGrid
William
Top achievements
Rank 1
William asked on 30 Apr 2021, 06:29 PM

Hi, I need to create RadDataGrid in xamarin forms programmatically. it only creates the header and it's fine. But when I insert new data in ItemSource there has no new row added in UI while ItemSource already updated with new row data. I have tried in several ways and still, there has no progress. Have any other options to add a data row in RadDataGrid? code snippets are attached.

 


  private readonly RadDataGrid dataGrid = new RadDataGrid() { IsEnabled = true, AutoGenerateColumns = false };
  private List<GridModel> dataSource = new List<GridModel>(); 

  public async Task DoSetup()
        {
             Grid outermost = this.GetOutermostGrid();
             this.Content = outermost;
             AddHeaderRow(columsList);
             dataGrid.ItemsSource = dataSource;
             outermost.Children.Add(dataGrid);
             Grid.SetColumn(dataGrid, 0);
        }

private void AddHeaderRow(IEnumerable<string> headers)
        {
            foreach (string s in headers)
            {
                this.dataGrid.Columns.Add(new DataGridTextColumn() { HeaderText = s, SizeMode = DataGridColumnSizeMode.Stretch, Name = s, PropertyName = s });
            }
        }

private void AddNewRow(GridModel model)
        {
            //dataSource.Add(model);
            //dataGrid.ItemsSource = dataSource;
            (dataGrid.ItemsSource as List<GridModel>).Add(model);
        }

1 Answer, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 30 Apr 2021, 07:38 PM
HI William, please change `List<GridModel>` to `ObservableCollection<GridModel>`.  When you add items to List at runtime, this will not update any UI elements. Also, when you call `AddNewRow()`, make sure you are not on a background thread.
William
Top achievements
Rank 1
commented on 04 May 2021, 07:42 AM

Thanks for your help. it's working now
Tags
DataGrid
Asked by
William
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or