C# - DevExpress XtraGrid - Master/Detail - Display Format

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

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 = .... } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!