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());