How to add in KendoUI MVC Grid a thousand comma separator

浪子不回头ぞ 提交于 2019-12-24 12:51:24

问题


I want to display 1000 like 1,000 in Grid (Must have commas). Initial data type is string. I need thousand comma separator.

I have the same questions as in How to change display format of long variable?. But my data type is string.

In this topic they did not have clear answer for KendoUI MVC Grid for thousand comma separator.

I have KendoUI Grid

@(Html.Kendo().Grid((IEnumerable<MyApp.Models.MyModel>)ViewBag.QualReq)
    .Name("MyGrid")
    .Columns(columns =>
        { 
        columns.Bound(p => p.Actual); 
        })
    .DataSource(d => d
        .Ajax()
    )
)

Model

public class MyModel
{ 
    public virtual string Actual { get; set; } 
}

Controller

    [OutputCache(Duration = 60)]
    public ActionResult MyActionMethod(int RankId = 0)
    { 
        . . .                  
        ViewBag.QualReq = qualificationRequirements.Where(x => x.RankID == RankId).ToList();
        . . .

I have not found any information for MVC Grid in Number Formatting topic. http://docs.telerik.com/kendo-ui/framework/globalization/numberformatting


回答1:


The number formatting document you link to is showing how you would do this in JavaScript. Part of the problem you're running in to is that you are using a string property to store numeric data. You need to change your Actual property to be an int (or some other numeric type) instead of a string and then add a DisplayFormat attribute [DisplayFormat(DataFormatString = "{0:N2}")].

The thing to keep in mind though, is that without setting the culture, it's going to use whatever the thread's current culture is (presumably en-US since you want to use a comma as the separator) to determine what the separator character should be. If you need it to be something different (for a different culture), you're going to need to set the culture explicitly.




回答2:


You can do this on the grid itself with ClientTemplate.

@(Html.Kendo().Grid((IEnumerable<MyApp.Models.MyModel>)ViewBag.QualReq)
    .Name("MyGrid")
    .Columns(columns =>
        { 
            columns.Bound(p => p.Actual).ClientTemplate("#= kendo.format('{0:N0}', parseInt(Actual)) #"); 
        })
    .DataSource(d => d
        .Ajax()
    )
)

If this field sometimes contains non-numeric data, you may want to include some logic to account for those cases.



来源:https://stackoverflow.com/questions/35949325/how-to-add-in-kendoui-mvc-grid-a-thousand-comma-separator

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