private string strcheck0(ObservableCollection<TabMaterialInquiry> datalist)
{
Dictionary<string, string> mesg = new Dictionary<string, string>();
mesg.Add("Count", "数量"); mesg.Add("Needtime", "需求时间"); mesg.Add("SendPrice", "送审价格"); mesg.Add("Unit", "单位"); mesg.Add("Type", "规格型号");
mesg.Add("Name", "材料名称"); if (mesg == null)
return null;
int i = 1;
foreach (var item in datalist)
{
string mexg = SLCommon.DataValidation.getProperties(item, mesg);
if (!String.IsNullOrEmpty(mexg))
{
return "第" + i.ToString() + "行" + mexg;
}
i = i + 1;
}
return "";
}
getProperties方法定义:
//创建:tangang
//功能:页面不为空数据验证
//参数:需要验证的实体,Dictionary<string, string>需要验证的属性和名称
//Key:验证的属性 Value:弹出的提示信息
//Dictionary<"ProjectName", "项目名称">
//返回:错误信息
//说明:
//时间:2012-06-7
//调用说明
// Dictionary<string, string> mesg = new Dictionary<string, string>();
// mesg.Add("ProjectName", "项目名称");
// mesg.Add("Address", "项目地址");
// mesg.Add("NotificationTime", "通知时间");
// mesg.Add("TotalFunds", "资金总额");
// return SLCommon.DataValidation.getProperties(ProjectItem.projectdocument, mesg);
//
//
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
namespace Glodon.Module.SLCommon
{
public class DataValidation
{
//
/// <summary>
/// 验证数据不为空
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t">需要验证的实体</param>
/// <param name="mesg">需要验证的属性键值对集合</param>
/// <returns></returns>
public static string getProperties<T>(T t, Dictionary<string, string> mesg)
{
string tStr = string.Empty;
if (t == null)
{
return tStr;
}
//获取属性对象
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
return tStr;
}
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
object value = item.GetValue(t, null);
//如果为String类型就判断是否为空
if (item.PropertyType.Name.StartsWith("String"))
{
foreach (var items in mesg)
{
if (name.Equals(items.Key))//判断是否为用户所指定的对象
{
if (value == null)
{
tStr = items.Value + "不能为空";
return tStr;
}
if (string.IsNullOrEmpty(value.ToString().Trim()))
{
tStr = items.Value + "不能为空";
return tStr;
}
}
}
}
else
{
foreach (var items in mesg)
{
if (name.Equals(items.Key))//判断是否为用户所指定的对象
{
if (value == null)
{
tStr = items.Value + "不能为空";
return tStr;
}
if (string.IsNullOrEmpty(value.ToString()))
{
tStr = items.Value + "不能为空";
return tStr;
}
}
}
}
}
return tStr;
}
}
}