2012-04-21 41 views
0

我有一个datagridview有3列和19 rows.first两列是只读的。最后一列的值已经设置为0.用户必须在最后一个输入整数值,我需要验证它是否为整数。如果不显示消息“输入整数”。我遇到了cellleave事件,我在想它可能是正确的用来执行检查,但我不知道如何自动调用该事件。在已经设置为零datagridview的第3列中的值 -Datgridview单元验证

总结

- 用户在第3列中输入值。

--IF值不是整数,显示消息“请输入整数”

我是C#初学者,真的希望就这一问题的任何帮助。

回答

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

+0

谢谢您的回复husnain :) 我发现了一个更简单的方法来检查该列中输入正确的格式。 私人无效gridRegisters_CellValueChanged(对象发件人,DataGridViewCellEventArgs E) { 尝试 { INT NUM = Int32.Parse(gridRegisters.Rows [e.RowIndex] .Cells [e.ColumnIndex] .Value.ToString()); } catch(Exception ex) { MessageBox.Show(ex.Message); } } – 2012-04-23 16:17:23