When displaying float or double values in PropertyGrid, is there a way to cut off the decimal point to a specific number of digits?

Sunghyun Shim 0 Reputation points
2023-02-24T05:06:02.2933333+00:00

When displaying only, I want to display the decimal points cleanly, and when editing by clicking the mouse, I want to display the entire decimal point so that the original precise value can be modified.

PropertyGrid 에서 float 이나 double 값을 표시할때 소수점을 특정 자리수로 잘라서 표시할 방법이 있을까요?

표시만 할때는 깨끗하게 소수점 잘라서 보여주고 마우스를 클릭하여 편집할때는 원래의 정밀한 값을 수정할 수 있게 전체 소수점을 표시하게 하고 싶습니다.

Developer technologies .NET Other
{count} votes

1 answer

Sort by: Most helpful
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2023-02-26T21:55:30.8766667+00:00

    Consider the following facts:

    • PropertyGrid uses TypeConverter of the property to get the string representation of the value.
    • PropertyGrid uses a TextBox to to edit property values
    • PropertyGrid has a SelectedGridItem property which tells you what is the selected property in the grid

    Now the idea that I used, is:

    • Register a custom TypeConverter for the double properties in your class, to show the formatted text for double values by default.
    • Finding the editor TextBox and when Enter event happens, find the property using SelectedGridItem, and get the value and set the full value as text of the TextBox

    pg

    Here is the implementation that worked for me:

    using System.ComponentModel;
    using System.Globalization;
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.propertyGrid1.SelectedObject = new Test();
            //For .NET projects the field is _gridView
            //For .NET Framework projects the field is gridView
            var gridView = propertyGrid1.GetType()
                .GetField("gridView",
                    System.Reflection.BindingFlags.NonPublic |
                    System.Reflection.BindingFlags.Instance)
                .GetValue(propertyGrid1);
            var edit = gridView.GetType()
                .GetProperty("Edit",
                    System.Reflection.BindingFlags.NonPublic |
                    System.Reflection.BindingFlags.Instance)
                .GetValue(gridView) as TextBox;
            edit.Enter += (obj, args) =>
            {
                var name = propertyGrid1.SelectedGridItem.PropertyDescriptor?.Name;
                if (name == "MyProperty1" || name == "MyProperty2")
                {
                    edit.Text = propertyGrid1.SelectedGridItem.Value.ToString();
                }
            };
        }
    }
    public class Test
    {
        [TypeConverter(typeof(MyDoubleConverter))]
        public double MyProperty1 { get; set; } = 1.12345678;
    
        [TypeConverter(typeof(MyDoubleConverter))]
        public double MyProperty2 { get; set; } = 1.12345678;
    }
    public class MyDoubleConverter : DoubleConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context,
            CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string) && value is double)
            {
                return ((double)value).ToString("N2");
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.