CS:
public List Cols;
public XeDataTable DataTable { get; set; }
protected override async Task OnInitializedAsync()
{
//await Task.Yield();
await RefreshGrid(SGrid, First);
}
private async Task RefreshGrid(SfGrid dataGrid, bool forced)
{
this.IsBusy = true;
try
{
if (First)
{ ....loading COLUMNS, actions etc. from remote source
}
if (DataTable.GridModel != null) //dynamically build SfGrid columns when empty
{
if (Cols == null || Cols.Count == 0)
{
CreateGridColumns(DataTable);
}
}
await DataTable.LoadData(); // Load data ROWS from remote source
finally
{
this.IsBusy = false;
if (First == false)
{
SGrid.Refresh();//!! required when DataSource2 is NOT ObservableCollection (just List<>). When ObservableCollection is used, loading grid is SLOW,flickering....
}
First = false;
}
}
}
public async Task OnRefreshClick()
{
if (IsBusy) return;
await RefreshGrid(SGrid, false);
}
public void RowSelectingHandler(RowSelectingEventArgs args)
{
}
public void GridCommandClickHandler(CommandClickEventArgs args)
{
}
protected void CreateGridColumns(XeDataTable DataTable)
{
try
{
var columns = new List();
Cols = new List();
foreach (sDCol field in DataTable.FieldDefinitions) //build all columns....
{
var column = new GridColumn
{
HeaderText = field.PubName,
Field = field.SysName,
FilterSettings = new FilterSettings() { Operator = Operator.Contains },
IsPrimaryKey = field.isUnique,
Visible = !field.isHidden,
AutoFit = true
};
if (field.DType == enDataType.Boolean)
{
column.DisplayAsCheckBox = true;
}
}
var columnact = new GridColumn
{
HeaderText = "Action",
AutoFit = false,
AllowFiltering = false,
AllowSorting = false,
Width = "60"//,IsFrozen=true//,AllowSearching=false
};
columnact.Commands = new List();
columnact.Commands.Add(new GridCommandColumn()
{
Title = "Action",
Type = CommandButtonType.None,
ButtonOption = new CommandButtonOptions()
{
//IconPosition=Syncfusion.Blazor.Buttons.IconPosition.Left,
//Content="ABC",
IsPrimary = true,
IconCss = "e-icons e-edit",
CssClass = "e-flat"
}
});
columns.Insert(0, columnact);
Cols = columns; //assign colums
}
}
public void CustomizeCell(QueryCellInfoEventArgs args)
{
var data = args.Data;
if (args != null && args.Data != null)
{
List styles = new List();
// Grid row background color
var rowBgrColorData = args.Data.FirstOrDefault(x => x.Key.StartsWith(Constants.SysParRowColorBack));
if (rowBgrColorData.Value != null)
{
Color bgrColor = (Color)new ColorConverter().ConvertFromString(Convert.ToString(rowBgrColorData.Value));
styles.Add("background-color: " + "#" + bgrColor.R.ToString("X2") + bgrColor.G.ToString("X2") + bgrColor.B.ToString("X2"));
}
// Grid cell background color
string colfulll = Constants.SysParColColorBack + args.Column.Field;
var bgrColorData = args.Data.FirstOrDefault(x => x.Key == colfulll);
if (bgrColorData.Value != null)
{
Color bgrColor = (Color)new ColorConverter().ConvertFromString(Convert.ToString(bgrColorData.Value));
styles.Add("background-color: " + "#" + bgrColor.R.ToString("X2") + bgrColor.G.ToString("X2") + bgrColor.B.ToString("X2"));
}
if (styles.Count > 0)
{
args.Cell.AddStyle(styles.ToArray());
}
}
}
public class XeDataTable //: ObservableBase
{
private WE1App App { get; set; }
public XeDataTable(WE1App _app)
{
App = _app;
}
public IList FieldDefinitions { get; set; }
public DataRecordSource DataSource { get; set; }=new DataRecordSource();
public IEnumerable DataSource2 { get => DataSource; }
public class DataRecordSource : List //ObservableCollection was SLOW - when populating rows Dictionary cannot be used for SfGrid
{
}
}