- Populating data for the unbound column
- Using QueryUnboundColumnValue event
Contact Support
Unbound column in MAUI DataGrid (SfDataGrid)
The data grid allows adding additional columns that are not bound with data objects from the underlying data source.The unbound column can be added using the SfDataGrid.DataGridUnboundColumn class.
<syncfusion:SfDataGrid
x:Name="datagrid"
ColumnWidthMode="Fill"
AutoGenerateColumnsMode="None"
ItemsSource="{Binding OrderInfoCollection}">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridUnboundColumn
MappingName="DiscountPrice"
HeaderText="SUM"
Expression="Price1+Price2"
Format="C" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
SfDataGrid dataGrid = new SfDataGrid();
OrderInforRepo orderInforRepo = new OrderInforRepo();
dataGrid.ItemsSource = orderInforRepo;
DataGridUnboundColumn DiscountColumn = new DataGridUnboundColumn()
{
MappingName = "DiscountPrice",
HeaderText = "SUM",
Expression = "Price1+Price2",
Format = "C"
};
this.datagrid.Columns.Add(DiscountColumn);
NOTE
It is mandatory to specify the
DataGridColumn.MappingName
forSfDataGrid.DataGridUnboundColumn
with some name to identify the column. It is not necessary to define name of the field in the data object.
Populating data for the unbound column
Data for the unbound column can be configured by setting the Expression
property.
Using Expression
The arithmetic or logic expression can be specified by using the expression property to compute the display value. By default, DataGridUnboundColumn
evaluates the expression with casing. The casing will be disabled while evaluating the expression by setting the CaseSensitive property to false
List of supported arithmetic and logical operations are as follows:
Arithmetic operations | Operator |
---|---|
Add | + |
Subtract | - |
Multiply | * |
Divide | / |
Power | ^ |
Mod | % |
Greater Than | < |
Less than | > |
Equal | = |
GreaterThanOrEqual | >= |
LessThanOrEqual | <= |
Logical operations | Operators |
---|---|
AND | (char)135 |
OR | (char)136 |
NOT | (char)137 |
<syncfusion:SfDataGrid x:Name="datagrid"
ColumnWidthMode="Fill"
AutoGenerateColumnsMode="None"
ItemsSource="{Binding OrderInfoCollection1}">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridUnboundColumn
MappingName="DiscountPrice"
Expression="Price1+Price2"
HeaderText="SUM"/>
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
Using QueryUnboundColumnValue event
The QueryUnboundColumnValue event is fired when value for the unbound column is quired. It provides information about the cell that triggered this event. So, you can set the desired value for the grid cells of the unbound column. This event is triggered with the DataGridUnboundEventArgs
.
The DataGridUnboundColumnEventArgs provides the following properties:
Column: Gets DataGridColumn of the cell that triggers this event.
OriginalSender: Gets the data grid raising event.
Record: Gets the underlying row data
UnboundAction: Defines the action for triggering event.
Value: Gets or Sets the value for DataGridUnboundColumn cell based on unboundAction.
Populate the data for the unbound column by handling QueryUnboundColumnValue
event which allows customizing the value of the DataGridUnboundColumn
. DataGridUnboundColumnEventArgs
exposes Value
property, by which you can set the value for the grid cells of the unbound column based on the UnboundAction
.
Refer to the following code example in which data for the unbound column is populated by handling the QueryUnboundColumnValue
event:
datagrid.QueryUnboundColumnValue += DataGrid_QueryUnboundColumnValue;
private void DataGrid_QueryUnboundColumnValue(object? sender, DataGridUnboundColumnEventArgs e)
{
if (e.UnboundAction == DataGridUnboundActions.QueryData)
{
var Price1 = Convert.ToInt16(e.Record.GetType().GetProperty("Price1").GetValue(e.Record));
var Price2 = Convert.ToInt16(e.Record.GetType().GetProperty("Price2").GetValue(e.Record));
e.Value = Price1 - Price2;
}
}