Back to Devexpress

How to: Add an Unbound Column to Supply Additional Data

aspnet-3770-components-grid-view-examples-how-to-add-an-unbound-column-to-supply-additional-data.md

latest5.4 KB
Original Source

How to: Add an Unbound Column to Supply Additional Data

  • Nov 11, 2021
  • 2 minutes to read

The ASPxGridView control is bound to a data source that contains UnitPrice , Quantity , and Discount fields. There is no field for the total sum – you can use UnitPrice * Quantity * (1-Discount) to calculate it.

The examples below show how to add an unbound column to the ASPxGridView control to display the total sum of an order.

Note

For unbound columns, you should specify unique field names (the GridViewDataColumn.FieldName property) that do not match any field in the underlying data source.

The image below shows the result.

Create an Unbound Column at Design Time

aspx
<dx:ASPxGridView ID="Grid" ClientInstanceName="Grid" runat="server" AutoGenerateColumns="false"
        DataSourceID="ProductsDataSource" KeyFieldName="OrderID" >
        <SettingsPager PageSize="5" />
        <Columns>
            <dx:GridViewDataTextColumn FieldName="ProductName" />
            <dx:GridViewDataSpinEditColumn FieldName="UnitPrice" >
                <PropertiesSpinEdit DisplayFormatString="c" DecimalPlaces="2" />
            </dx:GridViewDataSpinEditColumn>
            <dx:GridViewDataSpinEditColumn FieldName="Quantity" >
            </dx:GridViewDataSpinEditColumn>
            <dx:GridViewDataSpinEditColumn FieldName="Discount" >
                <PropertiesSpinEdit DisplayFormatString="p0"></PropertiesSpinEdit>
            </dx:GridViewDataSpinEditColumn>
            <dx:GridViewDataTextColumn FieldName="Total" UnboundType="Decimal" UnboundExpression="UnitPrice*Quantity*(1-Discount)">
                <PropertiesTextEdit DisplayFormatString="c2" />
            </dx:GridViewDataTextColumn>
        </Columns>
</dx:ASPxGridView>
<ef:EntityDataSource runat="server" ID="ProductsDataSource" ContextTypeName="DevExpress.Web.Demos.NorthwindContext" EntitySetName="Invoices" />

Create an Unbound Column at Run Time

aspx
<dx:ASPxGridView ID="Grid" ClientInstanceName="Grid" runat="server" AutoGenerateColumns="false"
        DataSourceID="ProductsDataSource" KeyFieldName="OrderID" OnInit="Grid_Init" OnCustomUnboundColumnData="Grid_CustomUnboundColumnData">
        <SettingsPager PageSize="5" />
        <Columns>
            <dx:GridViewDataTextColumn FieldName="ProductName" />
            <dx:GridViewDataSpinEditColumn FieldName="UnitPrice" >
                <PropertiesSpinEdit DisplayFormatString="c" DecimalPlaces="2" />
            </dx:GridViewDataSpinEditColumn>
            <dx:GridViewDataSpinEditColumn FieldName="Quantity" >
            </dx:GridViewDataSpinEditColumn>
            <dx:GridViewDataSpinEditColumn FieldName="Discount" >
                <PropertiesSpinEdit DisplayFormatString="p0"></PropertiesSpinEdit>
            </dx:GridViewDataSpinEditColumn>
        </Columns>
</dx:ASPxGridView>
<ef:EntityDataSource runat="server" ID="ProductsDataSource" ContextTypeName="DevExpress.Web.Demos.NorthwindContext" EntitySetName="Invoices" />
csharp
using DevExpress.Web.ASPxGridView;

protected void Grid_Init(object sender, EventArgs e) {
    // Creates a column, customizes its settings, and appends it to the Columns collection.
    GridViewDataTextColumn colTotal = new GridViewDataTextColumn();
    colTotal.FieldName = "Total";
    colTotal.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
    colTotal.VisibleIndex = Grid.VisibleColumns.Count;
    colTotal.PropertiesTextEdit.DisplayFormatString = "c2";
    Grid.Columns.Add(colTotal);
}
// Populates the unbound column.
protected void Grid_CustomUnboundColumnData(object sender,
    ASPxGridViewColumnDataEventArgs e) {
    if (e.Column.FieldName == "Total") {
        decimal unitPrice = Convert.ToDecimal(e.GetListSourceFieldValue("UnitPrice"));
        int quantity = Convert.ToInt32(e.GetListSourceFieldValue("Quantity"));
        decimal discount = Convert.ToDecimal(e.GetListSourceFieldValue("Discount"));
        e.Value = unitPrice * quantity * (1 - discount);
    }
}
vb
Imports DevExpress.Web.ASPxGridView

Protected Sub Grid_Init(ByVal sender As Object, ByVal e As System.EventArgs)_
    Handles Grid.Init
    ' Creates a column, customizes its settings, and appends it to the Columns collection.
    Dim colTotal As GridViewDataTextColumn = New GridViewDataTextColumn()
    colTotal.FieldName = "Total"
    colTotal.UnboundType = DevExpress.Data.UnboundColumnType.Decimal
    colTotal.VisibleIndex = Grid.VisibleColumns.Count
    colTotal.PropertiesTextEdit.DisplayFormatString = "c2"
    Grid.Columns.Add(colTotal)
End Sub

' Populates the unbound column.
Protected Sub Grid_CustomUnboundColumnData(ByVal sender As Object,_
   ByVal e As DevExpress.Web.ASPxGridViewColumnDataEventArgs)_
    Handles Grid.CustomUnboundColumnData
    If e.Column.FieldName = "Total" Then
        Dim unitPrice As Decimal = Convert.ToDecimal(e.GetListSourceFieldValue("UnitPrice"))
        Dim quantity As Integer = Convert.ToInt32(e.GetListSourceFieldValue("Quantity"))
        Dim discount As Decimal = Convert.ToDecimal(e.GetListSourceFieldValue("Discount"))
        e.Value = unitPrice * quantity * (1 - discount)
    End If
End Sub