winform控件值绑定

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using System.Windows.Forms;
using System.Reflection;

namespace ZSDUIBase
{
/// <summary>
/// 窗体控件 管理
/// </summary>
public class ControlInfoManager
{

#region 控件搜索
/// 查找控件
/// <summary>
/// 查找控件
/// </summary>
/// <param name="findControl">控件所在容器</param>
/// <param name="ctlName">控件名(这里的控件名与实体的属性名相同,没有加cmlue,cmtxt之类的前缀)</param>
/// <param name="obj">返回的控件引用</param>
/// <returns></returns>
public static bool FindControl(Control findControl, string ctlName, ref Control ctl)
{
return
(ctl = FindControlByName(findControl, "txt" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "dt" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "lue" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "uue" + ctlName)) != null
//SearchLookUpEdit
|| (ctl = FindControlByName(findControl, "slu" + ctlName)) != null
//GridLookUpEdit
|| (ctl = FindControlByName(findControl, "glu" + ctlName)) != null
//TreeListLookUpEdit
|| (ctl = FindControlByName(findControl, "tlu" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "cbb" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "ch" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "cal" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "mo" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "mmo" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "uc" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "rg" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "but" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "lb" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "lbl" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "pce" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "se" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "com" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "btne" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "pop" + ctlName)) != null;
}
public static bool FindControlList(Control findControl, string ctlName, ref List<Control> ctl)
{
if (ctl == null) {
ctl = new List<Control>();
}
if (FindControlListByName(findControl, "txt" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "txt" + ctlName));
}
if (FindControlListByName(findControl, "com" + ctlName) != null)
{
ctl.AddRange(FindControlListByName(findControl, "com" + ctlName));
}
if (FindControlListByName(findControl, "dt" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "dt" + ctlName));
}
if (FindControlListByName(findControl, "lue" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "lue" + ctlName));
}
if (FindControlListByName(findControl, "uue" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "uue" + ctlName));
}
if (FindControlListByName(findControl, "slu" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "slu" + ctlName));
}
if (FindControlListByName(findControl, "glu" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "glu" + ctlName));
}
if (FindControlListByName(findControl, "tlu" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "tlu" + ctlName));
}
if (FindControlListByName(findControl, "cbb" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "cbb" + ctlName));
}
if (FindControlListByName(findControl, "ch" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "ch" + ctlName));
}
if (FindControlListByName(findControl, "cal" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "cal" + ctlName));
}
if (FindControlListByName(findControl, "mo" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "mo" + ctlName));
}
if (FindControlListByName(findControl, "mmo" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "mo" + ctlName));
}
if (FindControlListByName(findControl, "uc" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "uc" + ctlName));
}
if (FindControlListByName(findControl, "rg" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "rg" + ctlName));
}
if (FindControlListByName(findControl, "but" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "but" + ctlName));
}
if (FindControlListByName(findControl, "lb" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "lb" + ctlName));
}
if (FindControlListByName(findControl, "lbl" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "lbl" + ctlName));
}
if (FindControlListByName(findControl, "pce" + ctlName) != null)//PopupContainerEdit
{
ctl.AddRange(FindControlListByName(findControl, "pce" + ctlName));
}
if (FindControlListByName(findControl, "se" + ctlName) != null)//SpinEdit
{
ctl.AddRange(FindControlListByName(findControl, "se" + ctlName));
}
if (FindControlListByName(findControl, "btne" + ctlName) != null)//SpinEdit
{
ctl.AddRange(FindControlListByName(findControl, "btne" + ctlName));//ButtonEdit
}
bool result = false;
if (ctl != null && ctl.Count > 0) {
result = true;
}
return result;
}

/// 根据控件名称查找控件
/// <summary>
/// 根据控件名称查找控件
/// </summary>
/// <param name="findControl">容器控件</param>
/// <param name="controlName">控件名</param>
/// <returns>返回找到的第一个满足条件的控件</returns>
public static Control FindControlByName(Control findControl, string controlName)
{
if (findControl == null) {
return null;
}
Control[] ctlArray = findControl.Controls.Find(controlName, true);
if (ctlArray.Length > 0) {
return ctlArray[0];
}
else {
return null;
}
}
public static List<Control> FindControlListByName(Control findControl, string controlName)
{
if (findControl == null) {
return null;
}
Control[] ctlArray = findControl.Controls.Find(controlName, true);
if (ctlArray.Length > 0) {
return ctlArray.ToList();
}
else {
return null;
}
}
public static Control FindControlByName(Control findControl, string controlName, ref object values)
{
if (findControl == null) {
return null;
}
Control ctr = null;
FindControl(findControl, controlName, ref ctr);
if (ctr != null) {
values = GetControlValue(ctr);
return ctr;
}
values = string.Empty;
return null;
}
#endregion

/// 获取控件VAlues
/// <summary>
/// 获取控件VAlues
/// </summary>
/// <param name="ctl"></param>
/// <returns></returns>
private static object GetControlValue(Control ctl)
{
if (ctl is TextBox) {
return ((TextBox)ctl).Text.Trim();
}
if (ctl.GetType() == typeof(DevExpress.XtraEditors.DateEdit)) {
return ((DevExpress.XtraEditors.DateEdit)ctl).EditValue;
}
if (ctl.GetType() == typeof(DevExpress.XtraEditors.TextEdit)) {
return ((DevExpress.XtraEditors.TextEdit)ctl).EditValue;
}
if (ctl is UserLUEdit) {
return ((UserLUEdit)ctl).EditValue;
}
//if (ctl is UserLookUpEdit) {
// return ((UserLookUpEdit)ctl).EditValue;
//}
//if (ctl is SearchLookUpEdit) {
// return ((SearchLookUpEdit)ctl).EditValue;
//}
if (ctl is GridLookUpEdit) {
return ((GridLookUpEdit)ctl).EditValue;
}
//if (ctl is TreeListLookUpEdit) {
// return ((TreeListLookUpEdit)ctl).EditValue;
//}
if (ctl is CheckBox) {
return ((CheckBox)ctl).Checked;
}
if (ctl is DateTimePicker) {
return ((DateTimePicker)ctl).Value;
}
if (ctl is NumericUpDown) {
return ((NumericUpDown)ctl).Value;
}
if (ctl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit)) {
return ((DevExpress.XtraEditors.MemoEdit)ctl).EditValue;
}
if (ctl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
return ((DevExpress.XtraEditors.ButtonEdit)ctl).EditValue;
}
if (ctl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit)) {
return ((DevExpress.XtraEditors.CheckEdit)ctl).Checked;
}
return null;
}
#region SetInfo 方法 ,可以将页面控件值绑定到实体

///将控件的值传给实体类
/// <summary>
/// 将控件的值传给实体类
/// </summary>
/// <param name="findControl">包含取值控件的容器</param>
/// <param name="myInfo">要赋值的实体</param>
/// <returns>返回赋值完毕的实体</returns>
public static object SetInfo(Control findControl, object myInfo)
{
Control ctl = null;
string controlNames = string.Empty;
string sValue = string.Empty;
PropertyInfo[] pInfos = myInfo.GetType().GetProperties();
foreach (PropertyInfo pInfo in pInfos) {
controlNames = pInfo.Name;
if (!FindControl(findControl, controlNames, ref ctl)) {
continue;
}
if (ctl is TextBox) {
sValue = ((TextBox)ctl).Text.Trim();
if (sValue == string.Empty) {
SetDefaultValue(myInfo, pInfo);
}
else {
pInfo.SetValue(myInfo, Convert.ChangeType(sValue, pInfo.PropertyType), null);
}
}
else if (ctl is UserLUEdit) {
if (((UserLUEdit)ctl).EditValue == null) {
pInfo.SetValue(myInfo, null, null);
}
else {
if (pInfo.PropertyType.Name == "String") {
pInfo.SetValue(myInfo, ((UserLUEdit)ctl).EditValue.ToString(), null);
}
else {
pInfo.SetValue(myInfo, Convert.ChangeType(((UserLUEdit)ctl).EditValue, pInfo.PropertyType), null);
}
}
}
//else if (ctl is UserLookUpEdit) {
// if (((UserLookUpEdit)ctl).EditValue == null) {
// pInfo.SetValue(myInfo, null, null);
// }
// else {
// if (pInfo.PropertyType.Name == "String") {
// pInfo.SetValue(myInfo, ((UserLookUpEdit)ctl).EditValue.ToString(), null);
// }
// else {
// pInfo.SetValue(myInfo, Convert.ChangeType(((UserLookUpEdit)ctl).EditValue, pInfo.PropertyType), null);
// }
// }
//}
//else if (ctl is SearchLookUpEdit) {
// if (((SearchLookUpEdit)ctl).EditValue == null) {
// pInfo.SetValue(myInfo, null, null);
// }
// else {
// if (pInfo.PropertyType.Name == "String") {
// pInfo.SetValue(myInfo, ((SearchLookUpEdit)ctl).EditValue.ToString(), null);
// }
// else {
// pInfo.SetValue(myInfo, Convert.ChangeType(((SearchLookUpEdit)ctl).EditValue, pInfo.PropertyType), null);
// }
// }
//}
else if (ctl is GridLookUpEdit) {
if (((GridLookUpEdit)ctl).EditValue == null) {
pInfo.SetValue(myInfo, null, null);
}
else {
if (pInfo.PropertyType.Name == "String") {
pInfo.SetValue(myInfo, ((GridLookUpEdit)ctl).EditValue.ToString(), null);
}
else {
pInfo.SetValue(myInfo, Convert.ChangeType(((GridLookUpEdit)ctl).EditValue, pInfo.PropertyType), null);
}
}
}
//else if (ctl is TreeListLookUpEdit) {
// if (((TreeListLookUpEdit)ctl).EditValue == null) {
// pInfo.SetValue(myInfo, null, null);
// }
// else {
// if (pInfo.PropertyType.Name == "String") {
// pInfo.SetValue(myInfo, ((TreeListLookUpEdit)ctl).EditValue.ToString(), null);
// }
// else {
// pInfo.SetValue(myInfo, Convert.ChangeType(((TreeListLookUpEdit)ctl).EditValue, pInfo.PropertyType), null);
// }
// }
//}
else if (ctl is DevExpress.XtraEditors.LookUpEdit) {
if (((DevExpress.XtraEditors.LookUpEdit)ctl).EditValue == null) {
pInfo.SetValue(myInfo, null, null);
}
else {
if (pInfo.PropertyType.Name == "String") {
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.LookUpEdit)ctl).EditValue.ToString(), null);
}
else {
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.LookUpEdit)ctl).EditValue, pInfo.PropertyType), null);
}
}
}
else if (ctl is CheckBox) {
pInfo.SetValue(myInfo, ((CheckBox)ctl).Checked == true ? "Y" : "N", null);
}
else if (ctl is RadioGroup) {
pInfo.SetValue(myInfo, Convert.ChangeType(((RadioGroup)ctl).EditValue, pInfo.PropertyType), null);
}
else if (ctl is DateTimePicker) {
if (pInfo.PropertyType.Name == "String") {
pInfo.SetValue(myInfo, ((DateTimePicker)ctl).Value.ToString(), null);
}
else {
pInfo.SetValue(myInfo, Convert.ChangeType(((DateTimePicker)ctl).Value, pInfo.PropertyType), null);
}
}

