格式化 DevExpress ASPxGridView 中的列?

发布于 2024-12-10 17:59:30 字数 178 浏览 1 评论 0原文

有谁知道我如何格式化 DevExpress ASPxGridView 中的列。我拥有的是每天从 xml 文件生成的 xml 文件。我想做的是格式化特定值的列,例如,带有测量值的列,如果未填充,我想添加尾随零,即 1.2 到 1.200。我只遇到过在 ASPX 页面中完成的示例,并用代码构建了我的专栏。请帮忙提供最简单的解决方案或属性,谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜爱纠缠 2024-12-17 17:59:30

在您的 .aspx 页面中,您可以执行此操作,将列格式设置为小数点后 0 位的美元金额。

  <dx:GridViewDataTextColumn FieldName="YourFieldName" VisibleIndex="1" Name="Displayame">
      <PropertiesTextEdit DisplayFormatString="${0}" />
  </dx:GridViewDataTextColumn>

在代码后面将 CellEditorInitialize 绑定到自定义事件处理程序,如下所示:

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

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);
}}
忱杏 2024-12-17 17:59:30

无需在活动中这样做。当然这并不完整,但你已经明白了:

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

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}";
  }
}
幽蝶幻影 2024-12-17 17:59:30

在后面的代码中,您可以使用 CustomColumnDisplayText 事件:

protected void Grid_CustomColumnDisplayTextEvent(ASPxGridViewColumnDisplayTextEventArgs e)
{
    if ("ColumnName".Equals(e.Column.FieldName))
    {
        e.DisplayText = someFormatFunction(e.Value);
    }
}

方法文档:
http://documentation.devexpress.com/#AspNet/DevExpressWebASPxGridViewASPxGridView_CustomColumnDisplayTexttopic

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文