The requested page is not available for the requested platform. You are viewing the content for Default platform.

How to: Add an Unbound Column to Supply Additional Data

  • 2 min 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 UnitPriceQuantity(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.

exCustomUnboundColumnData

Create an Unbound Column at Design Time

<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

<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" />
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);
    }
}