else if (ctl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit)) {
if (pInfo.PropertyType.Name == "String") {
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.MemoEdit)ctl).EditValue, null);
}
else {
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.MemoEdit)ctl).EditValue, pInfo.PropertyType), null);
}
}
else if (ctl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
if (((DevExpress.XtraEditors.ButtonEdit)ctl).EditValue != null) {
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.ButtonEdit)ctl).EditValue, pInfo.PropertyType), null);
}
}
else if (ctl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit)) {
if (pInfo.PropertyType.Name == "String") {
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.CheckEdit)ctl).Checked ? "Y" : "N", null);
}
else {
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.CheckEdit)ctl).Checked, pInfo.PropertyType), null);
}
}
else if (ctl.GetType() == typeof(DevExpress.XtraEditors.TextEdit)) {
if (((DevExpress.XtraEditors.TextEdit)ctl).EditValue != null) {
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.TextEdit)ctl).EditValue, pInfo.PropertyType), null);
}
}
else if (ctl.GetType() == typeof(DevExpress.XtraEditors.DateEdit)) {
if (((DevExpress.XtraEditors.DateEdit)ctl).EditValue != null) {
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.DateEdit)ctl).EditValue, pInfo.PropertyType), null);
}
}
}
return myInfo;
}
///将控件的值传给实体类
/// <summary>
/// 将控件的值传给实体类
/// </summary>
/// <param name="findControl">包含控件的容器,从该容器中的控件取值</param>
/// <param name="myInfo">要赋值的实体类</param>
/// <param name="controlCount">控件个数</param>
/// <returns>返回赋值完毕的实体类</returns>
public static object SetInfo(Control findControl, object myInfo, ref int controlCount)
{
Control ctrl = null;
string controlNames = string.Empty;
string sValue = string.Empty;
controlCount = 0;
PropertyInfo[] pInfos = myInfo.GetType().GetProperties();
foreach (PropertyInfo pInfo in pInfos) {
controlNames = pInfo.Name;
if (!FindControl(findControl, controlNames, ref ctrl)) {
continue;
}
if (ctrl is TextBox) {
sValue = ((TextBox)ctrl).Text;
if (sValue == string.Empty) {
if (pInfo.PropertyType.Name == "Int" || pInfo.PropertyType.Name == "Int32" || pInfo.PropertyType.Name == "Int64") {
pInfo.SetValue(myInfo, int.MinValue, null);
}
else if (pInfo.PropertyType.Name == "Double" || pInfo.PropertyType.Name == "Decimal"
|| pInfo.PropertyType.Name == "Float" || pInfo.PropertyType.Name == "Single") {
pInfo.SetValue(myInfo, double.MinValue, null);
}
else {
pInfo.SetValue(myInfo, Convert.ChangeType(sValue, pInfo.PropertyType), null);
}
}
else {
pInfo.SetValue(myInfo, Convert.ChangeType(sValue, pInfo.PropertyType), null);
controlCount++;
}
}
else if (ctrl is UserLUEdit) {
if (((UserLUEdit)ctrl).EditValue == null) {
pInfo.SetValue(myInfo, null, null);
}
else {
if (pInfo.PropertyType.Name == "String") {
pInfo.SetValue(myInfo, ((UserLUEdit)ctrl).EditValue.ToString(), null);
}
else {
pInfo.SetValue(myInfo, Convert.ChangeType(((UserLUEdit)ctrl).EditValue, pInfo.PropertyType), null);
}
if (((UserLUEdit)ctrl).Text.Trim() != string.Empty) {
controlCount++;
}
}
}
//else if (ctrl is UserLookUpEdit) {
// if (((UserLookUpEdit)ctrl).EditValue == null) {
// pInfo.SetValue(myInfo, null, null);
// }
// else {
// if (pInfo.PropertyType.Name == "String") {
// pInfo.SetValue(myInfo, ((UserLookUpEdit)ctrl).EditValue.ToString(), null);
// }
// else {
// pInfo.SetValue(myInfo, Convert.ChangeType(((UserLookUpEdit)ctrl).EditValue, pInfo.PropertyType), null);
// }
// if (((UserLookUpEdit)ctrl).Text.Trim() != string.Empty) {
// controlCount++;
// }
// }
//}
else if (ctrl is CheckBox) {
pInfo.SetValue(myInfo, ((CheckBox)ctrl).Checked == true ? "Y" : "N", null);
}
else if (ctrl is NumericUpDown) {
pInfo.SetValue(myInfo, Convert.ChangeType(((NumericUpDown)ctrl).Value, pInfo.PropertyType), null);
}
else if (ctrl is DateTimePicker) {

if (pInfo.PropertyType.Name == "String") {
pInfo.SetValue(myInfo, ((DateTimePicker)ctrl).Value.ToString(), null);
}
else {
pInfo.SetValue(myInfo, Convert.ChangeType(((DateTimePicker)ctrl).Value, pInfo.PropertyType), null);
}
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit)) {
if (pInfo.PropertyType.Name == "String") {
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.MemoEdit)ctrl).EditValue, null);
}
else {
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.MemoEdit)ctrl).EditValue, pInfo.PropertyType), null);
}
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.ButtonEdit)ctrl).EditValue, null);
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit)) {
if (pInfo.PropertyType == typeof(string)) {
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.CheckEdit)ctrl).Checked ? "Y" : "N", null);
}
else {
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.CheckEdit)ctrl).Checked, null);
}
}
else if (ctrl is DevExpress.XtraEditors.TextEdit) {
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.TextEdit)ctrl).EditValue, null);
}

}

return myInfo;
}

#endregion

#region 实体到控件
/// <summary>
/// 根据实体属性值设置控件值,要求实体属性名与除去前缀的控件命名相同
/// </summary>
/// <param name="myInfo">带数据的实体</param>
/// <param name="findControl">容器控件</param>
public static void SetControlValue(object myInfo, Control findControl)
{
SetControlValue(myInfo, findControl, string.Empty);
}
/// <summary>
/// 根据实体属性值设置控件值,要求实体属性名与除去前缀、后缀的控件命名相同
/// </summary>
/// <param name="myInfo">带数据的实体</param>
/// <param name="findControl">容器,要设置值的控件</param>
/// <param name="strSuffix">关键字 如:txtName_Key 中 _Key )</param>
public static void SetControlValue(object myInfo, Control findControl, string strSuffix)
{
Control ctrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名
PropertyInfo[] pInfos = myInfo.GetType().GetProperties();
object propValue = null; //属性值
string propType = string.Empty; //属性类型
foreach (PropertyInfo pi in pInfos) {
controlName = pi.Name.ToUpper() + strSuffix.ToUpper();
propValue = pi.GetValue(myInfo, null);
propType = pi.PropertyType.Name.ToUpper();
if (propValue == null || IsDefaultValue(propValue)) {
continue;
}
if (!FindControl(findControl, controlName, ref ctrl)) {
continue;
}
if (ctrl is TextBox || ctrl is DevExpress.XtraEditors.TextEdit) {
((TextBox)ctrl).Text = propValue.ToString();
}
else if (ctrl is CheckBox) {
((CheckBox)ctrl).Checked = (propValue.ToString() == "Y" ? true : false);
}
else if (ctrl is NumericUpDown) {
((NumericUpDown)ctrl).Value = Convert.ToDecimal(propValue.ToString());
}
else if (ctrl is RadioGroup) {
((RadioGroup)ctrl).EditValue = propValue.ToString();
}
else if (ctrl is DateTimePicker) {
((DateTimePicker)ctrl).Value = Convert.ToDateTime(propValue.ToString());
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.DateEdit)) {
if (!string.IsNullOrEmpty(propValue.ToString()) && Convert.ToDateTime(propValue) != DateTime.MinValue) {
((DevExpress.XtraEditors.DateEdit)ctrl).EditValue = Convert.ToDateTime(propValue.ToString());
}
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit)) {
((DevExpress.XtraEditors.TextEdit)ctrl).EditValue = propValue.ToString();
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit)) {
((DevExpress.XtraEditors.MemoEdit)ctrl).EditValue = propValue.ToString();
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
((DevExpress.XtraEditors.ButtonEdit)ctrl).EditValue = propValue.ToString();
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit)) {
if (pi.PropertyType == typeof(string)) {
((DevExpress.XtraEditors.CheckEdit)ctrl).Checked = propValue.ToString() == "Y" ? true : false;
}
else {
((DevExpress.XtraEditors.CheckEdit)ctrl).Checked = Convert.ToBoolean(propValue.ToString());
}
}
else if (ctrl is UserLUEdit) {
if (!string.IsNullOrEmpty(propValue.ToString())) {
((UserLUEdit)ctrl).EditValue = Convert.ChangeType(propValue.ToString(), pi.PropertyType);
}
}
//else if (ctrl is UserLookUpEdit) {
// if (!string.IsNullOrEmpty(propValue.ToString())) {
// ((UserLookUpEdit)ctrl).EditValue = Convert.ChangeType(propValue.ToString(), pi.PropertyType);
// }
//}
//else if (ctrl is SearchLookUpEdit) {
// if (!string.IsNullOrEmpty(propValue.ToString())) {
// ((SearchLookUpEdit)ctrl).EditValue = Convert.ChangeType(propValue.ToString(), pi.PropertyType);
// }
//}
else if (ctrl is GridLookUpEdit) {
if (!string.IsNullOrEmpty(propValue.ToString())) {
((GridLookUpEdit)ctrl).EditValue = Convert.ChangeType(propValue.ToString(), pi.PropertyType);
}
}
//else if (ctrl is TreeListLookUpEdit) {
// if (!string.IsNullOrEmpty(propValue.ToString())) {
// ((TreeListLookUpEdit)ctrl).EditValue = Convert.ChangeType(propValue.ToString(), pi.PropertyType);
// }
//}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LookUpEdit)) {
if (!string.IsNullOrEmpty(propValue.ToString())) {
((DevExpress.XtraEditors.LookUpEdit)ctrl).EditValue = Convert.ChangeType(propValue.ToString(), pi.PropertyType);
}
}
}
}
#endregion

