Saturday, November 13, 2010

Validate GridCell with Decimal Value in C#

Validate a Cell only Decimal No is Input , Alphabet should not be  enter in that Cell only no and one point(.) should be enter and Backspace shoul be allowed
To do this you have to make a Method Named validate(char Key) and after write code on keyPress Event

      #region Decimal Validation

        public static bool validate(char Key)
        {
            byte i;
            bool parsed = byte.TryParse(Key.ToString(), out i);
            bool val = true;

            if (parsed)
            {
                val = false;
            }
          
            else if (parsed == false)
            {
                switch (Convert.ToInt32(Key))
                {
                    default:
                        val = true;
                        break;
                    case 8:
                        val = false; // If Backspace, allow char.
                        break;
                    case 46:
                        val = false; // If Delete, allow char.
                        break;
                }
            }
            return val;
        }

        #endregion

This is the method you have to write

Write Code on Key Press Event


void datagrigview1_KeyPress(object sender, KeyPressEventArgs e)
{
#region ValidateDecimal


if (datagrigview1.CurrentCell.OwningColumn.Name == "Quantity")
{
e.Handled = true;
int m = e.KeyChar;

if (e.KeyChar == 46)
{
//if (dGVTrans.CurrentCell.Value != null)
{

int n =datagrigview1 .CurrentCell.EditedFormattedValue.ToString().IndexOf(".");
if (n > 0)
{
return;
}
}
}

e.Handled = validate(e.KeyChar);
}


#endregion

No comments:

Post a Comment

Mat Table Angular

Mat Table in Angular Mat table is used to display data . its a material designed styled data-table . For using Material Table MatTableMo...