一、获取界面控件值内容。
1.数据提取。
Dictionary<string, string> dicParam = new Dictionary<string, string>();//创建数据字典
//获取搜索框选控件值
foreach (Control ctl in panelSearch.Controls) //遍历窗体下某一panel 中的控件
{
if (!ctl.GetType().Name.Equals("LabelControl")) //舍弃labl控件提示框值
{
BaseEdit be = (BaseEdit)ctl;
dicParam.Add(ctl.Name, be.EditValue == null ? string.Empty : be.EditValue.ToString()); //获取控件中的值
}
}///数据存储格式:
dicParam["控件Name","控件值"]
2.数据使用。
dicParam["控件Name"]
二、获取界面GridView 选中内容
1.数据提取
//关闭gridview编辑
gridview.CloseEditor();
//提交gridview的更改
gridview.UpdateCurrentRow();List<Dictionary<string, string>> listCheckMat = new List<Dictionary<string, string>>();
//获取选中行的内容
for (int i = 0; i < gv.DataRowCount; i++)
{
if ((bool)gvMatMain.GetDataRow(i)["选择"])
{
Dictionary<string, string> item = new Dictionary<string, string>();
item.Add("mat_no", gvMatMain.GetDataRow(i)["A"].ToString());
item.Add("mat_kind", gvMatMain.GetDataRow(i)["B"].ToString());
listCheckMat.Add(item);
}
}///数据存储格式
listCheckMat[值1,值2];
2.数据使用
foreach (Dictionary<string, string> listMatNo in listCheckMat )
{
listMatNo["mat_kind"];
}
三、设置常用dev控件样式
public void LookUpEdit(PanelControl panel)
{
foreach (var paramControl in panel.Controls)
{
string controlType = paramControl.GetType().Name;
switch (controlType)
{
case "GridLookUpEdit":
GridLookUpEdit lu = (GridLookUpEdit)paramControl;
lu.Properties.TextEditStyle = TextEditStyles.Standard;
lu.Properties.ImmediatePopup = true;
lu.Properties.PopupFilterMode = PopupFilterMode.Contains;
lu.Properties.AutoComplete = false;
break;
case "DateEdit":
DateEdit detime = (DateEdit)paramControl;
if (detime.Name == "deBeginTime")
detime.EditValue = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
else
detime.EditValue = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
detime.Properties.VistaDisplayMode = DefaultBoolean.True;
detime.Properties.VistaEditTime = DefaultBoolean.True;
detime.Properties.DisplayFormat.FormatType = FormatType.DateTime;
detime.Properties.EditFormat.FormatType = FormatType.DateTime;
detime.Properties.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
detime.Properties.EditFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
detime.Properties.Mask.EditMask = "yyyy-MM-dd HH:mm:ss";
break;
default:
break;
}
}
}
四、一个DaTaTable划分成多个DaTaTable
/// </summary>
/// <param name="dt">源数据DataTable</param>
/// <param name="condition">查询条件</param>
/// <returns></returns>
private DataTable GetNewDataTable(DataTable dt, string condition)
{
DataTable newdt = new DataTable();
newdt = dt.Clone();
if (dt.Rows.Count < 1)
return newdt;
DataRow[] dr = dt.Select(condition);
for (int i = 0; i < dr.Length; i++)
{
newdt.ImportRow((DataRow)dr[i]);
}
return newdt;//返回的查询结果
}
五、IList类型转DataTable
/// <summary>
/// IList转DateTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="i_objlist"></param>
/// <returns></returns>
public DataTable ConvertToDataTable<T>(IList<T> i_objlist)
{
if (i_objlist == null || i_objlist.Count <= 0)
{
return null;
}
DataTable dt = new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;
System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (T t in i_objlist)
{
if (t == null)
{
continue;
}
row = dt.NewRow();
for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi = myPropertyInfo[i];string name = pi.Name;
if (dt.Columns[name] == null)
{
column = new DataColumn(name, pi.PropertyType);
dt.Columns.Add(column);
}row[name] = pi.GetValue(t, null);
}
dt.Rows.Add(row);
}
return dt;
}
六、List去重
/// <summary>
/// string类型list去重
/// </summary>
/// <param name="list"></param>
public static int removeDuplicate(List<string> list)
{
int count = 0;
for (int i = 0; i < list.Count; i++)
for (int j = 0; j < i; j++)
if (list[i] == list[j])
list[j] = string.Empty;
for (int i = 0; i < list.Count; i++)
if (list[i] != string.Empty)
count++;
return count;
}
七、DataTable转换为Dictionary
public static Dictionary<string, object> Dt2Dic(DataTable dt)
{
if (dt.rows.count()<1)
{
return null;
}
Dictionary<string, object> dic = new Dictionary<string, object>();
foreach (DataColumn item in dt.Columns)
{
dic[item.ColumnName] = dt.Rows[0][item];
}return dic;
}
九、拷贝Model
public static object CopyModel(object newObj, object oldObj)
{
Type newType = newObj.GetType();
Type oldType = oldObj.GetType();
string fieldName = string.Empty;
try
{
foreach (PropertyInfo item in newType.GetProperties())
{
fieldName = item.Name;
if (oldType.GetProperty(item.Name) != null)
{
//获取oldObj属性值
object value = oldType.GetProperty(item.Name).GetValue(oldObj, null);
item.SetValue(newObj, value, null);
}
}
}
catch (Exception ex)
{
throw new Exception(string.Format("字段{0}设置失败,原因:{1}", fieldName, ex.Message));
}
return newObj;//新Model
}
十、获取目录下所有文件地址
public static List<string> listPath = new List<string>(); //地址存放变量
/// <summary>
/// 获取所有文件地址
/// </summary>
/// <param name="directoryPath">文件根地址</param>public static void GetFiles(string directoryPath)
{
foreach (string file in Directory.GetFiles(directoryPath))
{
listPath.Add(file);
}foreach (string subdirectory in Directory.GetDirectories(directoryPath))
{
GetFiles(subdirectory);
}
}
十一、DataTable转字符串(string)
#region DataTable转字符串
/// <summary>
/// DataTable转字符串
/// </summary>
/// <param name="dt">DataTable</param>
/// <param name="listStreing">列名</param>
/// <param name="fuHao">分割符号</param>
/// <returns></returns>
public Dictionary<string,string> GetDataTableToString(DataTable dt, List<string> listStreing,string fuHao)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
string srvvalue = string.Empty;
if (dt == null)
{
return dic;
}
foreach (string srv in listStreing)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (string.IsNullOrEmpty(srvvalue))
srvvalue = dt.Rows[i][srv].ToString();
else
srvvalue = srvvalue+ string.Format(",{0}",dt.Rows[i][srv].ToString());}
dic.Add(srv,srvvalue);
srvvalue = string.Empty;
}
return dic;
}
#endregion
十二、json串转DataTable
#region json串转DataTable
/// <summary>
/// json串转DataTable
/// </summary>
/// <param name="json">Json文本内容</param>
/// <param name="listJson"><json列名> 可为null</param>
/// <returns></returns>
public DataTable GetJsonDataTable(string json,List<string> listJson)
{
JObject jo = JObject.Parse(json);
string str = jo["data"].ToString();
// 将 JSON 字符串解析为 JArray 对象
JArray jsonArray = JArray.Parse(str);
//创建DataTable
DataTable dt = new DataTable();
// 添加列
foreach (JProperty property in jsonArray[0])
{
if (listJson != null)
{
foreach (string item in listJson)
{
if (property.Name == item)
dt.Columns.Add(property.Name);
}
}
else
{
dt.Columns.Add(property.Name);
}
}
// 添加行
foreach (JObject jsonObject in jsonArray)
{
DataRow row = dt.NewRow();
foreach (JProperty property in jsonObject.Properties())
{
row[property.Name] = property.Value.ToString();
}
dt.Rows.Add(row);
}
return dt;
}
#endregion
十三、Model对比
/// <summary>
/// Model对比
/// </summary>
/// <param name="newObj">新model</param>
/// <param name="oldObj">旧model</param>
/// <returns>差异集合list</returns>
public List<string> Contrastmodel(object newObj, object oldObj)
{
List<string> list = new List<string>();
Type newType = newObj.GetType();
Type oldType = oldObj.GetType();
string fieldName = string.Empty;
foreach (System.Reflection.PropertyInfo item in newType.GetProperties())
{
fieldName = item.Name;
if (oldType.GetProperty(fieldName).Equals(newType.GetProperty(fieldName)))
{
if (oldType.GetProperty(fieldName).GetValue(oldObj, null).ToString().Trim() != newType.GetProperty(fieldName).GetValue(newObj, null).ToString().Trim())
list.Add(fieldName);
}}
return list;
}