#region 实体绑定到控件(DataBinding)
// 根据实体属性值 绑定 控件属性值,要求实体属性名与除去前缀的控件命名相同
/// <summary>
/// 根据实体属性值 绑定 控件属性值,要求实体属性名与除去前缀的控件命名相同
/// </summary>
/// <param name="myInfo">带数据的实体</param>
/// <param name="findControl">容器控件</param>
public static void EntityToControlDataBing(object myInfo, Control findControl)
{
EntityToControlDataBing(myInfo, findControl, string.Empty);
}

// 根据实体属性值 绑定 控件属性值,要求实体属性名与除去前缀 或 后缀的控件命名相同
/// <summary>
/// 根据实体属性值 绑定 控件属性值,要求实体属性名与除去前缀 或 后缀的控件命名相同
/// </summary>
/// <param name="myInfo">带数据的实体</param>
/// <param name="findControl">容器,要设置值的控件</param>
/// <param name="strSuffix">关键字 如:txtName_Key 中 _Key </param>
public static void EntityToControlDataBing(object myInfo, Control findControl, string SuffixKey)
{
Control ctrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名
if (myInfo == null) return;
PropertyInfo[] pInfos = myInfo.GetType().GetProperties();
string propType = string.Empty; //属性类型
foreach (PropertyInfo pi in pInfos) {
controlName = pi.Name + SuffixKey;

if (!FindControl(findControl, controlName, ref ctrl)) {
continue;
}
ctrl.DataBindings.Clear();
if (ctrl is TextBox) {
ctrl.DataBindings.Add("Text", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl is CheckBox) {

ctrl.DataBindings.Add("Checked", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl is NumericUpDown) {

ctrl.DataBindings.Add("Value", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl is RadioGroup) {

ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

}
else if (ctrl is DateTimePicker) {

ctrl.DataBindings.Add("Value", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.DateEdit)) {

ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit)) {

ctrl.DataBindings.Add("Text", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit)) {

ctrl.DataBindings.Add("Text", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit)) {

ctrl.DataBindings.Add("Checked", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl is UserLUEdit) {
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

}
//else if (ctrl is UserLookUpEdit) {
// ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

//}
//else if (ctrl is SearchLookUpEdit) {
// ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

//}
else if (ctrl is GridLookUpEdit) {
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

}
//else if (ctrl is TreeListLookUpEdit) {
// ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

//}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LookUpEdit)) {
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
//else if (ctrl is UIBase.ucYearMonth) {
// ctrl.DataBindings.Add("YearMonth", myInfo, pi.Name, true, DataSourceUpdateMode.OnValidation);
//}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CalcEdit)) {
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ComboBoxEdit)) {
ctrl.DataBindings.Add("Text", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl is UserCheckedComboBoxEdit)
{
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckedComboBoxEdit))
{
ctrl.DataBindings.Add("Text", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.PopupContainerEdit))
{
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
}
}

#endregion

#region 控件设置为 只读 状态(ReadOnly)
static Control TempCotrl = null;
/// 设置容器控件中 子控件 ReadOnly状态
/// <summary>
/// 设置容器控件中 子控件 ReadOnly状态
/// </summary>
/// <param name="findControl"> 容器控件</param>
/// <param name="isReadOnly"> ReadOnly状态 </param>
/// <param name="isContainsPanel"> ReadOnly状态 </param>
/// <param name="isContainsGrid"> ReadOnly状态 </param>
public static void SetControlReadOnly(Control findControl, bool isReadOnly, bool isContainsPanel = true, bool isContainsGrid = true)
{
if (findControl == null) return;
foreach (Control col in findControl.Controls) {
if (string.IsNullOrEmpty(col.Name)) continue;
if (isContainsGrid) {
if (col is DevExpress.XtraGrid.GridControl) continue;
}
SetControlReadOnlyState(col, isReadOnly, isContainsPanel);
if (col.Controls.Count > 0) {
SetControlReadOnly(col, isReadOnly, isContainsPanel);
}
}
}
/// 设置容器中具体某个控件 ReadOnly状态
/// <summary>
/// 设置容器中具体某个控件 ReadOnly状态
/// </summary>
/// <param name="ProName">对应实体字段名</param>
/// <param name="findControl"></param>
/// <param name="strSuffix">关键字 如:txtName_Key 中 _Key </param>
/// <param name="isReadOnly">ReadOnly状态</param>
public static void SetSpecificControlReadOnly(string ProName, Control findControl, bool isReadOnly, string strSuffix = "")
{
Control ctrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名
controlName = ProName + strSuffix.ToUpper();
if (!FindControl(findControl, controlName, ref ctrl)) {
return;
}
SetControlReadOnlyState(ctrl, isReadOnly, true);
}

/// 设置容器中具体某个控件 ReadOnly状态(遍历所有子控件)
/// <summary>
/// 设置容器中具体某个控件 ReadOnly状态(遍历所有子控件)
/// </summary>
/// <param name="ProName">>对应实体字段名</param>
/// <param name="findControl">父控件</param>
/// <param name="isReadOnly"></param>
/// <param name="strSuffix">关键字 如:txtName_Key 中 _Key</param>
/// <param name="CantainGrid">GridView 列是否搜索 </param>
public static void SetSpecificControlReadOnlyState(string ProName, Control findControl, bool isReadOnly, string strSuffix = "", bool CantainGrid = false)
{
TempCotrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名
controlName = ProName + strSuffix.ToUpper();
if (findControl.Controls.Count == 0) return;
if (!FindControl(findControl, controlName, ref TempCotrl)) {
#region =====================
foreach (Control col in findControl.Controls) {
if (col.Controls.Count > 0) {
if (FindControl(col, controlName, ref TempCotrl)) {
SetControlReadOnlyState(TempCotrl, isReadOnly, true);
return;
}
else {
if (col is DevExpress.XtraGrid.GridControl && CantainGrid) {
DevExpress.XtraGrid.GridControl grid = (DevExpress.XtraGrid.GridControl)col;
DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)grid.ViewCollection[0];
foreach (DevExpress.XtraGrid.Columns.GridColumn cl in view.Columns) {
if (cl.Name == controlName || cl.FieldName == controlName) {
cl.OptionsColumn.ReadOnly = isReadOnly;
return;
}
}
}
SetSpecificControlReadOnlyState(ProName, col, isReadOnly, strSuffix);
}
}
}
#endregion
}
else {
SetControlReadOnlyState(TempCotrl, isReadOnly, true);
return;
}
}
private static void SetControlReadOnlyState(Control ctrl, bool ReadOnlyState, bool isContainsPanel)
{
if (ctrl is TextBox) {
((TextBox)ctrl).ReadOnly = ReadOnlyState;
}
else if (ctrl is CheckBox) {
((CheckBox)ctrl).Enabled = !ReadOnlyState;
}
else if (ctrl is NumericUpDown) {
((NumericUpDown)ctrl).ReadOnly = ReadOnlyState;
}
else if (ctrl is RadioGroup) {
((RadioGroup)ctrl).Properties.ReadOnly = ReadOnlyState;
}
else if (ctrl is DateTimePicker) {
((DateTimePicker)ctrl).Enabled = !ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.DateEdit)) {
((DateEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}

else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit)) {
((TextEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit)) {
((MemoEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
((ButtonEdit)ctrl).Enabled = !ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit)) {
((CheckEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}
else if (ctrl is UserLUEdit) {
((UserLUEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}
//else if (ctrl is UserLookUpEdit) {
// ((UserLookUpEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
//}
//else if (ctrl is SearchLookUpEdit) {
// ((SearchLookUpEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
//}
else if (ctrl is GridLookUpEdit) {
((GridLookUpEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}
//else if (ctrl is TreeListLookUpEdit) {
// ((TreeListLookUpEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
//}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LookUpEdit)) {
((DevExpress.XtraEditors.LookUpEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
((DevExpress.XtraEditors.ButtonEdit)ctrl).Enabled = !ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CalcEdit)) {
((DevExpress.XtraEditors.CalcEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
((DevExpress.XtraEditors.ButtonEdit)ctrl).Enabled = !ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.SimpleButton)) {
((DevExpress.XtraEditors.SimpleButton)ctrl).Enabled = !ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.PopupContainerEdit) && isContainsPanel) {
((DevExpress.XtraEditors.PopupContainerEdit)ctrl).Enabled = !ReadOnlyState;
}
}

#endregion

#region 控件设置为(Enabled)

/// <summary>
///设置容器控件中 子控件 Enabled交互状态
/// </summary>
/// <param name="findControl"></param>
/// <param name="IsEnabled"></param>
/// <param name="isContainsPanel"></param>
public static void SetControlEnabled(Control findControl, bool IsEnabled, bool isContainsPanel = true)
{
if (findControl == null) return;
foreach (Control col in findControl.Controls) {
if (col is Label || col is LabelControl)
continue;
if (string.IsNullOrEmpty(col.Name)) continue;
SetControlToEnabled(col, IsEnabled, isContainsPanel);
if (col.Controls.Count > 0) {
SetControlEnabled(col, IsEnabled, isContainsPanel);
}
}
}
/// 设置容器中具体某个控件 Enabled状态(遍历所有子控件)
/// <summary>
/// 设置容器中具体某个控件 Enabled状态(遍历所有子控件)
/// </summary>
/// <param name="ProName">>对应实体字段名</param>
/// <param name="findControl">父控件</param>
/// <param name="ReadOnlyState"></param>
/// <param name="strSuffix">关键字 如:txtName_Key 中 _Key</param>
/// <param name="CantainGrid">GridView 列是否搜索 </param>
public static void SetSpecificControlEnabled(string ProName, Control findControl, bool ReadOnlyState, string strSuffix = "", bool CantainGrid = false)
{
TempCotrl = null;
string controlName = string.Empty;
controlName = ProName + strSuffix.ToUpper();
if (findControl.Controls.Count == 0) return;
if (!FindControl(findControl, controlName, ref TempCotrl)) {
foreach (Control col in findControl.Controls) {
if (col.Controls.Count > 0) {
if (FindControl(col, controlName, ref TempCotrl)) {
SetControlToEnabled(TempCotrl, ReadOnlyState, true);
return;
}
else {
if (col is DevExpress.XtraGrid.GridControl && CantainGrid) {
DevExpress.XtraGrid.GridControl grid = (DevExpress.XtraGrid.GridControl)col;
DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)grid.ViewCollection[0];
foreach (DevExpress.XtraGrid.Columns.GridColumn cl in view.Columns) {
if (cl.Name == controlName || cl.FieldName == controlName) {
cl.OptionsColumn.ReadOnly = ReadOnlyState;
return;
}
}
}
SetSpecificControlReadOnlyState(ProName, col, ReadOnlyState, strSuffix);
}
}
}
}
else {
SetControlToEnabled(TempCotrl, ReadOnlyState, true);
return;
}
}

private static void SetControlToEnabled(Control ctrl, bool isReadOnly, bool isContainsPanel)
{
if (ctrl is TextBox) {
((TextBox)ctrl).Enabled = isReadOnly;
}
else if (ctrl is CheckBox) {
((CheckBox)ctrl).Enabled = isReadOnly;
}
else if (ctrl is NumericUpDown) {
((NumericUpDown)ctrl).Enabled = isReadOnly;
}
else if (ctrl is RadioGroup) {
((RadioGroup)ctrl).Properties.Enabled = isReadOnly;
}
else if (ctrl is DateTimePicker) {
((DateTimePicker)ctrl).Enabled = isReadOnly;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.DateEdit)) {
((DateEdit)ctrl).Properties.Enabled = isReadOnly;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit)) {
((TextEdit)ctrl).Properties.Enabled = isReadOnly;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit)) {
((MemoEdit)ctrl).Properties.Enabled = isReadOnly;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
((ButtonEdit)ctrl).Enabled = isReadOnly;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit)) {
((CheckEdit)ctrl).Properties.Enabled = isReadOnly;
}
else if (ctrl is UserLUEdit) {
((UserLUEdit)ctrl).Properties.Enabled = isReadOnly;
}
//else if (ctrl is UserLookUpEdit) {
// ((UserLookUpEdit)ctrl).Properties.Enabled = isReadOnly;
//}
//else if (ctrl is SearchLookUpEdit) {
// ((SearchLookUpEdit)ctrl).Properties.Enabled = isReadOnly;
//}
else if (ctrl is GridLookUpEdit) {
((GridLookUpEdit)ctrl).Properties.Enabled = isReadOnly;
}
//else if (ctrl is TreeListLookUpEdit) {
// ((TreeListLookUpEdit)ctrl).Properties.Enabled = isReadOnly;
//}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LookUpEdit)) {
((DevExpress.XtraEditors.LookUpEdit)ctrl).Properties.Enabled = isReadOnly;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
((DevExpress.XtraEditors.ButtonEdit)ctrl).Enabled = isReadOnly;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CalcEdit)) {
((DevExpress.XtraEditors.CalcEdit)ctrl).Properties.Enabled = isReadOnly;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
((DevExpress.XtraEditors.ButtonEdit)ctrl).Enabled = isReadOnly;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.SimpleButton)) {
((DevExpress.XtraEditors.SimpleButton)ctrl).Enabled = isReadOnly;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.PopupContainerEdit) && isContainsPanel) {
((DevExpress.XtraEditors.PopupContainerEdit)ctrl).Enabled = isReadOnly;
}
}

