Forum Discussion

Sweez's avatar
Sweez
Contributor
2 days ago
Solved

Dynamic Grids

I am looking for anyone with some experience with Dynamic Grids or are aware of any good documentation.  There is very little in the OS documention beyond how to create the component and shell of a s...
  • Kit's avatar
    2 days ago

    It sounds like you have a grid component attached to a dynamic grid class, but you aren't sure what to do next. There are two functions that you will probably want to connect inside your dynamic grid class. The first one defines and loads your grid. In C#, it would look something like:

    public XFDynamicGridGetDataResult GetDynamicGridData(SessionInfo si, BRGlobals brGlobals, DashboardWorkspace workspace, DashboardDynamicGridArgs args)
    {
    	//Define the columns for the grid, similar to the grid view component's column format in the user interface
    	List<XFDynamicGridColumnDefinition> columnDefinitions =
    	[
    		new XFDynamicGridColumnDefinition()
    		{
    			ColumnName = "RowId",
    			IsVisible = TriStateBool.FalseValue,
    		},
    		new XFDynamicGridColumnDefinition()
    		{
    			ColumnName = "DataColumn",
    			IsVisible = TriStateBool.TrueValue,
    			Description = "My Label",
    			AllowUpdates = TriStateBool.TrueValue,
    		}
    	];
    
    	//Get the data you want to put in the grid
    	var gridData = _gridDataAdapter.GetAllRows();
    
    	// Create the XFTable
    	XFDataTable xfTable = new(si, gridData, [], 1000);
    
    	//Send the result to the interface component
    	var taskResult = new XFDynamicGridGetDataResult
    	{
    		DataTable = xfTable,
    		ColumnDefinitions = columnDefinitions,
    		AccessLevel = DataAccessLevel.AllAccess
    	};
    	return taskResult;
    }
    

    The second function you likely want is the save function, which determines what happens to the changes you made to the data in the grid in the user interface when you want to apply those changes back to the source data. In C#, it would look something like:

    public XFDynamicGridSaveDataResult SaveDynamicGridData(SessionInfo si, BRGlobals brGlobals, DashboardWorkspace workspace, DashboardDynamicGridArgs args)
    {
    	//Check if there are changes
    	DashboardDynamicGridSaveDataArgs saveDataArgs = args.SaveDataArgs;
    	if (saveDataArgs == null)
    	{
    		return null;
    	}
    
    	// Get the edited rows
    	List<XFEditedDataRow> editedDataRows = saveDataArgs.EditedDataRows;
    	if (editedDataRows == null || editedDataRows.Count == 0)
    	{
    		return null;
    	}
    
    	//You can use a message string to return a message when you are done
    	var message = string.Empty;
    
    	//Apply the logic for changes
    	foreach (var editedDataRow in editedDataRows)
    	{
    		//This is an example of how to differeniate the different types of changes
    		//You might also use if-then-else or other types of logic
    		//This switch statement will check the value of each row's InsertUpdateOrDelete property
    		//It will then find the matching case and continue. The break statement tells it to
    		//exit the switch.
    		switch (editedDataRow.InsertUpdateOrDelete)
    		{
    			case DbInsUpdateDelType.Delete:
    				//Do delete logic
    				break;
    			case DbInsUpdateDelType.Update:
    				//Do update logic
    				break;
    			case DbInsUpdateDelType.Insert:
    				//Do insert logic
    				break;
    		
    		}
    	}
    
    	// Return status - this example always returns success and will show a message if you set one
    	// You can write logic to return how many rows were deleted, updated, or inserted, or include
    	// error handling or whatever you like
    	XFDynamicGridSaveDataResult result = new()
    	{
    		SaveDataTaskResult = new XFDynamicGridSaveDataTaskResult()
    		{
    			IsOK = true,
    			ShowMessageBox = message != string.Empty,
    			Message = message
    		}
    	};
    
    	return result;
    }

    These code samples will need to be tailored to your environment, however hopefully it gives you the starting point you are looking for.