How to determine the current modified datatable whether the value of a column or double type int

How to determine the current modified datatable whether the value of a column or double type int

In doing today met datatable data to verify data datatable column of data type checking, so the record about my method of verification, check if there's an easier way to welcome you all to share, be grateful.

/*
取得改变过的datatable,注意不能把原有的datatable执行AcceptChanges()方法,先执行Copy()新得到一个
当前经过修改后datatable
*/
DataTable dtTemp = ((DataTable)gridControl1.DataSource).Copy();
 dtTemp.AcceptChanges();
//判断datatable是否为空
if (dtTemp.Rows.Count == 0)
{
   if(CommonFunction.ShowMsgBox("检测到复称列表为空,是否直接保存", MessageBoxButtons.YesNo, 0) == DialogResult.No)
   {
      return false;
   }
}
//以下是判断当前经过修改后datatable是否有空值并若为空值则校验不通过,若不为空值再进行数据类型的校验
if (dtTemp.Rows.Count > 0)
{
   foreach (DataRow dr in ((DataTable)gridControl1.DataSource).Rows)
   {
      if (CommonFunction.Trim(Convert.ToString(dr["序号"]))=="")
      {
         CommonFunction.ShowMsgBox("检测到复称列表有未填项");
        return false;
       }
       /*
       通过int.TryParse(string s out Int32 result)进行整数类型的校验
       该方法传入两个参数,第一个是字符串类型,第二个是转化成功后输出的整型变量
       如果转化成功则该方法返回true并输出转化后的结果,该结果可进一步被使用来判断数值范围 
       如果转化失败则该方法返回false,即该datatable的该单元格中的内容不数据类型错误    
       */
      int iRecordSeq;          
      if(int.TryParse(Convert.ToString(dr["序号"]),out iRecordSeq) == false || iRecordSeq < 1)
      {
         CommonFunction.ShowMsgBox("复称列表序号必须为大于0的整数");
         return false;
       }
       if (CommonFunction.Trim(Convert.ToString(dr["桶皮"])) == "")
        {
          CommonFunction.ShowMsgBox("检测到复称列表有未填项");
          return false;
        }
        double dTaerWeight;
        if (double.TryParse(Convert.ToString(dr["桶皮"]), out dTaerWeight) == false || dTaerWeight < 1)
         {
            CommonFunction.ShowMsgBox("复称列表桶皮必须为大于0的数字");
           return false;
         }
         if (CommonFunction.Trim(Convert.ToString(dr["毛重"])) == "")
         {
            CommonFunction.ShowMsgBox("检测到复称列表有未填项");
            return false;
         }
         double dGrossWeight;
         if (double.TryParse(Convert.ToString(dr["毛重"]), out dGrossWeight) == false || dGrossWeight < 1)
         {
            CommonFunction.ShowMsgBox("复称列表毛重必须为大于0的数字");
          return false;
         }
         if (CommonFunction.Trim(Convert.ToString(dr["单位"])) == "")
         {
            CommonFunction.ShowMsgBox("检测到复称列表有未填项");
            return false;
         }                 
     }
}    
//遍历当前修改过后的datatable,看序号这一列是否存在重复的值
foreach (DataRow dr in dtTemp.Rows)
{
  int iRecordSeq=Convert.ToInt32(dr["序号"]);
  if (dtTemp.Select("序号=" + iRecordSeq).Count() > 1)
  {
     CommonFunction.ShowMsgBox("检测到复称列表有重复的序号,请检查无误后操作");
     return false;
   }            
}
return true;

At this point, the check is completed.

Guess you like

Origin www.cnblogs.com/ubantu/p/11326461.html