#endregion

#region 控件设置为 只读 状态(Visable)
///批量 设置控件的 Visable
/// <summary>
///批量 设置控件的 Visable
/// </summary>
/// <param name="Controls"></param>
/// <param name="VisableState"></param>
public static void SetControlVisableByListControls(List<Control> Controls, bool VisableState)
{
if (Controls == null) return;
foreach (Control ctrl in Controls) {
SetControlVisable(ctrl, VisableState);
}
}
/// 设置控件与其子控件的Visable
/// <summary>
/// 设置控件与其子控件的Visable
/// </summary>
/// <param name="findControl"></param>
/// <param name="VisableState"></param>
public static void SetControlVisable(Control findControl, bool VisableState)
{
if (findControl == null) return;
SetControlVisibleStatus(findControl, VisableState);
foreach (Control ctrl in findControl.Controls) {
SetControlVisibleStatus(ctrl, VisableState);
if (ctrl.Controls.Count > 0) {
if (ctrl is DevExpress.XtraGrid.GridControl) continue;
SetControlVisable(ctrl, VisableState);
}
}
}
private static void SetControlVisibleStatus(Control ctrl, bool VisibleState)
{
if (ctrl is TextBox) {
((TextBox)ctrl).Visible = VisibleState;
}
else if (ctrl is CheckBox) {
((CheckBox)ctrl).Visible = VisibleState;
}
else if (ctrl is Label) {
((Label)ctrl).Visible = VisibleState;
}
else if (ctrl is NumericUpDown) {
((NumericUpDown)ctrl).Visible = VisibleState;
}
else if (ctrl is RadioGroup) {
((RadioGroup)ctrl).Visible = VisibleState;
}
else if (ctrl is DateTimePicker) {
((DateTimePicker)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.DateEdit)) {
((DevExpress.XtraEditors.DateEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit)) {
((DevExpress.XtraEditors.TextEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit)) {
((MemoEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
((ButtonEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit)) {
((CheckEdit)ctrl).Visible = VisibleState;
}
else if (ctrl is UserLUEdit) {
((UserLUEdit)ctrl).Visible = VisibleState;
}
//else if (ctrl is UserLookUpEdit) {
// ((UserLookUpEdit)ctrl).Visible = VisibleState;
//}
//else if (ctrl is SearchLookUpEdit) {
// ((SearchLookUpEdit)ctrl).Visible = VisibleState;
//}
else if (ctrl is GridLookUpEdit) {
((GridLookUpEdit)ctrl).Visible = VisibleState;
}
//else if (ctrl is TreeListLookUpEdit) {
// ((TreeListLookUpEdit)ctrl).Visible = VisibleState;
//}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LookUpEdit)) {
((DevExpress.XtraEditors.LookUpEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
((DevExpress.XtraEditors.ButtonEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CalcEdit)) {
((DevExpress.XtraEditors.CalcEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraGrid.GridControl)) {
((DevExpress.XtraGrid.GridControl)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.SimpleButton)) {
((DevExpress.XtraEditors.SimpleButton)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LabelControl)) {
((DevExpress.XtraEditors.LabelControl)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit)) {
((DevExpress.XtraEditors.TextEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LabelControl)) {
((DevExpress.XtraEditors.LabelControl)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
((DevExpress.XtraEditors.ButtonEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.SimpleButton)) {
((DevExpress.XtraEditors.SimpleButton)ctrl).Visible = VisibleState;
}
else {
ctrl.Visible = VisibleState;
}
}
public static void SetControlsVisibleStatus(List<Control> ctrllist, bool VisibleState)
{
if (ctrllist == null) { return; }
foreach (Control c in ctrllist) {
SetControlVisibleStatus(c, VisibleState);
}
}
static List<Control> ctrllist = null;
/// 设置容器中具体某个控件 Visible状态(遍历所有子控件)
/// <summary>
/// 设置容器中具体某个控件 Visible状态(遍历所有子控件)
/// </summary>
/// <param name="ProName">>对应实体字段名</param>
/// <param name="findControl">父控件</param>
/// <param name="VisibleState"></param>
/// <param name="strSuffix">关键字 如:txtName_Key 中 _Key</param>
/// <param name="CantainGrid">GridView 列是否搜索 </param>
public static void SetControlVisibleState(string ProName, Control findControl, bool VisibleState, string strSuffix = "", bool CantainGrid = false)
{
ctrllist = null;
TempCotrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名
controlName = ProName + strSuffix.ToUpper();
if (findControl.Controls.Count == 0) {
if (findControl.Name == ProName) {
SetControlVisibleStatus(findControl, VisibleState);
return;
}
}
else
if (findControl.Name == ProName) {
SetControlVisibleStatus(findControl, VisibleState);
return;
}
if (findControl is DevExpress.XtraGrid.GridControl && CantainGrid) {
DevExpress.XtraGrid.GridControl grid = (DevExpress.XtraGrid.GridControl)findControl;
DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)grid.ViewCollection[0];
foreach (DevExpress.XtraGrid.Columns.GridColumn cl in view.Columns) {
if (cl.Name == controlName || cl.FieldName.ToUpper() == controlName.ToUpper()) {
cl.Visible = VisibleState;
}
}
}
if (FindControlList(findControl, controlName, ref ctrllist)) {
foreach (Control c in ctrllist) {
SetControlVisibleStatus(c, VisibleState);
}
}
else {
return;
}
}
#endregion

/// 设置控件为空值 Empty
/// <summary>
/// 设置控件为空值 Empty
/// </summary>
/// <param name="ctrl"></param>
public static void SetControlValueEmpty(Control ctrl)
{
if (ctrl == null) return;
foreach (Control ch in ctrl.Controls) {
if (ch is Label || ch is LabelControl)
continue;
SetValueEmpty(ch);

}
}
private static void SetValueEmpty(Control ctrl)
{
if (ctrl is TextBox) {
((TextBox)ctrl).Text = "";
}
else if (ctrl is CheckBox) {
((CheckBox)ctrl).Checked = false;
}
else if (ctrl is NumericUpDown) {
((NumericUpDown)ctrl).Value = 0;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit)) {
((TextEdit)ctrl).Text = "";
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit)) {
((MemoEdit)ctrl).Text = "";
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit)) {
((CheckEdit)ctrl).Checked = false;
}
else if (ctrl is UserLUEdit) {
((UserLUEdit)ctrl).EditValue = null;
}
//else if (ctrl is UserLookUpEdit) {
// ((UserLookUpEdit)ctrl).EditValue = null;
//}
//else if (ctrl is SearchLookUpEdit) {
// ((SearchLookUpEdit)ctrl).EditValue = null;
//}
else if (ctrl is GridLookUpEdit) {
((GridLookUpEdit)ctrl).EditValue = null;
}
//else if (ctrl is TreeListLookUpEdit) {
// ((TreeListLookUpEdit)ctrl).EditValue = null;
//}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LookUpEdit)) {
((DevExpress.XtraEditors.LookUpEdit)ctrl).EditValue = null;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CalcEdit)) {
((DevExpress.XtraEditors.CalcEdit)ctrl).Value = 0;
}
}

/// 获取制定控件的值
/// <summary>
/// 获取制定控件的值
/// </summary>
/// <param name="ctrl"></param>
public static object GetControlValueByName(Control findControl, string FileName, string strSuffix = "")
{
TempCotrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名
controlName = FileName + strSuffix.ToUpper();
if (findControl.Controls.Count == 0) return "";
if (FindControl(findControl, controlName, ref TempCotrl)) {
return GetControlValue(TempCotrl);
}
else {

}
return null;

}

///判断给定对象是否是默认值
/// <summary>
/// 方法说明:判断给定对象是否是默认值
/// </summary>
/// <param name="value">待检测的对象</param>
/// <returns>是默认值返回true,否则返回false</returns>
public static bool IsDefaultValue(object value)
{
if (value == null) {
return true;
}
if (value.GetType().Equals(typeof(bool))) {
return false;
}
return value.Equals(GetDefaultValue(value.GetType()));
}

/// 方法说明:获取类型的默认值
/// <summary>
/// 方法说明:获取类型的默认值
/// </summary>
/// <param name="type">类型</param>
/// <returns>默认值</returns>
public static object GetDefaultValue(Type type)
{
if (type.Equals(typeof(bool))) {
return false;
}
if (type.Equals(typeof(string))) {
return null;
}
FieldInfo minValueField = type.GetField("MinValue");
if (minValueField != null) {
return minValueField.GetValue(null);
}

return null;
}
///方法说明:设置实体成员的默认值
/// <summary>
/// 方法说明:设置实体成员的默认值
/// </summary>
/// <param name="entity">实体</param>
/// <param name="member">实体成员信息</param>
public static void SetDefaultValue(object entity, MemberInfo member)
{
if ((entity == null) || (member == null)) {
return;
}
switch (member.MemberType) {
case MemberTypes.Field:
FieldInfo field = member as FieldInfo;
if (field != null) {
field.SetValue(entity, GetDefaultValue(field.FieldType));
}
break;
case MemberTypes.Property:
PropertyInfo property = member as PropertyInfo;
if (property != null) {
property.SetValue(entity, GetDefaultValue(property.PropertyType), null);
}
break;
default:
break;
}
}
public static void GetAllControls(Control.ControlCollection ctrls, ref List<Control> returnctls)
{
if (returnctls == null || returnctls.Count < 0) {
returnctls = new List<Control>();
}
foreach (Control c in ctrls) {
returnctls.Add(c);
if (c.HasChildren) {
GetAllControls(c.Controls, ref returnctls);
}
}
}

}

 


public class ControlInfoManager33
{

#region 控件搜索
/// <summary>
/// </summary>
/// <param name="findControl">控件所在容器</param>
/// <param name="ctlName">控件名(这里的控件名与实体的属性名相同,没有加cmlue,cmtxt之类的前缀)</param>
/// <param name="obj">返回的控件引用</param>
/// <returns></returns>
public static bool FindControl(Control findControl, string ctlName, ref Control ctl)
{
return
(ctl = FindControlByName(findControl, "txt" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "dt" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "lue" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "uue" + ctlName)) != null
//SearchLookUpEdit
|| (ctl = FindControlByName(findControl, "slu" + ctlName)) != null
//GridLookUpEdit
|| (ctl = FindControlByName(findControl, "glu" + ctlName)) != null
//TreeListLookUpEdit
|| (ctl = FindControlByName(findControl, "tlu" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "cbb" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "ch" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "cal" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "mo" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "mmo" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "uc" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "rg" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "but" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "lb" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "lbl" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "pce" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "se" + ctlName)) != null
|| (ctl = FindControlByName(findControl, "btne" + ctlName)) != null;
}
public static bool FindControlList(Control findControl, string ctlName, ref List<Control> ctl)
{
if (ctl == null) {
ctl = new List<Control>();
}
if (FindControlListByName(findControl, "txt" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "txt" + ctlName));
}
if (FindControlListByName(findControl, "dt" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "dt" + ctlName));
}
if (FindControlListByName(findControl, "lue" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "lue" + ctlName));
}
if (FindControlListByName(findControl, "uue" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "uue" + ctlName));
}
if (FindControlListByName(findControl, "slu" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "slu" + ctlName));
}
if (FindControlListByName(findControl, "glu" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "glu" + ctlName));
}
if (FindControlListByName(findControl, "tlu" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "tlu" + ctlName));
}
if (FindControlListByName(findControl, "cbb" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "cbb" + ctlName));
}
if (FindControlListByName(findControl, "ch" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "ch" + ctlName));
}
if (FindControlListByName(findControl, "cal" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "cal" + ctlName));
}
if (FindControlListByName(findControl, "mo" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "mo" + ctlName));
}
if (FindControlListByName(findControl, "mmo" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "mo" + ctlName));
}
if (FindControlListByName(findControl, "uc" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "uc" + ctlName));
}
if (FindControlListByName(findControl, "rg" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "rg" + ctlName));
}
if (FindControlListByName(findControl, "but" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "but" + ctlName));
}
if (FindControlListByName(findControl, "lb" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "lb" + ctlName));
}
if (FindControlListByName(findControl, "lbl" + ctlName) != null) {
ctl.AddRange(FindControlListByName(findControl, "lbl" + ctlName));
}
if (FindControlListByName(findControl, "pce" + ctlName) != null)//PopupContainerEdit
{
ctl.AddRange(FindControlListByName(findControl, "pce" + ctlName));
}
if (FindControlListByName(findControl, "se" + ctlName) != null)//SpinEdit
{
ctl.AddRange(FindControlListByName(findControl, "se" + ctlName));
}
if (FindControlListByName(findControl, "btne" + ctlName) != null)//SpinEdit
{
ctl.AddRange(FindControlListByName(findControl, "btne" + ctlName));//ButtonEdit
}
bool result = false;
if (ctl != null && ctl.Count > 0) {
result = true;
}
return result;
}

