2

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.

0

3 Answers 3

5

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);
}}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Emmanuel, I have tried this method, i.e. the one in the code behind and it does not hit this code for some reason. Is there something I need to set true or something.
3

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.