Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Datagridview bug

$
0
0

I have a datagridview in which I go through a keypress event and it checks if it's not a decimal then a message box comes up saying "Only numeric or decimal characters allowed." I noticed a strange bug. When I press a non numeric key on the first row then it brings up this message box one time when I go to the next row and press another non numeric key in the next row then it brings up the message box 2 times for the next row 3 times and then 4 times....

This is the code 

    private void dgvDriverSheet_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (dgvDriverSheet.CurrentCell.OwningColumn.Name == "Payment Amount" || dgvDriverSheet.CurrentCell.OwningColumn.Name == "Postdated")
            {
                Validation.IsDecimal(sender, e);
            }
        }


        public static void IsDecimal(object sender, KeyPressEventArgs e)
        {
            //if (!char.IsControl(e.KeyChar)
            //   && !char.IsDigit(e.KeyChar)
            //   && e.KeyChar != '.')
            //{
            //    e.Handled = true;
            //}
            //if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            //{
            //    e.Handled = true;
            //}

            string Value = (sender as TextBox).Text;
            Value = Value.Remove((sender as TextBox).SelectionStart, (sender as TextBox).SelectionLength);
            Value = Value.Insert((sender as TextBox).SelectionStart, e.KeyChar.ToString());
            if (Convert.ToBoolean(Value.LastIndexOf("-") > 0) ||
                  !(char.IsControl(e.KeyChar) ||
                    char.IsDigit(e.KeyChar) ||
                  (e.KeyChar == '.' && !((sender as TextBox).Text.Contains(".")) ||
                   e.KeyChar == '.' && (sender as TextBox).SelectedText.Contains("."))))
            {
                MessageBox.Show("Only numeric characters or decimals may be entered.", "Decimals Only", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Handled = true;
            }
        }


Debra has a question


Viewing all articles
Browse latest Browse all 2535

Trending Articles