dataGridView里面怎么对某一列上的输入做校验?这列是数字的,如果输入字母什么的就不理会哈

解决方案 »

  1.   

    private void dgScore_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
            {
                // Validate the ColumnIndex == 6 ,7,8 entry  
                
                if (e.ColumnIndex == 6 || e.ColumnIndex ==7 || e.ColumnIndex == 8)
                {
                    try
                    {
                        if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
                        {
                        }
                        else
                        {
                            decimal val = decimal.Parse(e.FormattedValue.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        dgScore.Rows[e.RowIndex].ErrorText = "必须输入数字";
                        MessageBox.Show("请输入数字 "); 
                        e.Cancel  =  true;
                    }               
                }
            }