Formatting columns in the DevExpress ASPxGridView?

21,472

Solution 1

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);
}}

Solution 2

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}";
  }
}

Solution 3

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

Share:
21,472
Donald N. Mafa
Author by

Donald N. Mafa

Highly self-motivated, experienced Software Architect/ Full Stack Developer, with impeccable work ethic &amp; a calm disposition Hard working, team player &amp; geared to work under pressure. Over 15 years’ experience in the technology realm

Updated on December 19, 2020

Comments

  • Donald N. Mafa
    Donald N. Mafa over 3 years

    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.