/// <summary>
/// 根据控件名查找控件
/// </summary>
/// <param name="findControl">容器控件</param>
/// <param name="controlName">控件名</param>
/// <returns>返回找到的第一个满足条件的控件</returns>
public static Control FindControlByName(Control findControl, string controlName)
{
if (findControl == null)
{
return null;
}
Control[] ctlArray = findControl.Controls.Find(controlName, true);
if (ctlArray.Length > 0)
{
return ctlArray[0];
}
else
{
return null;
}
}

public static List<Control> FindControlListByName(Control findControl, string controlName)
{
if (findControl == null)
{
return null;
}
Control[] ctlArray = findControl.Controls.Find(controlName, true);
if (ctlArray.Length > 0)
{
return ctlArray.ToList();
}
else
{
return null;
}
}

public static Control FindControlByName(Control findControl, string controlName, ref object values)
{
if (findControl == null)
{
return null;
}
Control ctr = null;
FindControl(findControl, controlName, ref ctr);
if (ctr != null)
{
values = GetControlValue(ctr);
return ctr;
}
values = string.Empty;
return null;
}
#endregion

#region SetInfo 方法 ,可以将页面控件值绑定到实体

/// 获取控件VAlues
/// <summary>
/// 获取控件VAlues
/// </summary>
/// <param name="ctl"></param>
/// <returns></returns>
private static object GetControlValue(Control ctl)
{
if (ctl is TextBox) {
return ((TextBox)ctl).Text.Trim();
}
if (ctl.GetType() == typeof(DevExpress.XtraEditors.DateEdit)) {
return ((DevExpress.XtraEditors.DateEdit)ctl).EditValue;
}
if (ctl.GetType() == typeof(DevExpress.XtraEditors.TextEdit)) {
return ((DevExpress.XtraEditors.TextEdit)ctl).EditValue;
}
if (ctl is UserLUEdit) {
return ((UserLUEdit)ctl).EditValue;
}
//if (ctl is UserLookUpEdit) {
// return ((UserLookUpEdit)ctl).EditValue;
//}
//if (ctl is SearchLookUpEdit) {
// return ((SearchLookUpEdit)ctl).EditValue;
//}
if (ctl is GridLookUpEdit) {
return ((GridLookUpEdit)ctl).EditValue;
}
//if (ctl is TreeListLookUpEdit) {
// return ((TreeListLookUpEdit)ctl).EditValue;
//}
if (ctl is CheckBox) {
return ((CheckBox)ctl).Checked;
}
if (ctl is DateTimePicker) {
return ((DateTimePicker)ctl).Value;
}
if (ctl is NumericUpDown) {
return ((NumericUpDown)ctl).Value;
}
if (ctl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit)) {
return ((DevExpress.XtraEditors.MemoEdit)ctl).EditValue;
}
if (ctl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
return ((DevExpress.XtraEditors.ButtonEdit)ctl).EditValue;
}
if (ctl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit)) {
return ((DevExpress.XtraEditors.CheckEdit)ctl).Checked;
}
return null;
}

