1
votes

I'm having problems with binding to a data grid. I've got a dynamic data set, so I can't just create objects. I've tried using both an array of ExpandoObjects and a DataTable, the result is the same. If I explicitly create the dataset, it works but this doesn't do me any good. My xaml is: <sf:SfDataGrid BackgroundColor="{ DynamicResource DataGridHeaderRowBackgroundColor }" ItemsSource="{Binding TableData, Mode=OneWay}" Columns="{Binding Columns, Mode=OneWay}" AutoGenerateColumns="False"></sf:SfDataGrid>

For example:

int cellIndex = 0;
foreach (TableCell cell in row.Cells.OrderBy(c => c.ColumnIndex))
{
    GridTextColumn column = new GridTextColumn
    {
        HeaderText = cell.CellText
    };

    if (cell.CellType == TableCellType.Checkbox)
    {
        column.ColumnSizer = ColumnSizer.SizeToHeader;
    }
    else
    {
        column.Width = 200;
    }

    column.MappingName = cellIndex.ToString();
    column.HeaderFontAttribute = FontAttributes.Bold;
    column.HeaderTextAlignment = TextAlignment.Center;
    column.HeaderFont = "SourceSansPro-Bold.ttf#Source Sans Pro";
    column.HeaderCellTextSize = 14;
    column.LoadUIView = true;
    column.LineBreakMode = LineBreakMode.CharacterWrap;

    this.Columns.Add(column);
    cellIndex++;
}

Dictionary s = new Dictionary();
s.Add("0", "zero");
s.Add("1", "one");
s.Add("2", "two");
s.Add("4", "three");
s.Add("5", "four");
s.Add("6", "five");
s.Add("7", "six");
s.Add("8", "seven");
s.Add("8", "eight");
tableData.Add(s.ToExpando());

this.TableData = tableData;

Now, for my real data I do this (column stuff is exactly the same). This does not work, I get the attached screenshot.

Dictionary rowData = new Dictionary();
int cellIndex = 0;
foreach (TableCell cell in row.Cells.OrderBy(c => c.ColumnIndex))
{
    rowData.Add(cellIndex.ToString(), string.IsNullOrEmpty(cell.CellText) ? "EMPTY" : cell.CellText);
    cellIndex++;
}

tableData.Add(rowData.ToExpando());

Screenshot

1
Interestingly, if I use Visual Studio and change the xaml (I was testing background color) the grid renders correctly.Nick

1 Answers

1
votes

You have defined LoadUIView for column as true change it false so the text will be rendered for Android platform alone.

For your information.

In Syncfusion DataGrid, text is drawn for better performance in Xamarin.Forms Android only, so that text is rendered. If we load Label for the same scenario by setting LoadUIView as true for all the column, then you will face rendering issue “Cell value doesl not render” in all the platform it is because Xamarin forms have not yet given binding support to the Expando object.

In Xamarin forms ExpandoObject binding is under implementation.
https://github.com/xamarin/Xamarin.Forms/issues/3177

Please refer the following link for more details regarding IOS platform limitation for Dynamic object.
Discussion Link : https://forums.xamarin.com/discussion/53941/expandoobject-crashing-ios
Limitation Link: https://docs.microsoft.com/en-gb/xamarin/ios/internals/limitations

Suggest you to use ObservableCollection or DataTable collection as SfDataGrid Itemsource.until Expando object binding support is provided by xamarin forms framework.

Regards,
Pradeep Kumar B