DataGridView 校验

Posted on 2013-05-07 16:21  水晶石头  阅读(360)  评论(0)    收藏  举报

一、得到单元格数据

public static string GetDgvCellText(DataGridViewCell dgvtc)
        {
            if (dgvtc.Value != null)
            {
                if(!string.IsNullOrEmpty(dgvtc.Value.ToString()))
                {
                    return dgvtc.Value.ToString().Trim().Replace('\n','|').Replace(',','@').Replace("'","''");
                }
                else
                {
                    return string.Empty;
                }
            }
            else
            {
                return string.Empty;
            }

 

二、校验单元格为非负数

//验证数字
 public static bool CheckProdModel(ComponentFactory.Krypton.Toolkit.KryptonDataGridView dgv_excelata, int colnum)
{           
   for (int j = 0; j < dgv_excelata.Rows.Count; j++)
    {
    for (int i = colnum; i < dgv_excelata.Columns.Count; i++)
    {
        string strtemp = GetDgvCellText(dgv_excelata.Rows[j].Cells[i]);
        DataGridViewTextBoxImpCell dgvic = (DataGridViewTextBoxImpCell)dgv_excelata.Rows[j].Cells[i];
        if (!string.IsNullOrEmpty(strtemp))
        {
        if (!DoUtility.CheckNumber(strtemp))
        {
            dgvic.ErrorMsg = "数量必须为非负数!";
            if (blchkflag)
            blchkflag = false;
        }
        }
        else
        {
        dgvic.ErrorMsg = "数量不能为空!";
        if (blchkflag)
            blchkflag = false;
        }
    }
    }
}

三、校验不存在空行

View Code
private static bool CheckNullRow(ComponentFactory.Krypton.Toolkit.KryptonDataGridView dgv_excel, int rownum, int colnum)
        {
            int n=0;
            int dgvrownum = dgv_excel.Rows.Count - 1;
            if (rownum < 0)
                return true;
            for (int i = 0; i < colnum; i++)
            {
                if (dgv_excel.Rows[dgvrownum].Cells[i].Value == null || string.IsNullOrEmpty(dgv_excel.Rows[dgvrownum].Cells[i].Value.ToString()))
                    n++;
            }
            if (n == colnum)
            {
                return false;
            }
            else
            {
                return true;
            }

        }

四、检验不存在单元格为空

View Code
 1 public static bool CheckCellNoneValue(ComponentFactory.Krypton.Toolkit.KryptonDataGridView dgv_excel,int[] intcols)
 2         {
 3             bool blchflag = true;
 4             string strrecord = string.Empty;
 5             for (int j = 0; j < dgv_excel.Rows.Count; j++)
 6             {
 7                 for (int i = 0; i < intcols.Length; i++)
 8                 {
 9                     string strtemp = DoImpBase.GetDgvCellText(dgv_excel.Rows[j].Cells[intcols[i]]);
10                     if (string.IsNullOrEmpty(strtemp))
11                     {
12                         DataGridViewTextBoxImpCell dgvic = (DataGridViewTextBoxImpCell)dgv_excel.Rows[j].Cells[intcols[i]];
13                         dgvic.ErrorMsg = "不能为空值";
14                         blchflag = false;
15                     }
16                 }
17             }
18             return blchflag;
19         }

五、校验数据在数据库中是否存在(例子:客户是否存在)

View Code
  1 /// <summary>
  2         /// 设置验证后的客户在前台显示
  3         /// </summary>
  4         /// <param name="dgv_excel"></param>
  5         /// <param name="colindex"></param>
  6         /// <returns></returns>
  7         public static bool SetCustomerFlag(ComponentFactory.Krypton.Toolkit.KryptonDataGridView dgv_excel, int colindex)
  8         {
  9             string strresult = CheckCustomer(dgv_excel, colindex);
 10             bool blflag = true;
 11             if (!string.IsNullOrEmpty(strresult))
 12             {
 13                 blflag = false;
 14                 string[] strerror = strresult.Split('#');
 15                 for (int i = 0; i < strerror.Length; i++)
 16                 {
 17                     if (!string.IsNullOrEmpty(strerror[i]))
 18                     {
 19                         for (int j = 0; j < dgv_excel.Rows.Count; j++)
 20                         {
 21                             string dgvectext = DoImpBase.GetDgvCellText(dgv_excel.Rows[j].Cells[colindex]);
 22                          
 23                             if (strerror[i].Equals(dgvectext))
 24                             {
 25                                 DataGridViewTextBoxImpCell dgvic = (DataGridViewTextBoxImpCell)dgv_excel.Rows[j].Cells[colindex];
 26                                 dgvic.ErrorMsg = "客户不存在";
 27                             }
 28                         }
 29                     }
 30                 }
 31             }
 32             return blflag;
 33         }
 34         /// <summary>
 35         /// 验证导入的客户是否在数据库中存在
 36         /// </summary>
 37         /// <param name="dgv_excel">需要验证的dataGridview</param>
 38         /// <param name="colindex">客户存在的列索引</param>
 39         /// <returns></returns>
 40         public static string CheckCustomer(ComponentFactory.Krypton.Toolkit.KryptonDataGridView dgv_excel, int colindex)
 41         {
 42             List<string> ll = new List<string>();
 43             for (int i = 0; i < dgv_excel.Rows.Count; i++)
 44             {
 45                 string strtemp = DoImpBase.GetDgvCellText(dgv_excel.Rows[i].Cells[colindex]);
 46                 if (!string.IsNullOrEmpty(strtemp))
 47                 {
 48                     if (!ll.Contains(strtemp))
 49                     {
 50                         ll.Add(strtemp);
 51                     }
 52                 }
 53             }
 54             if (ll.Count > 0)
 55             {
 56                 return (string)DatabaseAccessService.execute("{? = call HCM_DEMAND_PCK.CHECK_CUSTOMER_FOR_UI('" + DoUtility.MakeSqlConten(ll,false) + "')}", ReturnType.STRING);
 57             }
 58             else
 59             {
 60                 return string.Empty;
 61             }
 62         }
 63  /// <summary>
 64         /// 快速生成调包的参数
 65         /// </summary>
 66         /// <param name="li"></param>
 67         /// <param name="blrep">是否有替换符</param>
 68         /// <returns></returns>
 69         public static string MakeSqlConten(List<string> li,bool blrep)
 70         {
 71             string ret = string.Empty;
 72             if (li.Count != 0)
 73             {
 74                 for (int i = 0; i < li.Count; i++)
 75                 {
 76                     if (blrep)
 77                     {
 78                         ret += DoImpBase.RepChar + "," + li[i] + "\n";
 79                     }
 80                     else
 81                     {
 82                         ret += li[i] + "\n";
 83                     }
 84                 }
 85             }
 86             ret = ret.TrimEnd('\n');
 87             return ret;
 88         }
 89 
 90 /*==========================================================
 91     HCM-B-P02-05  验证数据 客户
 92     参数:  p_customer              客户
 93     返回值:x_msg_data
 94             客户名称                系统中不存在 该客户
 95             NULL                     存在 该客户
 96     Created :  2009-10-09 hand
 97   ==========================================================*/
 98   PROCEDURE check_customer(p_customer IN VARCHAR2, x_msg_data OUT NOCOPY VARCHAR2) IS
 99     l_count NUMBER := 0;
100   BEGIN
101     --modify by kungan.huang,20100818,
102     --修改取客户的逻辑,取-以后的字符
103     SELECT COUNT(1)
104       INTO l_count
105       FROM hcm_customer_v hcv
106      WHERE hcv.customer_alias = substr(p_customer, instr(p_customer, '-') + 1);
107     IF l_count > 0 THEN
108       x_msg_data := NULL;
109     ELSE
110       x_msg_data := p_customer;
111     END IF;
112   END check_customer;
113 
114   FUNCTION check_customer_for_ui(p_in_parameter IN CLOB) RETURN VARCHAR2 IS
115     l_record_count NUMBER;
116     l_customer     VARCHAR2(50);
117     l_msg_data     VARCHAR2(500);
118     l_msg_return   VARCHAR2(4000);
119   BEGIN
120     split_parameter(p_in_parameter => p_in_parameter);
121     l_record_count := hcm_demand_pck.g_in_record_count;
122     --批处理开始
123     FOR i IN 1 .. l_record_count LOOP
124       --取参数
125       l_customer := hcm_demand_pck.g_in_parameter_table(i) (1);
126       l_msg_data := NULL;
127       --调用客户化物理盘点过程
128       check_customer(l_customer, l_msg_data);
129       --错误处理
130       IF l_msg_data IS NOT NULL THEN
131         IF l_msg_return IS NOT NULL THEN
132           l_msg_return := l_msg_return || '#' || l_msg_data;
133         ELSE
134           l_msg_return := l_msg_data;
135         END IF;
136       END IF;
137     END LOOP;
138     RETURN l_msg_return;
139   END check_customer_for_ui;

 

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3