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

こ雲淡風輕ζ 提交于 2020-01-07 03:06:57

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/34509399/how-can-i-set-the-column-propertiesdisplayformatstring-to-be-precise-of-a-aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!