0





70
1

I've made it for integer value and by this code and it's work :

 private void ItemsDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {

        DataGridViewTextBoxCell cell =ItemsDataGridView[2, e.RowIndex] as DataGridViewTextBoxCell;

        if (cell != null)
        {
            if (e.ColumnIndex == 2)
            {
                char[] chars = e.FormattedValue.ToString().ToCharArray();

                foreach (char c in chars)
                {
                    if (char.IsDigit(c) == false)
                    {
                        MessageBox.Show("You have to enter digits only");

                        e.Cancel = true;
                        break;
                    }
                }
            }

i want to make another cell just accept double value to prevent user from enter two dots "." to avoid error

Question author Qais-khaled | Source

0


1

Keep it simple!

private void ItemsDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if (e.ColumnIndex == 2)
    {
        double result;

        if (!double.TryParse(e.FormattedValue.ToString(), out result))
        {
            e.Cancel = true;
        }
    }
}

Also, use the ErrorText property instead of MessageBox to indicate user error.

private void ItemsDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if (e.ColumnIndex == 2)
    {
        double result;

        if (!double.TryParse(e.FormattedValue.ToString(), out result))
        {
            e.Cancel = true;
            // Set error message
            ItemsDataGridView.Rows[e.RowIndex].ErrorText = "You have to enter doubles only";
        }
    }
}

private void ItemsDataGridView_CellValidated(object sender, DataGridViewCellEventArgs e)
{
    // Clear error message
    ItemsDataGridView.Rows[e.RowIndex].ErrorText = null;
}
Answer author Alexander-petrov

Ask about this question here!