Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-14
8
Medium Priority
?
248 Views
Last Modified: 2012-03-14
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
0
Comment
Question by:peterkiers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Assisted Solution

by:zofcentr
zofcentr earned 400 total points
ID: 37718930
Try something like this:

           
int outputInteger;
            decimal outputDecimal;
            String value = "8,8";
            bool isInteger, isDecimal;
            isInteger = int.TryParse(value, out outputInteger);
            isDecimal = decimal.TryParse(value, out outputDecimal);
            value = "8";
            isInteger = int.TryParse(value, out outputInteger);
            isDecimal = decimal.TryParse(value, out outputDecimal);

Open in new window


Please note that 8 is correct integer but also correct decimal.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 37719097
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
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 400 total points
ID: 37719121
Although above solution seems to fit your requirement, just thought that a more generic version would be more helpful in case you need to check against more types.

Have a look:-

public static bool CheckType<TValue>(string val)
            where TValue : struct
        {
            bool retVal = false;

            switch(Type.GetTypeCode(typeof(TValue)))
            {
                case TypeCode.Double:
                    double valueDouble;
                    retVal= double.TryParse(val, out valueDouble);
                    break;

                case TypeCode.Int32:
                    int valueInt;
                    retVal= int.TryParse(val, out valueInt);
                    break;
            }

            return retVal;
        }

public static void Check()
{
Console.Writeline(CheckType<double>("8.8"));
Console.Writeline(CheckType<int>("8"));
}

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 1

Author Comment

by:peterkiers
ID: 37719178
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
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37719188
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

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1200 total points
ID: 37719208
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.

For that it could be as simple as:

static bool AFunction(string value)
{
    return value.Contains(",") || value.Contains(".");
}

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 37719318
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
0
 
LVL 1

Author Comment

by:peterkiers
ID: 37719343
I was to soon with posting Kaufmed gave me the right solution.

Peter
0

Featured Post

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question