This is a migrated thread and some comments may be shown as answers.

change NumberDecimalSeparator in grid

8 Answers 615 Views
GridView
This is a migrated thread and some comments may be shown as answers.
moj
Top achievements
Rank 1
moj asked on 14 Jun 2019, 11:53 AM

hello

i want change NumberDecimalSeparator  to grid example below:

120.459->120/459

8 Answers, 1 is accepted

Sort by
0
moj
Top achievements
Rank 1
answered on 14 Jun 2019, 11:57 AM

1- i use format string:"{0:#/000}" but have problem:

0.123  -> /123

2- i need to active ThousandsSeparator

1234.567 - >  1,234.456

0
moj
Top achievements
Rank 1
answered on 14 Jun 2019, 12:01 PM

excuseme

3-if value is 0 i want show 0

4- if value not float number not show any float number 123.000-> 123

0
moj
Top achievements
Rank 1
answered on 18 Jun 2019, 11:29 AM

hi

please help me

0
Nadya | Tech Support Engineer
Telerik team
answered on 18 Jun 2019, 12:22 PM
Hello, Moj,

According to the provided information, it seems that the desired number formats are quite different. Indeed, you can use GridViewDecimalColumn which allows decimal data to be displayed and edited in RadGridView, as you have already found out. To cover all your specific needs you can also use  GridViewMaskBoxColumn and set a mask to the GridViewMaskBoxColumn using its Mask and MaskType properties. 

First, if you need to change decimal separator with "/", you can go to your computer's settings, following this path: "Windows Control Panel | Region | Formats | Additional settings | Numbers" (this is the path in Windows 10 and it may be different for other Windows versions, but the principle is the same) and then change your current decimal symbol with the specified one, e.g. "/". Please, keep in mind that this will affect all your Windows settings.

Another way is to use the CultureInfo class and initialize a new instance of the NumberFormatInfo class to implement your own specific logic. This approach is demonstrated in the following code snippet:

CultureInfo c = new System.Globalization.CultureInfo("en-EN");
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = "/";
c.NumberFormat = nfi;
System.Threading.Thread.CurrentThread.CurrentCulture = c;

Following your specific needs, described below, I can suggest to use the CellFormatting event, which allows the content of data cell to be formatted in a specific way to display. Please refer to the following code snippet which covers your cases. Feel free to modify it in a way which suits your requirements best.



public partial class Form1 : RadForm
{
    public Form1()
    {
        InitializeComponent();
 
        CultureInfo c = new System.Globalization.CultureInfo("en-EN");
        NumberFormatInfo nfi = new NumberFormatInfo();
        nfi.NumberDecimalSeparator = "/";
        c.NumberFormat = nfi;
        System.Threading.Thread.CurrentThread.CurrentCulture = c;
 
        GridViewMaskBoxColumn maskBox = new GridViewMaskBoxColumn();
        maskBox.MaskType = MaskType.Numeric;
        maskBox.HeaderText = "MaskBoxColumn"
        maskBox.TextAlignment = ContentAlignment.BottomRight;
        maskBox.Width = 150;
        maskBox.DataType = typeof(decimal);
        radGridView1.MasterTemplate.Columns.Add(maskBox);
 
         
        this.radGridView1.Rows.Add(123.1234);
        this.radGridView1.Rows.Add(0.1234);
        this.radGridView1.Rows.Add(1234.567);
        this.radGridView1.Rows.Add(0);
        this.radGridView1.Rows.Add(123.000);
 
        this.radGridView1.CellFormatting += RadGridView1_CellFormatting;
    }
 
    decimal parsedValue = 0;
    private void RadGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement.Value != null && decimal.TryParse(e.CellElement.Value.ToString(), out parsedValue))
        {
            if (parsedValue == 0)
            {
                e.CellElement.Text = "0";
            }
            else if (e.CellElement.Value.ToString().StartsWith("0"))
            {
                e.CellElement.Text = e.CellElement.Value.ToString().Substring(1);
            }
            else if (parsedValue > 999)
            {
                e.CellElement.Text = parsedValue.ToString("N");
            }
        }
    }
}

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Nadya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
moj
Top achievements
Rank 1
answered on 22 Jun 2019, 04:40 PM

Hello

Very thanks for reply

I need sort column by numeric but in GridViewMaskBoxColumn sort column by string.

0
moj
Top achievements
Rank 1
answered on 22 Jun 2019, 05:09 PM

Hello

I solve My problem for "/" with following code:

CultureInfo PersianCultureInfo = new System.Globalization.CultureInfo("fa-IR");
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = "/";
PersianCultureInfo.NumberFormat = nfi;
System.Threading.Thread.CurrentThread.CurrentCulture = PersianCultureInfo;
 
this.gridViewDecimalColumn3.DecimalPlaces = 3;
this.gridViewDecimalColumn3.FormatInfo = PersianCultureInfo;
this.gridViewDecimalColumn3.FormatString = "{0:#,0.000}";

 

I have 1 question, how  remove float number if it is zero similar  123/000 -> 123

0
Nadya | Tech Support Engineer
Telerik team
answered on 24 Jun 2019, 12:56 PM
Hello Moj,

To achieve a result with no zeros after the decimal separator you can use the CellFormatting event: 
private void RadGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.Value != null )
    {
        decimal value = (decimal)e.CellElement.Value;
        
        bool isInt = value % 1 == 0;
        if (isInt)
        {
            e.CellElement.Text = ((int)value).ToString();
        }
    }
}

Should you have any other questions, do not hesitate to ask.

Regards,
Nadya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
moj
Top achievements
Rank 1
answered on 25 Jun 2019, 12:33 PM

Hello, Nadya

Very thanks

Tags
GridView
Asked by
moj
Top achievements
Rank 1
Answers by
moj
Top achievements
Rank 1
Nadya | Tech Support Engineer
Telerik team
Share this question
or