//将控件的值传给实体类
/// <summary>
/// 将控件的值传给实体类
/// </summary>
/// <param name="findControl">包含取值控件的容器</param>
/// <param name="myInfo">要赋值的实体</param>
/// <returns>返回赋值完毕的实体</returns>
public static object SetInfo(Control findControl, object myInfo)
{
Control ctl = null;
string controlNames = string.Empty;
string sValue = string.Empty;
PropertyInfo[] pInfos = myInfo.GetType().GetProperties();
foreach (PropertyInfo pInfo in pInfos)
{
controlNames = pInfo.Name;
if (!FindControl(findControl, controlNames, ref ctl))
{
continue;
}
if (ctl is TextBox)
{
sValue = ((TextBox)ctl).Text.Trim();
if (sValue == string.Empty)
{
SetDefaultValue(myInfo, pInfo);
}
else
{
pInfo.SetValue(myInfo, Convert.ChangeType(sValue, pInfo.PropertyType), null);
}
}

else if (ctl is DevExpress.XtraEditors.LookUpEdit )
{
if (((DevExpress.XtraEditors.LookUpEdit)ctl).EditValue == null)
{
pInfo.SetValue(myInfo, null, null);
}
else
{
if (pInfo.PropertyType.Name == "String")
{
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.LookUpEdit)ctl).EditValue.ToString(), null);
}
else
{
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.LookUpEdit)ctl).EditValue, pInfo.PropertyType), null);
}
}
}
else if (ctl is CheckBox)
{
pInfo.SetValue(myInfo, ((CheckBox)ctl).Checked == true ? "Y" : "N", null);
}
else if (ctl is RadioGroup)
{
pInfo.SetValue(myInfo, Convert.ChangeType(((RadioGroup)ctl).EditValue, pInfo.PropertyType), null);
}
else if (ctl is DateTimePicker)
{
if (pInfo.PropertyType.Name == "String")
{
pInfo.SetValue(myInfo, ((DateTimePicker)ctl).Value.ToString(), null);
}
else
{
pInfo.SetValue(myInfo, Convert.ChangeType(((DateTimePicker)ctl).Value, pInfo.PropertyType), null);
}
}

else if (ctl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit))
{
if (pInfo.PropertyType.Name == "String")
{
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.MemoEdit)ctl).EditValue, null);
}
else
{
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.MemoEdit)ctl).EditValue, pInfo.PropertyType), null);
}
}
else if (ctl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit))
{
if (((DevExpress.XtraEditors.ButtonEdit)ctl).EditValue != null)
{
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.ButtonEdit)ctl).EditValue, pInfo.PropertyType), null);
}
}
else if (ctl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit))
{
if (pInfo.PropertyType.Name == "String")
{
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.CheckEdit)ctl).Checked ? "Y" : "N", null);
}
else
{
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.CheckEdit)ctl).Checked, pInfo.PropertyType), null);
}
}
else if (ctl.GetType() == typeof(DevExpress.XtraEditors.TextEdit))
{
if (((DevExpress.XtraEditors.TextEdit)ctl).EditValue != null)
{
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.TextEdit)ctl).EditValue, pInfo.PropertyType), null);
}
}
else if (ctl.GetType() == typeof(DevExpress.XtraEditors.DateEdit))
{
if (((DevExpress.XtraEditors.DateEdit)ctl).EditValue != null)
{
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.DateEdit)ctl).EditValue, pInfo.PropertyType), null);
}
}
}
return myInfo;
}

//将控件的值传给实体类
/// <summary>
/// 将控件的值传给实体类
/// </summary>
/// <param name="findControl">包含控件的容器,从该容器中的控件取值</param>
/// <param name="myInfo">要赋值的实体类</param>
/// <param name="controlCount">控件个数</param>
/// <returns>返回赋值完毕的实体类</returns>
public static object SetInfo(Control findControl, object myInfo, ref int controlCount)
{
Control ctrl = null;
string controlNames = string.Empty;
string sValue = string.Empty;
controlCount = 0;
PropertyInfo[] pInfos = myInfo.GetType().GetProperties();
foreach (PropertyInfo pInfo in pInfos)
{
controlNames = pInfo.Name;
if (!FindControl(findControl, controlNames, ref ctrl))
{
continue;
}
if (ctrl is TextBox)
{
sValue = ((TextBox)ctrl).Text;
if (sValue == string.Empty)
{
if (pInfo.PropertyType.Name == "Int" || pInfo.PropertyType.Name == "Int32" || pInfo.PropertyType.Name == "Int64")
{
pInfo.SetValue(myInfo, int.MinValue, null);
}
else if (pInfo.PropertyType.Name == "Double" || pInfo.PropertyType.Name == "Decimal"
|| pInfo.PropertyType.Name == "Float" || pInfo.PropertyType.Name == "Single")
{
pInfo.SetValue(myInfo, double.MinValue, null);
}
else
{
pInfo.SetValue(myInfo, Convert.ChangeType(sValue, pInfo.PropertyType), null);
}
}
else
{
pInfo.SetValue(myInfo, Convert.ChangeType(sValue, pInfo.PropertyType), null);
controlCount++;
}
}

else if (ctrl is CheckBox)
{
pInfo.SetValue(myInfo, ((CheckBox)ctrl).Checked == true ? "Y" : "N", null);
}
else if (ctrl is NumericUpDown)
{
pInfo.SetValue(myInfo, Convert.ChangeType(((NumericUpDown)ctrl).Value, pInfo.PropertyType), null);
}
else if (ctrl is DateTimePicker)
{

if (pInfo.PropertyType.Name == "String")
{
pInfo.SetValue(myInfo, ((DateTimePicker)ctrl).Value.ToString(), null);
}
else
{
pInfo.SetValue(myInfo, Convert.ChangeType(((DateTimePicker)ctrl).Value, pInfo.PropertyType), null);
}
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit))
{
if (pInfo.PropertyType.Name == "String")
{
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.MemoEdit)ctrl).EditValue, null);
}
else
{
pInfo.SetValue(myInfo, Convert.ChangeType(((DevExpress.XtraEditors.MemoEdit)ctrl).EditValue, pInfo.PropertyType), null);
}
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit))
{
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.ButtonEdit)ctrl).EditValue, null);
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit))
{
if (pInfo.PropertyType == typeof(string))
{
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.CheckEdit)ctrl).Checked ? "Y" : "N", null);
}
else
{
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.CheckEdit)ctrl).Checked, null);
}
}
else if (ctrl is DevExpress.XtraEditors.TextEdit)
{
pInfo.SetValue(myInfo, ((DevExpress.XtraEditors.TextEdit)ctrl).EditValue, null);
}

}

return myInfo;
}

#endregion

#region 实体到控件

/// <summary>
/// 根据实体属性值设置控件值,要求实体属性名与除去前缀的控件命名相同
/// </summary>
/// <param name="myInfo">带数据的实体</param>
/// <param name="findControl">容器控件</param>
public static void SetControlValue(object myInfo, Control findControl)
{
SetControlValue(myInfo, findControl, string.Empty);
}


/// <summary>
/// 根据实体属性值设置控件值,要求实体属性名与除去前缀、后缀的控件命名相同
/// </summary>
/// <param name="myInfo">带数据的实体</param>
/// <param name="findControl">容器,要设置值的控件</param>
/// <param name="strSuffix">关键字 如:txtName_Key 中 _Key )</param>
public static void SetControlValue(object myInfo, Control findControl, string strSuffix)
{
Control ctrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名
PropertyInfo[] pInfos = myInfo.GetType().GetProperties();
object propValue = null; //属性值
string propType = string.Empty; //属性类型
foreach (PropertyInfo pi in pInfos) {
controlName = pi.Name.ToUpper() + strSuffix.ToUpper();
propValue = pi.GetValue(myInfo, null);
propType = pi.PropertyType.Name.ToUpper();
if (propValue == null || IsDefaultValue(propValue)) {
continue;
}
if (!FindControl(findControl, controlName, ref ctrl)) {
continue;
}
if (ctrl is TextBox || ctrl is DevExpress.XtraEditors.TextEdit) {
((TextBox)ctrl).Text = propValue.ToString();
}
else if (ctrl is CheckBox) {
((CheckBox)ctrl).Checked = (propValue.ToString() == "Y" ? true : false);
}
else if (ctrl is NumericUpDown) {
((NumericUpDown)ctrl).Value = Convert.ToDecimal(propValue.ToString());
}
else if (ctrl is RadioGroup) {
((RadioGroup)ctrl).EditValue = propValue.ToString();
}
else if (ctrl is DateTimePicker) {
((DateTimePicker)ctrl).Value = Convert.ToDateTime(propValue.ToString());
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.DateEdit)) {
if (!string.IsNullOrEmpty(propValue.ToString()) && Convert.ToDateTime(propValue) != DateTime.MinValue) {
((DevExpress.XtraEditors.DateEdit)ctrl).EditValue = Convert.ToDateTime(propValue.ToString());
}
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit)) {
((DevExpress.XtraEditors.TextEdit)ctrl).EditValue = propValue.ToString();
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit)) {
((DevExpress.XtraEditors.MemoEdit)ctrl).EditValue = propValue.ToString();
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
((DevExpress.XtraEditors.ButtonEdit)ctrl).EditValue = propValue.ToString();
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit)) {
if (pi.PropertyType == typeof(string)) {
((DevExpress.XtraEditors.CheckEdit)ctrl).Checked = propValue.ToString() == "Y" ? true : false;
}
else {
((DevExpress.XtraEditors.CheckEdit)ctrl).Checked = Convert.ToBoolean(propValue.ToString());
}
}
else if (ctrl is UserLUEdit) {
if (!string.IsNullOrEmpty(propValue.ToString())) {
((UserLUEdit)ctrl).EditValue = Convert.ChangeType(propValue.ToString(), pi.PropertyType);
}
}
//else if (ctrl is UserLookUpEdit) {
// if (!string.IsNullOrEmpty(propValue.ToString())) {
// ((UserLookUpEdit)ctrl).EditValue = Convert.ChangeType(propValue.ToString(), pi.PropertyType);
// }
//}
//else if (ctrl is SearchLookUpEdit) {
// if (!string.IsNullOrEmpty(propValue.ToString())) {
// ((SearchLookUpEdit)ctrl).EditValue = Convert.ChangeType(propValue.ToString(), pi.PropertyType);
// }
//}
else if (ctrl is GridLookUpEdit) {
if (!string.IsNullOrEmpty(propValue.ToString())) {
((GridLookUpEdit)ctrl).EditValue = Convert.ChangeType(propValue.ToString(), pi.PropertyType);
}
}
//else if (ctrl is TreeListLookUpEdit) {
// if (!string.IsNullOrEmpty(propValue.ToString())) {
// ((TreeListLookUpEdit)ctrl).EditValue = Convert.ChangeType(propValue.ToString(), pi.PropertyType);
// }
//}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LookUpEdit)) {
if (!string.IsNullOrEmpty(propValue.ToString())) {
((DevExpress.XtraEditors.LookUpEdit)ctrl).EditValue = Convert.ChangeType(propValue.ToString(), pi.PropertyType);
}
}
}
}
#endregion

