Hi,
Can a ListView customise the DisplayFormat of an individual cell based on a property value associated with that row?
My application is multi-currency, the currency is set on a per-object basis and is only concerned with the format used to display the currency, there is no conversion or complicated logic involved, it's simply whether it should be displayed with a $, £, € etc. Each object has a Currency property which allows you to select from multiple currency formats. I've extended the ASPxDecimalPropertyEditor to first read the Currency property value from the object and then set the appropriate DisplayFormat. This works perfectly for DetailViews, but not in ListViews.
How do I get a ListView to check the Currency property and set the DisplayFormat on a row by row basis?
Is this possible with a PropertyEditor or will I need to create a custom ListEditor or something?
Thanks,
Paul
Multi-currency DisplayFormats in a ListView
Answers
Hello Paul,
Thank you for contacting us. To accomplish your task it is possible to either create a custom ASPxDecimalPropertyEditor (I suggest you check out the How to show a hyper link (URL, email, etc.) for a business class property example for a an example of a custom PropertyEditor that works for both ListView and DetailView) or implement a ViewController. Below is the latter approach:
C#[DefaultClassOptions]
public class TestClass : BaseObject {
public static string GetFormatString(FlaggedEnum curency) {
switch (curency) {
case FlaggedEnum.EU: return "{0:N}";
case FlaggedEnum.RU: return "{0:0.00}p";
case FlaggedEnum.US: return "{0:0.00}$";
default: return "{0:0.00}";
}
}
public TestClass(Session session) : base(session) { }
public decimal Cost { get; set; }
public FlaggedEnum Curency { get; set; }
}
[Flags]
public enum FlaggedEnum {
RU = 1,
US = 2,
EU = 3
}
Web Forms:
C#public class MyControllerWeb : ViewController<ListView> {
public MyControllerWeb() {
this.TargetObjectType = typeof(TestClass);
}
protected override void OnActivated() {
View.ControlsCreated += new EventHandler(View_ControlsCreated);
base.OnActivated();
}
void View_ControlsCreated(object sender, EventArgs e) {
ASPxGridListEditor editor = View.Editor as ASPxGridListEditor;
if (editor != null) {
((ASPxGridView)(editor.Control)).HtmlDataCellPrepared += new ASPxGridViewTableDataCellEventHandler(MyControllerWeb_HtmlDataCellPrepared);
}
}
void MyControllerWeb_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) {
if (e.DataColumn.FieldName == "Cost") {
TestClass rowObj = (TestClass)(sender as ASPxGridView).GetRow(e.VisibleIndex);
e.Cell.Text = string.Format(TestClass.GetFormatString(rowObj.Curency), rowObj.Cost);
}
}
}
Windows Forms:
C#public class MyController : ViewController<ListView> {
public MyController() {
this.TargetObjectType = typeof(TestClass);
}
protected override void OnActivated() {
View.ControlsCreated += new EventHandler(View_ControlsCreated);
base.OnActivated();
}
void View_ControlsCreated(object sender, EventArgs e) {
GridListEditor editor = View.Editor as GridListEditor;
if (editor != null) {
editor.GridView.CustomDrawCell += new DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(GridView_CustomDrawCell);
}
}
void GridView_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) {
if (e.Column.FieldName == "Cost") {
TestClass rowObj = (TestClass)DevExpress.ExpressApp.Win.Core.XtraGridUtils.GetRow(View.CollectionSource, sender as DevExpress.XtraGrid.Views.Grid.GridView, e.RowHandle);
e.DisplayText = string.Format(TestClass.GetFormatString(rowObj.Curency), rowObj.Cost);
}
}
}
We hope you find this information helpful.
Thanks,
Dennis
Hello Paul,
Thank you for the feedback. I am glad to hear that you have solved your problem.
If you want your comments about our support services to be shown on the http://www.devexpress.com/Home/Comments.xml page, feel free to drop a few lines at management@devexpress.com.
Your input is greatly appreciated!
Thanks,
Dennis
Hello Paul
Could you provide a sample property editor that you have implemented for multicurrency requirement. I have a same requirement and it would help me. Thank you.
I can see that you have already implemented this Property Editor - Custom Decimal Property Editor not invoked when Currency value is changed. I have answered you regarding the IDependentPropertyEditor interface there. Please feel free to write a comment to the T352836 ticket if you need further assistance.