Ticket Q416750
Visible to All Users

ASPxGridView - How to add a calculated column

created 13 years ago

We have a ASPxGridView control that has (among others) two columns "A" and "B". They are decimal numbers such as A=8.869 and B=3.372.

We wish to include another column "A-B" that is the calculation of A-B. So in this case 8.869 - 3.372=5.479. So A-B would be 5.479.

We'd like to calculate this for each the rows in the GridView. How to do that?

<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" KeyFieldName="device_id">
<Columns>
<dx:GridViewCommandColumn VisibleIndex="0">
<ClearFilterButton Visible="True">
</ClearFilterButton>
</dx:GridViewCommandColumn>
<dx:GridViewDataTextColumn FieldName="device_id" ReadOnly="True"
VisibleIndex="1">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="device_meter_id" VisibleIndex="2">
</dx:GridViewDataTextColumn>

<dx:GridViewDataTextColumn FieldName="device_A" VisibleIndex="6" Caption="A">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="device_B" VisibleIndex="7" Caption="B">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn VisibleIndex="8" Caption="A-B">
</dx:GridViewDataTextColumn>
</Columns>
<Settings ShowFilterRow="True" ShowGroupPanel="True" />
</dx:ASPxGridView>

Comments (2)

    Excellent! Works great. Took a bit of testing to see what to put in the GetListSourceFieldValue. It is the name of the column in the sql table, as you know (but I didn't).
    For others, if necessary, here's the code that worked:
    protected void grid_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
            {
                if (e.Column.FieldName == "myFieldInGridView")
                {
                    decimal a = Convert.ToDecimal(e.GetListSourceFieldValue("sql_database_column_5"));
                    decimal b = Convert.ToDecimal(e.GetListSourceFieldValue("sql_database_column_6"));
                    e.Value = a - b;
                }
            }
    and this in the gridview control:
    OnCustomUnboundColumnData="grid_CustomUnboundColumnData"

    DevExpress Support Team 13 years ago

      Hello John,
      Thank you for informing us that the problem has been resolved. I have approved the Answer of Isaac Khazi user.
      You can mark the answer as a Solution and close the question:
      http://www.devexpress.com/Support/Center/FAQ

      Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

      Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.