Thank you, that solved my problem
For anyone struggling with how to add dynamic (variable number of) rows and columns dependent on the underlying data, here is the test code I used and have attached a picture of the resulting grid
Xml
<syncfusion:SfDataGrid
x:Name="XmlDataGrid"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
AutoGenerateColumns="False"
GridTapped="TappedXmlGrid">
</syncfusion:SfDataGrid>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Syncfusion.SfDataGrid.XForms;
using System.Collections.ObjectModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace sfDataGridDynamicColumns
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AsfDataGridPage : ContentPage
{
public AsfDataGridPage ()
{
InitializeComponent ();
DisplayDataInSfGrid();
}
private void DisplayDataInSfGrid()
{
int iColCount = 30;
int iRowCount = 50;
#region CreateTheColumns
XmlDataGrid.AutoGenerateColumns = false;
// First column used ONLY as an index key for the number of rows in the data
GridTextColumn rowIdColumn = new GridTextColumn();
rowIdColumn.MappingName = "RowID";
rowIdColumn.HeaderText = "Row";
rowIdColumn.IsHidden = true;
XmlDataGrid.Columns.Add(rowIdColumn);
for (int iCol = 1; iCol <= iColCount; iCol++)
{
string sColName = iCol.ToString();
string sColHeading = "Head "+iCol.ToString();
GridUnboundColumn MyUnboundColumn = new GridUnboundColumn()
{
MappingName = sColName,
HeaderText = sColHeading
};
XmlDataGrid.Columns.Add(MyUnboundColumn);
}
XmlDataGrid.FrozenColumnsCount = 2;
#endregion
#region CreateTheRows
DataGridRowInfoRepository DataGridRows = new DataGridRowInfoRepository();
XmlDataGrid.ItemsSource = DataGridRows.RowInfoCollection;
for (int iRow = 0; iRow < iRowCount; iRow++)
{
DataGridRowInfo aDataGridRow = new DataGridRowInfo(iRow);
DataGridRows.RowInfoCollection.Add(aDataGridRow);
}
#endregion
//
XmlDataGrid.QueryCellStyle += DataGrid_QueryCellStyle;
XmlDataGrid.QueryUnboundColumnValue += DataGrid_QueryUnboundColumnValue;
}
#region DataModel
public class DataGridRowInfo
{
public int RowID { get; set; }
public DataGridRowInfo(int iRow)
{
this.RowID = iRow;
}
}
public class DataGridRowInfoRepository
{
private ObservableCollection<DataGridRowInfo> _RowInfo;
public ObservableCollection<DataGridRowInfo> RowInfoCollection
{
get { return _RowInfo; }
set { this._RowInfo = value; }
}
public DataGridRowInfoRepository()
{
_RowInfo = new ObservableCollection<DataGridRowInfo>();
}
}
#endregion
#region Grid Events
private void DataGrid_QueryUnboundColumnValue(object sender, GridUnboundColumnEventArgs e)
{
if (e.UnboundAction == UnboundActions.QueryData)
{
string sColumn = e.Column.HeaderText;
string sColumnName = e.Column.MappingName;
var iRow = Convert.ToInt16(e.Record.GetType().GetProperty("RowID").GetValue(e.Record));
string sRow = iRow.ToString();
e.Value = sColumnName + " " + sRow;
}
}
private void DataGrid_QueryCellStyle(object sender, QueryCellStyleEventArgs e)
{
if (e.ColumnIndex == 1)
{
//style the left hand column as per the heading
e.Style.BackgroundColor = Color.White;
}
else if (e.ColumnIndex == 4 && e.RowIndex % 3 == 1)
{
// give some cells a different background color (as a test)
e.Style.BackgroundColor = Color.BlueViolet;
e.Style.ForegroundColor = Color.White;
}
else
{
// default background color
e.Style.BackgroundColor = Color.Silver;
}
e.Handled = true;
}
public void TappedXmlGrid(object sender, GridTappedEventArgs e)
{
//Process user tapping the grid
var rowIndex = e.RowColumnIndex.RowIndex;
var rowData = e.RowData;
var columnIndex = e.RowColumnIndex.ColumnIndex;
string sMessage = "You tapped Row " + rowIndex.ToString() + ", Column " + columnIndex.ToString();
Application.Current.MainPage.DisplayAlert("Alert", sMessage, "OK");
}
#endregion
}
}
Attachment:
grid_261d72f3.zip