#region 实体绑定到控件(DataBinding)
// 根据实体属性值 绑定 控件属性值,要求实体属性名与除去前缀的控件命名相同
/// <summary>
/// 根据实体属性值 绑定 控件属性值,要求实体属性名与除去前缀的控件命名相同
/// </summary>
/// <param name="myInfo">带数据的实体</param>
/// <param name="findControl">容器控件</param>
public static void EntityToControlDataBing(object myInfo, Control findControl)
{
EntityToControlDataBing(myInfo, findControl, string.Empty);
}


// 根据实体属性值 绑定 控件属性值,要求实体属性名与除去前缀 或 后缀的控件命名相同
/// <summary>
/// 根据实体属性值 绑定 控件属性值,要求实体属性名与除去前缀 或 后缀的控件命名相同
/// </summary>
/// <param name="myInfo">带数据的实体</param>
/// <param name="findControl">容器,要设置值的控件</param>
/// <param name="strSuffix">关键字 如:txtName_Key 中 _Key </param>
public static void EntityToControlDataBing(object myInfo, Control findControl, string SuffixKey)
{
Control ctrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名
if (myInfo == null) return;
PropertyInfo[] pInfos = myInfo.GetType().GetProperties();
string propType = string.Empty; //属性类型
foreach (PropertyInfo pi in pInfos) {
controlName = pi.Name + SuffixKey;

if (!FindControl(findControl, controlName, ref ctrl)) {
continue;
}
ctrl.DataBindings.Clear();
if (ctrl is TextBox) {
ctrl.DataBindings.Add("Text", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl is CheckBox) {

ctrl.DataBindings.Add("Checked", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl is NumericUpDown) {

ctrl.DataBindings.Add("Value", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl is RadioGroup) {

ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

}
else if (ctrl is DateTimePicker) {

ctrl.DataBindings.Add("Value", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.DateEdit)) {

ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit)) {

ctrl.DataBindings.Add("Text", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit)) {

ctrl.DataBindings.Add("Text", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit)) {
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit)) {

ctrl.DataBindings.Add("Checked", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl is UserLUEdit) {
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

}
//else if ( ctrl is UserLookUpEdit ) {
// ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

//}
//else if (ctrl is SearchLookUpEdit) {
// ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

//}
else if (ctrl is GridLookUpEdit) {
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

}
//else if (ctrl is TreeListLookUpEdit) {
// ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);

//}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LookUpEdit)) {
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
//else if (ctrl is UIBase.ucYearMonth) {
// ctrl.DataBindings.Add("YearMonth", myInfo, pi.Name, true, DataSourceUpdateMode.OnValidation);
//}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CalcEdit)) {
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ComboBoxEdit)) {
ctrl.DataBindings.Add("EditValue", myInfo, pi.Name, true, DataSourceUpdateMode.OnPropertyChanged);
}
}
}
#endregion

#region 控件设置为 只读 状态(ReadOnly)

// 设置容器控件中 子控件 ReadOnly状态
/// <summary>
/// 设置容器控件中 子控件 ReadOnly状态
/// </summary>
/// <param name="findControl"> 容器控件</param>
/// <param name="ReadOnlyState"> ReadOnly状态 </param>
public static void SetControlReadOnly(Control findControl, bool ReadOnlyState)
{
if (findControl == null) return;
foreach (Control ctrl in findControl.Controls)
{
SetControlState(ctrl, ReadOnlyState);
if (ctrl.Controls.Count > 0)
{
if (ctrl is DevExpress.XtraGrid.GridControl) continue;

SetControlReadOnly(ctrl, ReadOnlyState);
}
}
}

public static void SetControlsVisable(List<Control> Controls, bool VisableState)
{
if (Controls == null) return;
foreach (Control ctrl in Controls)
{
SetControlVisable(ctrl, VisableState);
}
}

public static void SetControlVisable(Control findControl, bool VisableState)
{
if (findControl == null) return;
SetControlVisibleStatus(findControl, VisableState);
foreach (Control ctrl in findControl.Controls)
{
SetControlVisibleStatus(ctrl, VisableState);
if (ctrl.Controls.Count > 0)
{
if (ctrl is DevExpress.XtraGrid.GridControl) continue;

SetControlVisable(ctrl, VisableState);
}
}
}
// 设置容器中具体某个控件 ReadOnly状态
/// <summary>
/// 设置容器中具体某个控件 ReadOnly状态
/// </summary>
/// <param name="ProName">对应实体字段名</param>
/// <param name="findControl"></param>
/// <param name="strSuffix">关键字 如:txtName_Key 中 _Key </param>
/// <param name="ReadOnlyState">ReadOnly状态</param>
public static void SetSpecificControlReadOnly(string ProName, Control findControl, bool ReadOnlyState, string strSuffix = "")
{
Control ctrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名
controlName = ProName + strSuffix.ToUpper();
if (!FindControl(findControl, controlName, ref ctrl))
{
return;
}
SetControlState(ctrl, ReadOnlyState);
}


static Control ctrl = null;



// 设置容器中具体某个控件 ReadOnly状态(遍历所有子控件)
/// <summary>
/// 设置容器中具体某个控件 ReadOnly状态(遍历所有子控件)
/// </summary>
/// <param name="ProName">>对应实体字段名</param>
/// <param name="findControl">父控件</param>
/// <param name="ReadOnlyState"></param>
/// <param name="strSuffix">关键字 如:txtName_Key 中 _Key</param>
/// <param name="CantainGrid">GridView 列是否搜索 </param>
public static void SetSpecificControlState(string ProName, Control findControl, bool ReadOnlyState,string strSuffix = "",bool CantainGrid=false)
{

ctrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名

controlName = ProName + strSuffix.ToUpper();
if (findControl.Controls.Count == 0) return;
if (!FindControl(findControl, controlName, ref ctrl))
{
foreach (Control col in findControl.Controls)
{
if (col.Controls.Count > 0)
{
if (FindControl(col, controlName, ref ctrl))
{
SetControlState(ctrl, ReadOnlyState);
return;
}
else
{
if (col is DevExpress.XtraGrid.GridControl && CantainGrid)
{
DevExpress.XtraGrid.GridControl grid = (DevExpress.XtraGrid.GridControl)col;
DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)grid.ViewCollection[0];
foreach (DevExpress.XtraGrid.Columns.GridColumn cl in view.Columns)
{
if (cl.Name == controlName || cl.FieldName==controlName)
{
cl.OptionsColumn.ReadOnly =ReadOnlyState;
return;
}
}
}
SetSpecificControlState(ProName, col, ReadOnlyState, strSuffix);
}
}
}
}
else
{
SetControlState(ctrl, ReadOnlyState);
return;
}

}

private static void SetControlState(Control ctrl,bool ReadOnlyState)
{
if (ctrl is TextBox)
{
((TextBox)ctrl).ReadOnly = ReadOnlyState;
}
else if (ctrl is CheckBox)
{
((CheckBox)ctrl).Enabled = !ReadOnlyState;
}
else if (ctrl is NumericUpDown)
{
((NumericUpDown)ctrl).ReadOnly = ReadOnlyState;
}
else if (ctrl is RadioGroup)
{
((RadioGroup)ctrl).Properties.ReadOnly = ReadOnlyState;
}
else if (ctrl is DateTimePicker)
{
((DateTimePicker)ctrl).Enabled = !ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.DateEdit))
{
((DateEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit))
{
((TextEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit))
{
((MemoEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit))
{
((ButtonEdit)ctrl).Enabled = !ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit))
{
((CheckEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}

else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LookUpEdit))
{
((DevExpress.XtraEditors.LookUpEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit))
{
((DevExpress.XtraEditors.ButtonEdit)ctrl).Enabled = !ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CalcEdit))
{
((DevExpress.XtraEditors.CalcEdit)ctrl).Properties.ReadOnly = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit))
{
((DevExpress.XtraEditors.ButtonEdit)ctrl).Enabled = !ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.SimpleButton))
{
((DevExpress.XtraEditors.SimpleButton)ctrl).Enabled = !ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.PopupContainerEdit))
{
((DevExpress.XtraEditors.PopupContainerEdit)ctrl).Enabled = !ReadOnlyState;
}
}


