DevExpress常用组件操作扩展类:DevExpressExtensions.cs
C# 全选
/// <summary>
/// DevExpress组件扩展方法
/// </summary>
public static class DevExpressExtensions
{
/// <summary>
/// 获取LookupEdit选择的行对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="lue"></param>
/// <returns></returns>
public static T GetSelectedRow<T>(this LookUpEdit lue) where T : class
{
return (T)lue.Properties.GetDataSourceRowByKeyValue(lue.EditValue);
}
/// <summary>
/// 禁用/启用用户自定义按钮
/// </summary>
/// <param name="gc"></param>
/// <param name="buttonTag"></param>
/// <param name="enable"></param>
public static void SetCustomButtonEnable(this GridControl gc, string buttonTag, bool enable)
{
var count = gc.EmbeddedNavigator.Buttons.CustomButtons.Count;
for (var i = 0; i <= count - 1; i++)
{
if (gc.EmbeddedNavigator.Buttons.CustomButtons[i].Tag.ToStringEx().ToLower() == buttonTag.ToLower())
{
gc.EmbeddedNavigator.Buttons.CustomButtons[i].Enabled = enable;
break;
}
}
}
/// <summary>
/// 禁用/启用用户自定义按钮
/// </summary>
/// <param name="gv"></param>
/// <param name="buttonTag"></param>
/// <param name="enable"></param>
public static void SetCustomButtonEnable(this GridView gv, string buttonTag, bool enable)
{
SetCustomButtonEnable(gv.GridControl, buttonTag, enable);
}
/// <summary>
/// 删除表格列
/// </summary>
/// <param name="view"></param>
/// <param name="fieldName"></param>
public static void RemoveColumn(this GridView view, string fieldName)
{
var col = view.Columns[fieldName];
if (col != null) view.Columns.Remove(col);
}
/// <summary>
/// 获取当前行对象实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="view"></param>
/// <returns></returns>
public static T GetObject<T>(this GridView view) where T : class
{
return (T)view.GetRow(view.FocusedRowHandle);
}
/// <summary>
/// 获取指定行的对象实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="view"></param>
/// <param name="rowHandle"></param>
/// <returns></returns>
public static T GetRow<T>(this GridView view, int rowHandle) where T : class
{
return (T)view.GetRow(rowHandle);
}
/// <summary>
/// 获取当前行对象实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="view"></param>
/// <returns></returns>
public static T GetRow<T>(this GridView view) where T : class
{
return (T)view.GetRow(view.FocusedRowHandle);
}
/// <summary>
/// 获取指定行对象实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="view"></param>
/// <param name="rowHandle"></param>
/// <returns></returns>
public static T GetObject<T>(this GridView view, int rowHandle) where T : class
{
return (T)view.GetRow(rowHandle);
}
/// <summary>
/// 获取勾选行的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="control">复合勾选组件</param>
/// <param name="valueMemeber">取值的属性名称,如:CustomerCode</param>
/// <param name="split"></param>
/// <returns></returns>
public static string GetEditValue<T>(this CheckedListBoxControl control, string valueMemeber, string split = ",")
{
StringBuilder sb = new StringBuilder();
if (control.DataSource is IList list)
{
foreach (T v in control.CheckedItems)
{
var value = ObjectHelper.GetValueOfObject(v, valueMemeber);
sb.Append(value + split);
}
}
return sb.ToString();
}
/// <summary>
/// 获取勾选的行有效行号(RowHandle>=0),行号去重。
/// </summary>
/// <param name="gv"></param>
/// <returns></returns>
public static int[] GetSelectedRowHandles(this GridView gv)
{
var rows = gv.GetSelectedRows();
return rows.Where(w => w >= 0).Distinct().ToArray();
}
/// <summary>
/// 获取勾选行的指定列的值
/// </summary>
/// <typeparam name="T">实体模型</typeparam>
/// <param name="gv">表格组件</param>
/// <param name="func">取值函数</param>
/// <returns></returns>
public static List<String> GetCheckedKeys<T>(this GridView gv, Func<T, String> func)
where T : class
{
List<String> list = new List<string>();
if (gv.SelectedRowsCount > 0)
{
foreach (int i in gv.GetSelectedRowHandles())
{
list.Add(func((T)gv.GetRow(i)));
}
}
return list;
}
/// <summary>
/// 获取勾选行对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="gv"></param>
/// <returns></returns>
public static List<T> GetCheckedRows<T>(this GridView gv)
where T : class
{
List<T> list = new List<T>();
if (gv.SelectedRowsCount > 0)
{
foreach (int i in gv.GetSelectedRowHandles())
{
list.Add((T)gv.GetRow(i));
}
}
return list;
}
/// <summary>
/// 表格组件添加一条对象实体记录
/// </summary>
/// <param name="view">表格组件</param>
/// <param name="row">对象实体</param>
/// <param name="refreshDataSource">添加记录后立即刷新数据源</param>
/// <returns></returns>
public static int AddRow(this GridView view, object row, bool refreshDataSource = true)
{
//获取数据源
var list = GetDataSourceList(view);
//数据源添加对象实体
list.Add(row);
int index = list.IndexOf(row);
//刷新表格数据源
if (refreshDataSource)
{
view.GridControl.RefreshDataSource();
view.FocusedRowHandle = view.GetRowHandle(index);
}
return index;
}
/// <summary>
/// 表格组件添加多条对象实体记录
/// </summary>
/// <param name="view"></param>
/// <param name="rows"></param>
/// <param name="refreshDataSource"></param>
/// <returns></returns>
public static int AddRows(this GridView view, IList rows, bool refreshDataSource = true)
{
if (rows.Count == 0) return -1;
//获取数据源
var list = GetDataSourceList(view);
//数据源添加对象实体
for (int i = 0; i <= rows.Count - 1; i++) list.Add(rows[i]);
//获取数据源序号
var index = list.IndexOf(rows[0]);
//刷新表格数据源
if (refreshDataSource)
{
view.GridControl.RefreshDataSource();
view.FocusedRowHandle = view.GetRowHandle(index);
}
return index;
}
/// <summary>
/// 获取表格数据源
/// </summary>
/// <param name="view"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static IList GetDataSourceList(this GridView view)
{
if (view.GridControl.DataSource is IList list)
return list;
else if (view.GridControl.DataSource is IListSource listSource)
return listSource.GetList();
else if (view.GridControl.DataSource == null)
throw new Exception(view.GridControl.Name + "表格组件没有初始化数据源!");
else
throw new Exception(view.GridControl.Name + "表格组件数据源不支持,请扩展GetDataSourceList接口!");
}
/// <summary>
/// 删除所有记录
/// </summary>
/// <param name="view">表格组件</param>
/// <param name="refreshDataSource">刷新数据源</param>
/// <returns></returns>
public static int DeleteAllRows(this GridView view, bool refreshDataSource = true)
{
//获取数据源
var list = GetDataSourceList(view);
//清空数据源
list.Clear();
//刷新表格数据源
if (refreshDataSource)
{
view.GridControl.RefreshDataSource();
}
return list.Count;
}
/// <summary>
/// 表格插入对象记录
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="view">表格组件</param>
/// <param name="row">对象实体</param>
/// <param name="refreshDataSource">刷新数据源</param>
/// <returns></returns>
public static int InsertRow<T>(this GridView view, T row, bool refreshDataSource = true) where T : class
{
//获取数据源
var list = GetDataSourceList(view);
int handle = view.FocusedRowHandle;
int index = 0;
if (handle >= 0)
{
int i = list.IndexOf(view.GetFocusedRow());
list.Insert(i, row); //数据源插入对象实体
index = list.IndexOf(row);
}
else
{
index = list.Add(row);//数据源添加对象实体
}
//刷新表格数据源
if (refreshDataSource)
{
view.GridControl.RefreshDataSource();
view.FocusedRowHandle = view.GetRowHandle(index);
}
return index;
}
/// <summary>
/// 自动设置CheckedListBoxControl组件的勾选的项目。通过 ValueMember 绑定的属性名称取值。
/// 该方法对绑定数据源的列表组件有效。
/// </summary>
/// <param name="ctl">CheckedListBoxControl组件</param>
/// <param name="values">值,如:A,B,C,多个值分隔符分开</param>
/// <param name="split">默认为调号分隔符</param>
public static void SetItemChecked(this CheckedListBoxControl ctl, string values, string split = ",")
{
var arr = (values ?? "").Split(new string[] { split }, StringSplitOptions.RemoveEmptyEntries);
var list = ctl.DataSource as IList;
for (int i = 0; i <= list.Count - 1; i++)
{
var v = ObjectHelper.GetValueOfObject(list[i], ctl.ValueMember).ToString();
ctl.SetItemChecked(i, arr.Contains(v));
}
}
/// <summary>
/// 获取CheckedListBoxControl组件勾选的值,如勾选多个项,返回A,B,C。
/// 通过 ValueMember 绑定的属性名称取值。
/// </summary>
/// <param name="ctl">CheckedListBoxControl组件</param>
/// <param name="split">默认为调号分隔符</param>
/// <returns></returns>
public static string GetEditValue(this CheckedListBoxControl ctl, string split = ",")
{
StringBuilder sb = new StringBuilder();
var list = ctl.DataSource as IList;
for (int i = 0; i <= list.Count - 1; i++)
{
if (ctl.GetItemChecked(i))
{
var v = ObjectHelper.GetValueOfObject(list[i], ctl.ValueMember).ToString();
sb.Append(v + split);
}
}
return sb.ToString();
}
/// <summary>
/// 获取勾选项的值
/// </summary>
/// <param name="ctl"></param>
/// <param name="split"></param>
/// <returns></returns>
public static string GetCheckedValue(this CheckedListBoxControl ctl, string split = ",")
{
return GetEditValue(ctl, split);
}
/// <summary>
/// 获取表格组件的数据源
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="view"></param>
/// <returns></returns>
public static List<T> GetDataSource<T>(this GridView view) where T : class
{
return view.GridControl.DataSource as List<T>;
}
/// <summary>
/// 获取表格组件的数据源
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="gc"></param>
/// <returns></returns>
public static List<T> GetDataSource<T>(this GridControl gc) where T : class
{
return gc.DataSource as List<T>;
}
/// <summary>
/// 若当前记录的实例类型是T类型,返回当前记录,否则,转换为T类型并返回。
/// 适用场景:表格的数据源是List<res_T>, 转换为T。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="view"></param>
/// <returns></returns>
public static T GetObjectAs<T>(this ISummaryView view) where T : class
{
var obj = view.GetRow(view.FocusedRowHandle);
if (obj is T)
return (T)obj;
else
return ObjectHelper.Convert<T>(obj);
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网