Reputation: 5283
Does anyone know how I could format columns in the DevExpress ASPxGridView. What I have is a xml file that is generated daily from an xml file. What I would like to do is format columns for specific values, for example, a column with measurements, I want to add trailing zeros if these are not filled, i.e. 1.2 to 1.200. I have only come across examples done in the ASPX page and have built my columns in code. Please assist with the simplest solutions or property, thanks.
Upvotes: 2
Views: 22018
Reputation: 3347
In code behind you can use CustomColumnDisplayText
event:
protected void Grid_CustomColumnDisplayTextEvent(ASPxGridViewColumnDisplayTextEventArgs e)
{
if ("ColumnName".Equals(e.Column.FieldName))
{
e.DisplayText = someFormatFunction(e.Value);
}
}
Method documentation: http://documentation.devexpress.com/#AspNet/DevExpressWebASPxGridViewASPxGridView_CustomColumnDisplayTexttopic
Upvotes: 1
Reputation: 4058
No need to do it in an event. This is not complete of course but you get the idea:
void doStuff(){
theGridView.DataSource = getDataSource();
theGridView.DataBind();
foreach(GridViewColumn gvc in theGridView.Columns)
{
String strSomeParamter = "";
if(gvc.Name.Contains("$")
strSomeParameter = "currency";
(gvc as GridViewDataTextColumn).PropertiesTextEdit.DisplayFormatString = getTextFormatStringBasedOnSomeParameter(strSomeParamter);
}
}
String getTextFormatStringBasedOnSomeParameter(String someParam){
switch(someParam)
{
default:
return "";
case "currency":
return "{0:c2}";
case "percent":
return "{0:p2}";
}
}
Upvotes: 3
Reputation: 7449
In your .aspx page you can do this to format your column to dollar amount with 0 decimal place
<dx:GridViewDataTextColumn FieldName="YourFieldName" VisibleIndex="1" Name="Displayame">
<PropertiesTextEdit DisplayFormatString="${0}" />
</dx:GridViewDataTextColumn>
In Code behind bind CellEditorInitialize to a custome event handler something like:
ASPxGridViewData.CellEditorInitialize+=new DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventHandler(ASPxGridViewData_CellEditorInitialize);
protected void ASPxGridViewData_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e){
if (e.Column.FieldName == "YourFieldName") {
e.Editor.Value = string.Format("${0}", e.Value);
}}
Upvotes: 5