C# - DevExpress XtraGrid - Master/Detail - Display

2019-05-10 05:34发布

问题:

Scenario

  • I have a DevExpress XtraGrid.
  • The data displayed is in a master/detail format, whereby clicking the '+' at the start of the row expands the detail for that master row.
  • I have implemented this by binding the grids datasource to a dictionary of objects that contain their own Dictionary property (to hold the detail).

Problem

  • What I want to do is format the data in specific columns of the detail.
  • However, I cannot get hold of the column, presumably because it is a sub-element of the master row (and therefore does not get checked?)
  • Below are 2 code examples of the implementations I have tried so far that do not work.

Attempted Code Solutions

gridView1.Columns["Price"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                gridView1.Columns["Price"].DisplayFormat.FormatString = "n4";


  private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            GridView View = sender as GridView;
            if (e.Column.FieldName == "Price")
            {
                e.Column.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                e.Column.DisplayFormat.FormatString = "n4";
            }
            }

Help greatly appreciated.

回答1:

To format values in a detail GridView, you should obtain an instance of this object first. Pretty much the standard way of doing this is to handle the Master GridView's MasterRowExpanded event handler. In this event handler you can also set a column's DisplayFormat:

private void gridView1_MasterRowExpanded_1(object sender, CustomMasterRowEventArgs e) {
    GridView master = sender as GridView;
    GridView detail = master.GetDetailView(e.RowHandle, e.RelationIndex) as GridView;
    detail.Columns["SomeColumn"].DisplayFormat = ....
}


标签: c# DevExpress