Good day,
Is it possible to bind a the xamdatagrid to a list of Dictionary Items and display it's values?
Let me explain. I have a REST call that returns a list of records, the columns are dynamic, ie depending on the call structure the call can return list of record with 5 columns or another call can return a list of records with 10 columns and so on.
So I want to bind the result to a xamadatagrid. I currently get the result back and the xamdatagrid display the number of rows, but each row /column has no value.
From my understanding, for xamarin forms, that if you put the columns names in brackets, [], it will map the key field of the dictionary to the column name and the value to the actual value, it is working like that on listviews and other view bindings. I have also seen a sample on your support forums, but it was silverlight, that did the same thing. So for the PropertyPath value of a columns that I add I set to the ColumnName in brackets when I know that I do not the actual record layout, see code snippet below.
I condensed the code snippet to try and explain what I am trying todo:
/// <summary> /// Return a list of records from json content /// </summary> /// <param name="content"></param> /// <returns>List of Records in KeyValue pairs: Key = ColumnName and Value = ColumnValue </returns> protected override ObservableRangeCollection<Dictionary<string, object>> DeserializeObject(string content) { var result = JsonConvert.DeserializeObject<ObservableRangeCollection<Dictionary<string, object>>>(content); return result; }
private void ViewModelOnCollectionChanged(object o, ItemsEventArgs itemsEventArgs) { try { var columns = ViewModel.ColumnsTypes; // get columns names CreateGridColumns(columns); // create columns for grid EntityGrid.ItemsSource = DeserializeObject(ViewModel.FormData); // get records } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } }
private void CreateGridColumns(List<GridColumnsEntity> columns) { var grid = EntityGrid; if (columns != null) { grid.AutoGenerateColumns = false; grid.DefaultColumnWidth = ColumnWidth.Default; grid.Columns?.Clear(); foreach (var column in columns) { switch ((FieldTypes) column.TypeID) { case FieldTypes.Number: { grid.Columns?.Add(new NumericColumn { PropertyPath = ViewModel.IsColumnNameNotKnown ? "[" + ClientHelper.ToCamelCase(columnName) + "]" : columnName, HeaderText = column.Description, }); break; } case FieldTypes.Currency: { grid.Columns?.Add(new NumericColumn { PropertyPath = ViewModel.IsColumnNameNotKnown ? "[" + ClientHelper.ToCamelCase(columnName) + "]" : columnName, HeaderText = column.Description, FormatString = "C" }); break; } case FieldTypes.AppointmentDate: case FieldTypes.DateTime: case FieldTypes.Date: { if (!Enum.TryParse(ClientHelper.DateFormat, out DateTimeFormats format)) { format = DateTimeFormats.DateShort; } var dateCol = new DateTimeColumn { // PropertyPath = ViewModel.IsColumnNamesNotKnown ? "[" + ClientHelper.ToCamelCase(columnName) + "]" : columnName, HeaderText = column.Description, DateTimeFormat = format, FormatString = ClientHelper.DateFormat, Width = new ColumnWidth { IsStarSized = false, Value = 100 } }; dateCol.IsHidden = false; grid.Columns?.Add(dateCol); break; } case FieldTypes.LongText: { grid.Columns?.Add(new TextColumn() { PropertyPath = ViewModel.IsColumnNamesNotKnown ? "[" + ClientHelper.ToCamelCase(columnName) + "]" : columnName, HeaderText = column.Description, Width = new ColumnWidth {IsStarSized = false, Value = 200} }); break; } default: { grid.Columns?.Add(new TextColumn() { PropertyPath = ViewModel.IsColumnNamesNotKnown ? "[" + ClientHelper.ToCamelCase(columnName) + "]" : columnName, HeaderText = column.Description, Width = new ColumnWidth {IsStarSized = false, Value = 100}, }); break; } } } } else { grid.DefaultColumnWidth = new ColumnWidth {IsStarSized = false, Value = 200}; grid.AutoGenerateColumns = true; } }
Hello Key360,
Thank you for your post(s). I am glad you were able to resolve this issue. It sounds like perhaps you needed to rebuild or re-deploy your Xamarin.Forms application to the device/simulator that you were testing on.
I would just like to reconfirm that your understanding of putting the column names in [] brackets that it maps the column, and so the code that you have provided should certainly work.
Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
Nevermind, got it working. I did not do anything, it just started to work.