Search code examples
asp.netdevexpressaspxgridview

How can I set the column properties(DisplayFormatString to be precise) of a aspx(devExpress) grid from code behind?


I have an aspx(devexpress) grid. Using which I generate columns dynamically from code behind.Below is the code from my grid_databinding event.

GridViewDataTextColumn bfield = new GridViewDataTextColumn();
if (TestString.YearSelectedNames.ToString().Length > 4)
{  string colName = string.Empty;
if (iCount % 2 == 0)
  {

   colName = TestString.YearSelectedNames.ToString().Substring(5, 4) + "-" + dtFreezing.Columns[iCount].ColumnName.ToString();
   bfield.HeaderTemplate = new DevxGridViewTemplate(ListItemType.Header, typeof(Label), colName, iCount);
  }
  else
     {
     colName = TestString.YearSelectedNames.ToString().Substring(0, 4) + "-" + dtFreezing.Columns[iCount].ColumnName.ToString().Replace('1', ' ');
     bfield.HeaderTemplate = new DevxGridViewTemplate(ListItemType.Header, typeof(Label), colName, iCount);
     }

}
else
    {
    bfield.HeaderTemplate = new DevxGridViewTemplate(ListItemType.Header, typeof(Label), dtFreezing.Columns[iCount].ColumnName.Trim(), iCount);
    }
    bfield.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    bfield.HeaderStyle.Wrap = DevExpress.Utils.DefaultBoolean.True;
    bfield.Name = dtFreezing.Columns[iCount].ColumnName.Trim();
    bfield.Width = Unit.Pixel(120);
    bfield.VisibleIndex = iCount;
    bfield.DataItemTemplate = new DevxGridViewTemplate(ListItemType.Item, typeof(Label), dtFreezing.Columns[iCount].ColumnName.Trim(), iCount);
    bfield.CellStyle.HorizontalAlign = HorizontalAlign.Right;
    bfield.PropertiesTextEdit.DisplayFormatString = "N2";
    gridViewProductCrop.Columns.Add(bfield);

Here the line of code

bfield.PropertiesTextEdit.DisplayFormatString = "N2";

is where I am trying to set the property of the grids' column to display only two decimals after the decimal point.

This line of code doesn't seem to work in the first place.

I have even tried using "{0:0.00}" and "{0:N2}" but in vain

Possible reason being that I am writing this line of code in the grid's databinding event. But how else can I set the column properties from code behind


Solution

  • Try to change this code

    bfield.PropertiesTextEdit.DisplayFormatString = "N2";
    

    to

    this.PropertiesTextEdit.DisplayFormatString = "N2";
    

    i think this happen coz u loop the object(make a new object) and the properties would be overwrite.

    CMIIW