private static void SetControlsVisibleStatus(List<Control> ctrllist, bool VisibleState)
{
if (ctrllist == null)
{ return; }
foreach (Control c in ctrllist)
{
SetControlVisibleStatus(c, VisibleState);
}
}
private static void SetControlVisibleStatus(Control ctrl, bool VisibleState)
{
if (ctrl is TextBox)
{
((TextBox)ctrl).Visible = VisibleState;
}
else if (ctrl is CheckBox)
{
((CheckBox)ctrl).Visible = VisibleState;
}
else if (ctrl is Label)
{
((Label)ctrl).Visible = VisibleState;
}
else if (ctrl is NumericUpDown)
{
((NumericUpDown)ctrl).Visible = VisibleState;
}
else if (ctrl is RadioGroup)
{
((RadioGroup)ctrl).Visible = VisibleState;
}
else if (ctrl is DateTimePicker)
{
((DateTimePicker)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.DateEdit))
{
((DevExpress.XtraEditors.DateEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit))
{
((DevExpress.XtraEditors.TextEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit))
{
((MemoEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit))
{
((ButtonEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit))
{
((CheckEdit)ctrl).Visible = VisibleState;
}

else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LookUpEdit))
{
((DevExpress.XtraEditors.LookUpEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit))
{
((DevExpress.XtraEditors.ButtonEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CalcEdit))
{
((DevExpress.XtraEditors.CalcEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraGrid.GridControl))
{
((DevExpress.XtraGrid.GridControl)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.SimpleButton))
{
((DevExpress.XtraEditors.SimpleButton)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LabelControl))
{
((DevExpress.XtraEditors.LabelControl)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit))
{
((DevExpress.XtraEditors.TextEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LabelControl))
{
((DevExpress.XtraEditors.LabelControl)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit))
{
((DevExpress.XtraEditors.ButtonEdit)ctrl).Visible = VisibleState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.SimpleButton))
{
((DevExpress.XtraEditors.SimpleButton)ctrl).Visible = VisibleState;
}
}
#endregion

#region 控件设置为(Enabled)
// 设置容器中具体某个控件 Enabled状态(遍历所有子控件)
/// <summary>
/// 设置容器中具体某个控件 Enabled状态(遍历所有子控件)
/// </summary>
/// <param name="ProName">>对应实体字段名</param>
/// <param name="findControl">父控件</param>
/// <param name="ReadOnlyState"></param>
/// <param name="strSuffix">关键字 如:txtName_Key 中 _Key</param>
/// <param name="CantainGrid">GridView 列是否搜索 </param>
public static void SetSpecificControlEnabled(string ProName, Control findControl, bool ReadOnlyState, string strSuffix = "", bool CantainGrid = false)
{

ctrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名

controlName = ProName + strSuffix.ToUpper();
if (findControl.Controls.Count == 0) return;
if (!FindControl(findControl, controlName, ref ctrl))
{
foreach (Control col in findControl.Controls)
{
if (col.Controls.Count > 0)
{
if (FindControl(col, controlName, ref ctrl))
{
SetControlToEnabled(ctrl, ReadOnlyState);
return;
}
else
{
if (col is DevExpress.XtraGrid.GridControl && CantainGrid)
{
DevExpress.XtraGrid.GridControl grid = (DevExpress.XtraGrid.GridControl)col;
DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)grid.ViewCollection[0];
foreach (DevExpress.XtraGrid.Columns.GridColumn cl in view.Columns)
{
if (cl.Name == controlName || cl.FieldName == controlName)
{
cl.OptionsColumn.ReadOnly = ReadOnlyState;
return;
}
}
}
SetSpecificControlState(ProName, col, ReadOnlyState, strSuffix);
}
}
}
}
else
{
SetControlToEnabled(ctrl, ReadOnlyState);
return;
}

}

private static void SetControlToEnabled(Control ctrl, bool ReadOnlyState)
{
if (ctrl is TextBox)
{
((TextBox)ctrl).Enabled = ReadOnlyState;
}
else if (ctrl is CheckBox)
{
((CheckBox)ctrl).Enabled = ReadOnlyState;
}
else if (ctrl is NumericUpDown)
{
((NumericUpDown)ctrl).Enabled = ReadOnlyState;
}
else if (ctrl is RadioGroup)
{
((RadioGroup)ctrl).Properties.Enabled = ReadOnlyState;
}
else if (ctrl is DateTimePicker)
{
((DateTimePicker)ctrl).Enabled = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.DateEdit))
{
((DateEdit)ctrl).Properties.Enabled = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit))
{
((TextEdit)ctrl).Properties.Enabled = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit))
{
((MemoEdit)ctrl).Properties.Enabled = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit))
{
((ButtonEdit)ctrl).Enabled = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit))
{
((CheckEdit)ctrl).Properties.Enabled = ReadOnlyState;
}

else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LookUpEdit))
{
((DevExpress.XtraEditors.LookUpEdit)ctrl).Properties.Enabled = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit))
{
((DevExpress.XtraEditors.ButtonEdit)ctrl).Enabled = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CalcEdit))
{
((DevExpress.XtraEditors.CalcEdit)ctrl).Properties.Enabled = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.ButtonEdit))
{
((DevExpress.XtraEditors.ButtonEdit)ctrl).Enabled = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.SimpleButton))
{
((DevExpress.XtraEditors.SimpleButton)ctrl).Enabled = ReadOnlyState;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.PopupContainerEdit))
{
((DevExpress.XtraEditors.PopupContainerEdit)ctrl).Enabled = ReadOnlyState;
}
}

#endregion

#region 控件设置为控件交互状态(EnabledState)
/// <summary>
///设置容器控件中 子控件 Enabled交互状态
/// </summary>
/// <param name="findControl">容器控件 </param>
/// <param name="EnableState"> 交互状态</param>
public static void SetControlEnabled(Control findControl, bool EnableState)
{
if (findControl == null) return;
foreach (Control ctrl in findControl.Controls)
{
if (ctrl is Label || ctrl is LabelControl)
continue;
ctrl.Enabled = EnableState;
if (ctrl.Controls.Count > 0)
{
SetControlEnabled(ctrl, EnableState);
}
}
}

#endregion

/// 设置控件为空值
/// <summary>
/// 设置控件为空值
/// </summary>
/// <param name="ctrl"></param>
public static void SetControlValueEmpty(Control ctrl)
{
if (ctrl == null) return;
foreach (Control ch in ctrl.Controls)
{
if (ch is Label || ch is LabelControl)
continue;
SetValueEmpty(ch);

}
}
private static void SetValueEmpty(Control ctrl)
{
if (ctrl is TextBox)
{
((TextBox)ctrl).Text="";
}
else if (ctrl is CheckBox)
{
((CheckBox)ctrl).Checked = false ;
}
else if (ctrl is NumericUpDown)
{
((NumericUpDown)ctrl).Value =0;
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.TextEdit))
{
((TextEdit)ctrl).Text = "";
}
else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.MemoEdit))
{
((MemoEdit)ctrl).Text="";
}

else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit))
{
((CheckEdit)ctrl).Checked=false;
}

else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.LookUpEdit))
{
((DevExpress.XtraEditors.LookUpEdit)ctrl).EditValue=null;
}

else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CalcEdit))
{
((DevExpress.XtraEditors.CalcEdit)ctrl).Value=0;
}
}


///判断给定对象是否是默认值
/// <summary>
/// 方法说明:判断给定对象是否是默认值
/// </summary>
/// <param name="value">待检测的对象</param>
/// <returns>是默认值返回true,否则返回false</returns>
public static bool IsDefaultValue(object value)
{
if (value == null)
{
return true;
}

if (value.GetType().Equals(typeof(bool)))
{
return false;
}

return value.Equals(GetDefaultValue(value.GetType()));
}

/// 方法说明:获取类型的默认值
/// <summary>
/// 方法说明:获取类型的默认值
/// </summary>
/// <param name="type">类型</param>
/// <returns>默认值</returns>
public static object GetDefaultValue(Type type)
{
if (type.Equals(typeof(bool)))
{
return false;
}

if (type.Equals(typeof(string)))
{
return null;
}
FieldInfo minValueField = type.GetField("MinValue");
if (minValueField != null)
{
return minValueField.GetValue(null);
}

return null;
}

///方法说明:设置实体成员的默认值
/// <summary>
/// 方法说明:设置实体成员的默认值
/// </summary>
/// <param name="entity">实体</param>
/// <param name="member">实体成员信息</param>
public static void SetDefaultValue(object entity, MemberInfo member)
{
if ((entity == null) || (member == null))
{
return;
}
switch (member.MemberType)
{
case MemberTypes.Field:
FieldInfo field = member as FieldInfo;
if (field != null)
{
field.SetValue(entity,GetDefaultValue(field.FieldType));
}
break;
case MemberTypes.Property:
PropertyInfo property = member as PropertyInfo;
if (property != null)
{
property.SetValue(entity, GetDefaultValue(property.PropertyType), null);
}
break;
default:
break;
}
}

public static void GetAllControls(Control.ControlCollection ctrls, ref List<Control> returnctls)
{
if (returnctls == null || returnctls.Count < 0)
{
returnctls = new List<Control>();
}
foreach(Control c in ctrls)
{
returnctls.Add(c);
if (c.HasChildren)
{
GetAllControls(c.Controls,ref returnctls);
}
}
}
static List<Control> ctrllist = null;

/// 设置容器中具体某个控件 Visible状态(遍历所有子控件)
/// <summary>
/// 设置容器中具体某个控件 Visible状态(遍历所有子控件)
/// </summary>
/// <param name="ProName">>对应实体字段名</param>
/// <param name="findControl">父控件</param>
/// <param name="ReadOnlyState"></param>
/// <param name="strSuffix">关键字 如:txtName_Key 中 _Key</param>
/// <param name="CantainGrid">GridView 列是否搜索 </param>
public static void SetVisibleControlStateA(string ProName, Control findControl, bool VisibleState, string strSuffix = "", bool CantainGrid = false)
{
ctrllist = null;
ctrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名

controlName = ProName + strSuffix.ToUpper();
if (findControl.Controls.Count == 0)
{
if (findControl.Name == ProName)
{
SetControlVisibleStatus(findControl, VisibleState);
return;
}
}
else
if (findControl.Name == ProName)
{
SetControlVisibleStatus(findControl, VisibleState);
return;
}
if (!FindControlList(findControl, controlName, ref ctrllist))
{
foreach (Control col in findControl.Controls)
{
if (col.Controls.Count > 0)
{
if (FindControlList(col, controlName, ref ctrllist))
{
SetControlsVisibleStatus(ctrllist, VisibleState);
return;
}
else
{
if (col is DevExpress.XtraGrid.GridControl && CantainGrid)
{
DevExpress.XtraGrid.GridControl grid = (DevExpress.XtraGrid.GridControl)col;
DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)grid.ViewCollection[0];
foreach (DevExpress.XtraGrid.Columns.GridColumn cl in view.Columns)
{
if (cl.Name == controlName || cl.FieldName == controlName)
{
cl.Visible = VisibleState;
return;
}
}
}
SetVisibleControlState(ProName, col, VisibleState, strSuffix);
}
}
}
}
else
{
SetControlsVisable(ctrllist, VisibleState);
return;
}

}


public static void SetVisibleControlState(string ProName, Control findControl, bool VisibleState, string strSuffix = "", bool CantainGrid = false)
{
ctrllist = null;
ctrl = null; //保存在容器中查找到的控件
string controlName = string.Empty; //保存控件名

controlName = ProName + strSuffix.ToUpper();
if (findControl.Controls.Count == 0)
{
if (findControl.Name == ProName)
{
SetControlVisibleStatus(findControl, VisibleState);
return;
}
}
else
if (findControl.Name == ProName)
{
SetControlVisibleStatus(findControl, VisibleState);
return;
}
if (findControl is DevExpress.XtraGrid.GridControl && CantainGrid)
{
DevExpress.XtraGrid.GridControl grid = (DevExpress.XtraGrid.GridControl)findControl;
DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)grid.ViewCollection[0];
foreach (DevExpress.XtraGrid.Columns.GridColumn cl in view.Columns)
{
if (cl.Name == controlName || cl.FieldName.ToUpper() == controlName.ToUpper())
{
cl.Visible = VisibleState;
}
}
}
if (FindControlList(findControl, controlName, ref ctrllist))
{
foreach (Control c in ctrllist)
{
SetControlVisibleStatus(c, VisibleState);
}
}
else
{
return;
}

 

}

}
}

posted @ 2018-07-16 09:54  执念、旧时光  阅读(1573)  评论(0编辑  收藏  举报