Felicio Felicio
Felicio Felicio

Reputation: 1

Winforms Gridview Calculation

I have 2 columns so when the first column cell value contains "ok" then a calculation will be done on the second column. If it does not contain "ok" then I don't need any calculation.

The code works but does not depend on "ok", it doesn't matter if the cell contains "ok" or whatever else.

private void calculate()
{
    if (gridView3.Columns["Status"] == gridView3.GetFocusedRowCellValue("ok"))
    {
        gridColumn3.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
        gridColumn3.UnboundExpression = "DateDiffDay([StartDate], LocalDateTimeToday())";
    }
    else
    {   

    }
}

Upvotes: 0

Views: 137

Answers (2)

DmitryG
DmitryG

Reputation: 17850

You should either build the correct unbound expression:

gridColumn3.UnboundExpression = "Iif([Status]=1, DateDiffDay([StartDate], LocalDateTimeToday()), 0)";

or handle the GridView.CustomUnboundColumnData event as follows:

gridColumn3.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
gridView1.CustomUnboundColumnData += gridView1_CustomUnboundColumnData;
// ...
void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) {
    ColumnView view = ((ColumnView)sender);
    if(e.IsGetData && e.Column == gridColumn3) 
        e.Value = DateTime.Now.Day - ((EventObj)e.Row).StartDate.Day;
}
// ...
public enum Status { 
    Unknown, Ok 
}
public class EventObj {
    public Status Status { get; set; }
    public DateTime StartDate { get; set; }
}

Upvotes: 1

Svetlana
Svetlana

Reputation: 421

Setting the UnboundExpression property influences a whole unbound column. If you need to calculate values only for particular rows, it is necessary to leave the UnboundExpression property empty and handle the GridView.CustomUnboundColumnData event. This event is raised for each row allowing you to calculate values for the required rows.

Upvotes: 1

Related Questions