0

I have a datagridview with 3 columns and 19 rows.first two columns are read-only. The last column has values already set to 0. User has to input integer values in the last one and I need to validate if it is an integer.if not display message "Enter Integer". I came across the cellleave event and i was thinking it might be the right one to use to perform the check but i don't know how to call that event automatically.

To summarise

-- Values in 3rd column of datagridview already set to zero.

--user inputs value in 3rd column.

--if value is not integer display message "Please enter integer"

I am a beginner in c# and would really appreciate any help on that matter.

4

1 回答 1

0
 private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            DataGridViewTextBoxCell cell = dataGridView1[e.ColumnIndex, e.RowIndex] as DataGridViewTextBoxCell;

            if (cell != null)
            {
                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;
                    }
                }
            }
        }

http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/6fc564f9-e154-4be1-8d2c-9988a81e2be7

于 2012-04-21T21:52:12.390 回答