Avatar of Peter Kiers
Peter Kiers
Flag for Netherlands asked on

Determine if user enters a integer or a double value into a cell of a grid.

Dear Experts,

I use a grid from DevExpress on my form. What I would like is a methode to check
if a user enters an integer-value (f.e: 8) or a double value (f.e: 8,8) into the cells
of column called colValue (fieldname = "Value").

I wrote it in psuedo-code:

if the column fieldname = Value
if (IsMGDL == false || value is a interger-value)
then devide (/) the value with 18
if (IsMGDL == true|| value is a double-value)
then x the value  with 18

I have begun to do this myself and this is al I got:

private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "Value")
            {
                if (Properties.Settings.Default.IsMGDL==false)
                {
                    e.DisplayText = (value / 18).ToString();
                }
                else if (Properties.Settings.Default.IsMGDL==true) 
                {
                    e.DisplayText = (value * 18).ToString();
                }
            }
        }
}

Open in new window


Who can help me?

Greetings,

Peter Kiers
.NET Programming

Avatar of undefined
Last Comment
Peter Kiers

8/22/2022 - Mon
SOLUTION
zofcentr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Peter Kiers

ASKER
I'm not exactly sure what to do with this code.

And another approach would be to check if a value has a comma or a period in it.

Peter
SOLUTION
BuggyCoder

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Peter Kiers

ASKER
I have thought about this and came up with another solution:

I want a function that checks if a value entered in a cell has a comma (,) or
period (.) in it. If not then give false else true.

And use this function in my gridView1_CustomColumnDisplayText methode.

Peter
kaufmed

You could write your own method for such a task. For example, you could create an enum to represent the appropriate "types" and parse the value from within the method. Here is a rudimentary example. It may work for your environment, but you may need to tweak it as well.

using System;
using System.Globalization;
using System.Threading;

namespace _27631360
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(GetValueType("8").ToString());
            Console.WriteLine(GetValueType("8.00").ToString());
            Console.WriteLine(GetValueType("   8").ToString());
            Console.WriteLine(GetValueType("8   ").ToString());
            Console.WriteLine(GetValueType("  8  ").ToString());
            Console.WriteLine(GetValueType(" .8").ToString());
            Console.WriteLine(GetValueType("8,000").ToString());
            Console.WriteLine(GetValueType("8,000.00").ToString());
            Console.WriteLine(GetValueType("8a.00").ToString());
            Console.ReadKey();
        }

        static NumericType GetValueType(string value)
        {
            NumericType result = NumericType.Integer;
            NumberFormatInfo numFormat = Thread.CurrentThread.CurrentCulture.NumberFormat;

            value = value.Trim();

            for (int i = 0; i < value.Length; i++)
            {
                if (value[i] == numFormat.CurrencyDecimalSeparator[0])
                {
                    if (result == NumericType.Double)
                    {
                        return NumericType.NonNumeric;
                    }
                    else
                    {
                        result = NumericType.Double;
                    }
                }
                else if (value[i] == numFormat.CurrencyGroupSeparator[0])
                {
                    continue;
                }
                else if (!char.IsNumber(value[i]))
                {
                    return NumericType.NonNumeric;
                }
            }

            return result;
        }
    }

    public enum NumericType
    {
        NonNumeric,
        Integer,
        Double
    }
}

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Peter Kiers

ASKER
I have this:

        public static bool CheckType(string strToTransform)
        {
            decimal result;
            if (!decimal.TryParse(strToTransform as string, out result))
            {
               // return false
            }

            if (strToTransform.Contains("."))
            {
                //return true
            }
            if (strToTransform.Contains(","))
            {
               //return true
            }
            return ????
        }

Can someone help me with this.

Peter
Peter Kiers

ASKER
I was to soon with posting Kaufmed gave me the right solution.

Peter