|
@@ -1,7 +1,6 @@
|
|
|
using DevExpress.Utils;
|
|
|
using DevExpress.Utils.Helpers;
|
|
|
using DevExpress.Utils.Menu;
|
|
|
-using DevExpress.Utils.Paint;
|
|
|
using DevExpress.Utils.Svg;
|
|
|
using DevExpress.XtraBars;
|
|
|
using DevExpress.XtraEditors;
|
|
@@ -10,15 +9,19 @@ using DevExpress.XtraEditors.Repository;
|
|
|
using DevExpress.XtraGrid;
|
|
|
using DevExpress.XtraGrid.Columns;
|
|
|
using DevExpress.XtraGrid.Views.Grid;
|
|
|
+using DevExpress.XtraRichEdit.Model;
|
|
|
using DxHelper;
|
|
|
+using ExtensionsDev;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.ComponentModel;
|
|
|
using System.Drawing;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Windows.Forms;
|
|
|
+
|
|
|
public static class GridControlEx
|
|
|
{
|
|
|
internal class GridTag
|
|
@@ -41,21 +44,119 @@ public static class GridControlEx
|
|
|
/// <param name="showScrollH">是否显示水平滚动条(默认不显示)</param>
|
|
|
/// <typeparam name="T">绑定的数据源模型类型</typeparam>
|
|
|
/// <returns></returns>
|
|
|
- public static GridControl UseDefault<T>(this GridControl grid, List<T> dataSource, int rowHeight = 24, bool showScrollH = false)
|
|
|
+ public static GridControl UseDefault<T>(this GridControl grid, List<T> dataSource, int rowHeight = 24, bool showScrollH = false, bool formatNullValue = true)
|
|
|
{
|
|
|
grid.DataSource = dataSource;
|
|
|
- grid.HideColumn("ID","CreateTime","UpdateTime");
|
|
|
+ grid.Load += (sender, e) =>
|
|
|
+ {
|
|
|
+ grid.HideColumn("UpdateTime");
|
|
|
+ grid.HideColumn(p => p.FieldName.ToUpper().EndsWith("ID"));
|
|
|
+ grid.HideColumn(p => p.ColumnType == typeof(List<>));
|
|
|
+ grid.HideColumn(p => p.ColumnType == typeof(ICollection<>));
|
|
|
+ grid.HideColumn(p => p.ColumnType.BaseType != null && p.ColumnType.BaseType.Name.Contains("BaseEntity"));
|
|
|
+ };
|
|
|
+
|
|
|
grid.MainView.BorderStyle = BorderStyles.Flat;
|
|
|
var view = grid.MainView as GridView;
|
|
|
+ SetViewDefaultProps(grid, view, rowHeight, showScrollH);
|
|
|
+ var childView = grid.GetChildView();
|
|
|
+ if (childView != null)
|
|
|
+ {
|
|
|
+ view.OptionsView.ShowDetailButtons = true;
|
|
|
+ SetViewDefaultProps(grid, childView, rowHeight, showScrollH);
|
|
|
+ childView.OptionsMenu.EnableColumnMenu = false;
|
|
|
+ childView.OptionsCustomization.AllowSort = false;
|
|
|
+ childView.OptionsCustomization.AllowFilter = false;
|
|
|
+ childView.OptionsCustomization.AllowGroup = false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ grid.Tag = new GridTag();
|
|
|
+ if (formatNullValue)
|
|
|
+ grid.UseNullValueText();
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 初始化表格(默认禁用分组)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="grid"></param>
|
|
|
+ /// <param name="dataSource">绑定的数据源</param>
|
|
|
+ /// <param name="rowHeight">行高(默认24px)</param>
|
|
|
+ /// <param name="showScrollH">是否显示水平滚动条(默认不显示)</param>
|
|
|
+ /// <typeparam name="T">绑定的数据源模型类型</typeparam>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static GridControl UseDefaultEx<T>(this GridControl grid, BindingList<T> dataSource, int rowHeight = 24, bool showScrollH = false, bool formatNullValue = true)
|
|
|
+ {
|
|
|
+ grid.DataSource = dataSource;
|
|
|
+ grid.Load += (sender, e) =>
|
|
|
+ {
|
|
|
+ grid.HideColumn("UpdateTime");
|
|
|
+ grid.HideColumn(p => p.FieldName.ToUpper().EndsWith("ID"));
|
|
|
+ grid.HideColumn(p => p.ColumnType == typeof(List<>));
|
|
|
+ grid.HideColumn(p => p.ColumnType == typeof(ICollection<>));
|
|
|
+ grid.HideColumn(p => p.ColumnType.BaseType != null && p.ColumnType.BaseType.Name.Contains("BaseEntity"));
|
|
|
+ };
|
|
|
+
|
|
|
+ grid.MainView.BorderStyle = BorderStyles.Flat;
|
|
|
+ var view = grid.MainView as GridView;
|
|
|
+ SetViewDefaultProps(grid, view, rowHeight, showScrollH);
|
|
|
+ var childView = grid.GetChildView();
|
|
|
+ if (childView != null)
|
|
|
+ {
|
|
|
+ view.OptionsView.ShowDetailButtons = true;
|
|
|
+ SetViewDefaultProps(grid, childView, rowHeight, showScrollH);
|
|
|
+ childView.OptionsMenu.EnableColumnMenu = false;
|
|
|
+ childView.OptionsCustomization.AllowSort = false;
|
|
|
+ childView.OptionsCustomization.AllowFilter = false;
|
|
|
+ childView.OptionsCustomization.AllowGroup = false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ grid.Tag = new GridTag();
|
|
|
+ if (formatNullValue)
|
|
|
+ grid.UseNullValueText();
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+ public static GridView GetChildView(this GridControl grid)
|
|
|
+ {
|
|
|
+ return grid.LevelTree?.Nodes?.FirstOrDefault()?.LevelTemplate as GridView;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static GridControl UseEditForm(this GridControl grid, int columnCount, params string[] columns)
|
|
|
+ {
|
|
|
+ var view = grid.MainView as GridView;
|
|
|
+ view.OptionsBehavior.Editable = true;
|
|
|
+ view.OptionsBehavior.EditingMode = DevExpress.XtraGrid.Views.Grid.GridEditingMode.EditForm;
|
|
|
+ //foreach (GridColumn item in view.Columns)
|
|
|
+ //{
|
|
|
+ // if (item.VisibleIndex > 0)
|
|
|
+ // item.OptionsEditForm.VisibleIndex = item.VisibleIndex;
|
|
|
+ //}
|
|
|
+ var maxIdx = view.Columns.Max(p => p.OptionsEditForm.VisibleIndex);
|
|
|
+ for (int i = 0; i < columns.Length; i++)
|
|
|
+ {
|
|
|
+ view.Columns[columns[i]].OptionsEditForm.Visible = DefaultBoolean.True;
|
|
|
+ view.Columns[columns[i]].OptionsEditForm.VisibleIndex = maxIdx + i + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void SetViewDefaultProps(GridControl grid, GridView view, int rowHeight, bool showScrollH)
|
|
|
+ {
|
|
|
view.CustomColumnDisplayText += View_CustomColumnDisplayText;
|
|
|
+ view.OptionsView.ShowButtonMode = DevExpress.XtraGrid.Views.Base.ShowButtonModeEnum.ShowAlways;
|
|
|
view.Appearance.HeaderPanel.TextOptions.HAlignment = HorzAlignment.Center;
|
|
|
-
|
|
|
view.Appearance.Row.Options.UseTextOptions = true;
|
|
|
view.Appearance.Row.TextOptions.HAlignment = HorzAlignment.Center;
|
|
|
view.OptionsView.ColumnHeaderAutoHeight = DefaultBoolean.True;
|
|
|
view.RowHeight = rowHeight;
|
|
|
view.OptionsView.ShowGroupPanel = false;
|
|
|
|
|
|
+ view.OptionsDetail.ShowDetailTabs = false;
|
|
|
+ view.OptionsDetail.AllowZoomDetail = false;
|
|
|
+
|
|
|
view.OptionsBehavior.Editable = false;
|
|
|
view.OptionsView.ShowDetailButtons = false;
|
|
|
view.OptionsView.ShowIndicator = false;
|
|
@@ -79,61 +180,50 @@ public static class GridControlEx
|
|
|
view.OptionsLayout.StoreFormatRules = true;
|
|
|
view.OptionsView.ColumnAutoWidth = !showScrollH;
|
|
|
view.PopupMenuShowing += View_PopupMenuShowing;
|
|
|
-
|
|
|
view.OptionsSelection.EnableAppearanceFocusedCell = false;
|
|
|
+
|
|
|
string GetLayoutName()
|
|
|
{
|
|
|
- Control ctrl;
|
|
|
+ Control ctrl = null;
|
|
|
if (view.Tag is SearchLookUpEdit searchLookUpEdit)
|
|
|
+ {
|
|
|
ctrl = searchLookUpEdit;
|
|
|
+ }
|
|
|
else
|
|
|
+ {
|
|
|
ctrl = grid;
|
|
|
+ }
|
|
|
while (ctrl.Parent != null)
|
|
|
{
|
|
|
ctrl = ctrl.Parent;
|
|
|
if (ctrl is UserControl) break;
|
|
|
+ if (ctrl is Form) break;
|
|
|
+
|
|
|
}
|
|
|
- var props = typeof(T).GetProperties();
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- foreach (var item in props)
|
|
|
- {
|
|
|
- sb.Append(item.Name);
|
|
|
- }
|
|
|
- var md5 = MD5Helper.StrToMD5(sb.ToString());
|
|
|
+ if (ctrl is not UserControl && ctrl is not Form) return null;
|
|
|
+ //var props = typeof(T).GetProperties();
|
|
|
+ //StringBuilder sb = new StringBuilder();
|
|
|
+ //foreach (var item in props)
|
|
|
+ //{
|
|
|
+ // sb.Append(item.Name);
|
|
|
+ //}
|
|
|
+ //var md5 = MD5Helper.StrToMD5(sb.ToString());
|
|
|
if (view.Tag is SearchLookUpEdit searchLookUpEdit2)
|
|
|
- return $"{ctrl.Name}_{searchLookUpEdit2.Name}_{md5}";
|
|
|
+ return $"{ctrl.Name}_{searchLookUpEdit2.Name}";
|
|
|
else
|
|
|
- return $"{ctrl.Name}_{grid.Name}_{md5}";
|
|
|
+ return $"{ctrl.Name}_{grid.Name}_{view.Name}";
|
|
|
}
|
|
|
string name = GetLayoutName();
|
|
|
- void SaveLayoutAction()
|
|
|
+ if (!string.IsNullOrWhiteSpace(name))
|
|
|
{
|
|
|
-
|
|
|
- Form frm = null;
|
|
|
- if (view.Tag is SearchLookUpEdit edit)
|
|
|
- frm = edit.FindForm();
|
|
|
- else
|
|
|
- frm = grid.FindForm();
|
|
|
-
|
|
|
- if (frm != null)
|
|
|
+ view.Layout += (sender, e) =>
|
|
|
{
|
|
|
- frm.VisibleChanged += (sender2, e2) =>
|
|
|
- {
|
|
|
- Directory.CreateDirectory("Layout");
|
|
|
- if (name == null) return;
|
|
|
- grid.MainView?.SaveLayoutToXml($"Layout\\{name}.xml", OptionsLayoutBase.FullLayout);
|
|
|
- };
|
|
|
- }
|
|
|
- }
|
|
|
- if (grid.IsLoaded)
|
|
|
- {
|
|
|
- SaveLayoutAction();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- grid.Load += (sender, e) => SaveLayoutAction();
|
|
|
+ Directory.CreateDirectory("Layout");
|
|
|
+ if (name == null) return;
|
|
|
+ (sender as GridView).SaveLayoutToXml($"Layout\\{name}.xml", OptionsLayoutBase.FullLayout);
|
|
|
+ };
|
|
|
}
|
|
|
- if (name != null && File.Exists($"Layout\\{name}.xml"))
|
|
|
+ if (!string.IsNullOrWhiteSpace(name) && File.Exists($"Layout\\{name}.xml"))
|
|
|
{
|
|
|
view.RestoreLayoutFromXml($"Layout\\{name}.xml", OptionsLayoutBase.FullLayout);
|
|
|
view.ClearColumnsFilter();
|
|
@@ -143,8 +233,6 @@ public static class GridControlEx
|
|
|
view.ClearSelection();
|
|
|
}
|
|
|
view.KeyUp += View_KeyUp;
|
|
|
- grid.Tag = new GridTag();
|
|
|
- return grid;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -153,7 +241,7 @@ public static class GridControlEx
|
|
|
/// <param name="grid"></param>
|
|
|
/// <param name="emptyText"></param>
|
|
|
/// <returns></returns>
|
|
|
- public static GridControl UseEmptyText(this GridControl grid, string emptyText = "暂无数据")
|
|
|
+ public static GridControl UseEmptyText(this GridControl grid, string emptyText = "暂无数据")
|
|
|
{
|
|
|
if (string.IsNullOrWhiteSpace(emptyText)) return grid;
|
|
|
var view = grid.MainView as GridView;
|
|
@@ -163,7 +251,7 @@ public static class GridControlEx
|
|
|
var x = (e.Bounds.Width - s.Width) / 2;
|
|
|
var y = (e.Bounds.Height - s.Height) / 2;
|
|
|
e.Appearance.ForeColor = Color.Gray;
|
|
|
- e.Appearance.DrawString(e.Cache, emptyText, new Rectangle(x, y, s.Width, s.Height));
|
|
|
+ e.Appearance.DrawString(e.Cache, emptyText, new Rectangle(x, y + 20, s.Width, s.Height));
|
|
|
};
|
|
|
return grid;
|
|
|
}
|
|
@@ -194,8 +282,14 @@ public static class GridControlEx
|
|
|
{
|
|
|
var addRow = callback();
|
|
|
if (addRow == null) return;
|
|
|
- var ds = grid.DataSource as List<T>;
|
|
|
- ds.Add(addRow);
|
|
|
+ if (grid.DataSource is List<T> ds)
|
|
|
+ {
|
|
|
+ ds.Add(addRow);
|
|
|
+ }
|
|
|
+ else if (grid.DataSource is BindingList<T> bl)
|
|
|
+ {
|
|
|
+ bl.Add(addRow);
|
|
|
+ }
|
|
|
view.RefreshData();
|
|
|
}
|
|
|
|
|
@@ -220,8 +314,14 @@ public static class GridControlEx
|
|
|
var addRow = await callback();
|
|
|
if (addRow == null) return;
|
|
|
var view = grid.MainView as GridView;
|
|
|
- var ds = grid.DataSource as List<T>;
|
|
|
- ds.Add(addRow);
|
|
|
+ if (grid.DataSource is List<T> ds)
|
|
|
+ {
|
|
|
+ ds.Add(addRow);
|
|
|
+ }
|
|
|
+ else if (grid.DataSource is BindingList<T> bl)
|
|
|
+ {
|
|
|
+ bl.Add(addRow);
|
|
|
+ }
|
|
|
view.RefreshData();
|
|
|
}
|
|
|
|
|
@@ -246,10 +346,18 @@ public static class GridControlEx
|
|
|
var newRow = callback(data);
|
|
|
if (newRow == null) return;
|
|
|
var view = grid.MainView as GridView;
|
|
|
- var ds = grid.DataSource as List<T>;
|
|
|
- var idx = ds.IndexOf(data);
|
|
|
- ds.Remove(data);
|
|
|
- ds.Insert(idx, newRow);
|
|
|
+ if (grid.DataSource is List<T> ds)
|
|
|
+ {
|
|
|
+ var idx=ds.IndexOf(data);
|
|
|
+ ds.Remove(data);
|
|
|
+ ds.Insert(idx, newRow);
|
|
|
+ }
|
|
|
+ else if (grid.DataSource is BindingList<T> bl)
|
|
|
+ {
|
|
|
+ var idx = bl.IndexOf(data);
|
|
|
+ bl.Remove(data);
|
|
|
+ bl.Insert(idx, newRow);
|
|
|
+ }
|
|
|
view.RefreshData();
|
|
|
}
|
|
|
|
|
@@ -274,10 +382,18 @@ public static class GridControlEx
|
|
|
var newRow = await callback(data);
|
|
|
if (newRow == null) return;
|
|
|
var view = grid.MainView as GridView;
|
|
|
- var ds = grid.DataSource as List<T>;
|
|
|
- var idx = ds.IndexOf(data);
|
|
|
- ds.Remove(data);
|
|
|
- ds.Insert(idx, newRow);
|
|
|
+ if (grid.DataSource is List<T> ds)
|
|
|
+ {
|
|
|
+ var idx = ds.IndexOf(data);
|
|
|
+ ds.Remove(data);
|
|
|
+ ds.Insert(idx, newRow);
|
|
|
+ }
|
|
|
+ else if (grid.DataSource is BindingList<T> bl)
|
|
|
+ {
|
|
|
+ var idx = bl.IndexOf(data);
|
|
|
+ bl.Remove(data);
|
|
|
+ bl.Insert(idx, newRow);
|
|
|
+ }
|
|
|
view.RefreshData();
|
|
|
}
|
|
|
|
|
@@ -363,7 +479,7 @@ public static class GridControlEx
|
|
|
}, false);
|
|
|
return grid;
|
|
|
}
|
|
|
- public static GridControl SetLogImageColumn(this GridControl grid, string columnFieldName,Type enumType)
|
|
|
+ public static GridControl SetLogImageColumn(this GridControl grid, string columnFieldName, Type enumType)
|
|
|
{
|
|
|
var view = grid.MainView as GridView;
|
|
|
RepositoryItemImageComboBox edit = new RepositoryItemImageComboBox();
|
|
@@ -376,9 +492,244 @@ public static class GridControlEx
|
|
|
edit.SmallImages = svgImages;
|
|
|
edit.GlyphAlignment = DevExpress.Utils.HorzAlignment.Center;
|
|
|
view.Columns[columnFieldName].ColumnEdit = edit;
|
|
|
+ view.Columns[columnFieldName].VisibleIndex = 0;
|
|
|
+ view.Columns[columnFieldName].MaxWidth = 64;
|
|
|
+ view.Columns[columnFieldName].MinWidth = 64;
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+ public static GridControl SetStateImageColumn(this GridControl grid, string columnFieldName, Type enumType)
|
|
|
+ {
|
|
|
+ var view = grid.MainView as GridView;
|
|
|
+ RepositoryItemImageComboBox edit = new RepositoryItemImageComboBox();
|
|
|
+ edit.AddEnum(enumType);
|
|
|
+ SvgImageCollection svgImages = new SvgImageCollection();
|
|
|
+ svgImages.ImageSize = new System.Drawing.Size(20, 20);
|
|
|
+ svgImages.Add(SvgHelper.CreateTskStateStoped());
|
|
|
+ svgImages.Add(SvgHelper.CreateTskStateWaitToRun());
|
|
|
+ svgImages.Add(SvgHelper.CreateTskStateRunning());
|
|
|
+ svgImages.Add(SvgHelper.CreateTskStateStopping());
|
|
|
+ edit.SmallImages = svgImages;
|
|
|
+ edit.GlyphAlignment = DevExpress.Utils.HorzAlignment.Center;
|
|
|
+ view.Columns[columnFieldName].ColumnEdit = edit;
|
|
|
+ view.Columns[columnFieldName].VisibleIndex = 0;
|
|
|
+ view.Columns[columnFieldName].MaxWidth = 64;
|
|
|
+ view.Columns[columnFieldName].MinWidth = 64;
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static GridControl UseRowFocusedStyle(this GridControl grid)
|
|
|
+ {
|
|
|
+ var view = grid.MainView as GridView;
|
|
|
+ view.OptionsSelection.EnableAppearanceFocusedCell = false;
|
|
|
+ view.FocusRectStyle = DrawFocusRectStyle.RowFocus;
|
|
|
+ var childView = grid.GetChildView();
|
|
|
+ if (childView != null)
|
|
|
+ {
|
|
|
+ childView.OptionsSelection.EnableAppearanceFocusedCell = false;
|
|
|
+ childView.FocusRectStyle = DrawFocusRectStyle.RowFocus;
|
|
|
+ }
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+ public static GridControl UseFileChooseColumn(this GridControl grid, string columnFieldName, bool columnCanInput = false)
|
|
|
+ {
|
|
|
+ var view = grid.MainView as GridView;
|
|
|
+ var edit = new RepositoryItemButtonEdit() { AutoHeight = false };
|
|
|
+ edit.ReadOnly = !columnCanInput;
|
|
|
+ view.Columns[columnFieldName].Visible = true;
|
|
|
+ view.Columns[columnFieldName].ColumnEdit = edit;
|
|
|
+ edit.ButtonClick += (sender, e) =>
|
|
|
+ {
|
|
|
+ OpenFileDialog dlg = new OpenFileDialog();
|
|
|
+ dlg.Title = "文件选择";
|
|
|
+ dlg.Multiselect = false;
|
|
|
+ if (dlg.ShowDialog() == DialogResult.OK)
|
|
|
+ {
|
|
|
+ view.SetFocusedRowCellValue(columnFieldName, dlg.FileName);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+ public static GridControl UseHttpDownloadColumn(this GridControl grid, string columnFieldName)
|
|
|
+ {
|
|
|
+ grid.UseEdit(false);
|
|
|
+
|
|
|
+ var view = grid.MainView as GridView;
|
|
|
+ var edit = new RepositoryItemButtonEdit() { AutoHeight = false, ReadOnly = true };
|
|
|
+ edit.Buttons[0].Kind = ButtonPredefines.Glyph;
|
|
|
+ edit.Buttons[0].ToolTip = "下载文件";
|
|
|
+ edit.TextEditStyle = TextEditStyles.DisableTextEditor;
|
|
|
+ edit.Buttons[0].ImageOptions.SvgImage = SvgHelper.CreateDownload(width: 12, height: 12);
|
|
|
+ view.Columns[columnFieldName].OptionsColumn.AllowEdit = true;
|
|
|
+ view.Columns[columnFieldName].Visible = true;
|
|
|
+ view.Columns[columnFieldName].ColumnEdit = edit;
|
|
|
+ edit.ButtonClick += async (sender, e) =>
|
|
|
+ {
|
|
|
+ var url = view.GetFocusedRowCellValue(columnFieldName)?.ToString();
|
|
|
+ if (string.IsNullOrWhiteSpace(url)) return;
|
|
|
+ var localFile = Path.GetFileName(url);
|
|
|
+ SaveFileDialog d = new SaveFileDialog();
|
|
|
+ d.FileName = localFile;
|
|
|
+ if (d.ShowDialog() == DialogResult.OK)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (await HttpHelper.DownloadFileAsync(url, d.FileName))
|
|
|
+ {
|
|
|
+ var dir = Path.GetDirectoryName(d.FileName);
|
|
|
+ System.Diagnostics.Process.Start("explorer.exe", dir);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw new Exception($"文件下载失败,url={url}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw new Exception($"文件下载失败,url={url}",ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+ public static GridControl UseBoolColumn(this GridControl grid, string columnFieldName, string trueText = "是", string falseText = "否", GridView view = null)
|
|
|
+ {
|
|
|
+ if (view == null)
|
|
|
+ view = grid.MainView as GridView;
|
|
|
+ var edit = new RepositoryItemImageComboBox() { AutoHeight = false };
|
|
|
+ edit.Items.Add(new ImageComboBoxItem(trueText, true));
|
|
|
+ edit.Items.Add(new ImageComboBoxItem(falseText, false));
|
|
|
+ edit.ReadOnly = false;
|
|
|
+ view.Columns[columnFieldName].Visible = true;
|
|
|
+ view.Columns[columnFieldName].ColumnEdit = edit;
|
|
|
+ if (columnFieldName == "Enable")
|
|
|
+ view.Columns[columnFieldName].VisibleIndex = 99;
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+ public static GridControl UseLookUpEditColumn(this GridControl grid, string columnFieldName, string displayField = "", GridView view = null)
|
|
|
+ {
|
|
|
+ if (view == null)
|
|
|
+ view = grid.MainView as GridView;
|
|
|
+ var edit = new RepositoryItemLookUpEdit();
|
|
|
+ edit.AutoHeight = false;
|
|
|
+ edit.ValueMember = "Id";
|
|
|
+ edit.DisplayMember = displayField;
|
|
|
+ edit.NullText = "未知";
|
|
|
+ view.Columns[columnFieldName].Visible = true;
|
|
|
+ view.Columns[columnFieldName].ColumnEdit = edit;
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+ public static GridControl UseButtonEditColumn(this GridControl grid, string columnFieldName, GridView view = null)
|
|
|
+ {
|
|
|
+ grid.UseEdit(false);
|
|
|
+ if (view == null)
|
|
|
+ view = grid.MainView as GridView;
|
|
|
+ var edit = new RepositoryItemButtonEdit();
|
|
|
+ edit.Buttons.Clear();
|
|
|
+ edit.Buttons.Add(new EditorButton(ButtonPredefines.Glyph) { Caption="Click"});
|
|
|
+ edit.AutoHeight = false;
|
|
|
+ edit.TextEditStyle = TextEditStyles.HideTextEditor;
|
|
|
+ view.Columns[columnFieldName].OptionsColumn.AllowEdit = true;
|
|
|
+ view.Columns[columnFieldName].Visible = true;
|
|
|
+ view.Columns[columnFieldName].ColumnEdit = edit;
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+ public static GridControl UseDateTimeEditColumn(this GridControl grid, string columnFieldName, GridView view = null)
|
|
|
+ {
|
|
|
+ if (view == null)
|
|
|
+ view = grid.MainView as GridView;
|
|
|
+ var edit = new RepositoryItemDateEdit();
|
|
|
+ edit.AllowNullInput = DevExpress.Utils.DefaultBoolean.False;
|
|
|
+ edit.AutoHeight = false;
|
|
|
+ edit.CalendarTimeEditing = DevExpress.Utils.DefaultBoolean.True;
|
|
|
+ edit.CalendarTimeProperties.DisplayFormat.FormatString = "HH:mm:ss";
|
|
|
+ edit.CalendarTimeProperties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
|
|
|
+ edit.CalendarTimeProperties.EditFormat.FormatString = "HH:mm:ss";
|
|
|
+ edit.CalendarTimeProperties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
|
|
|
+ edit.CalendarTimeProperties.MaskSettings.Set("mask", "HH:mm:ss");
|
|
|
+ edit.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ edit.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
|
|
|
+ edit.EditFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ edit.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
|
|
|
+ edit.MaskSettings.Set("mask", "yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+
|
|
|
+ view.Columns[columnFieldName].Visible = true;
|
|
|
+ view.Columns[columnFieldName].ColumnEdit = edit;
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+ public static void UpdateLookUpEditDataSource<T>(this GridControl grid, string columnFieldName, List<T> dataSource, GridView view = null)
|
|
|
+ {
|
|
|
+ if (view == null)
|
|
|
+ view = grid.MainView as GridView;
|
|
|
+ var edit = view.Columns[columnFieldName].ColumnEdit as RepositoryItemLookUpEdit;
|
|
|
+ if (edit != null)
|
|
|
+ edit.DataSource = dataSource;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static GridControl UseSearchEditColumn<T>(this GridControl grid, string columnFieldName, List<T> editDataSource, string displayField = "This", GridView view = null)
|
|
|
+ {
|
|
|
+ if (view == null)
|
|
|
+ view = grid.MainView as GridView;
|
|
|
+ var edit = new RepositoryItemSearchLookUpEdit();
|
|
|
+ SearchLookUpEdit dd;
|
|
|
+ Control ctrl = grid;
|
|
|
+ while (ctrl.Parent != null)
|
|
|
+ {
|
|
|
+ ctrl = ctrl.Parent;
|
|
|
+ if (ctrl is UserControl) break;
|
|
|
+ }
|
|
|
+ var searchEdit1View = new DevExpress.XtraGrid.Views.Grid.GridView();
|
|
|
+ searchEdit1View.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
|
|
|
+ searchEdit1View.OptionsSelection.EnableAppearanceFocusedCell = false;
|
|
|
+ searchEdit1View.OptionsView.ShowGroupPanel = false;
|
|
|
+ edit.Name = $"{ctrl.Name}_{grid.Name}_{columnFieldName}_SearchEdit";
|
|
|
+ edit.AutoHeight = false;
|
|
|
+ edit.PopupView = searchEdit1View;
|
|
|
+ edit.ValueMember = "Id";
|
|
|
+ edit.KeyMember = "Id";
|
|
|
+ edit.DisplayMember = displayField;
|
|
|
+ edit.UseDefault().SetData(editDataSource);
|
|
|
+ view.Columns[columnFieldName].Visible = true;
|
|
|
+ view.Columns[columnFieldName].ColumnEdit = edit;
|
|
|
+
|
|
|
+ searchEdit1View.GridControl.UseDefault<T>(null);
|
|
|
+ return grid;
|
|
|
+ }
|
|
|
+ public static GridControl UseSearchEditColumn<T>(this GridControl grid, string columnFieldName, Func<Task<List<T>>> onGetData, string displayField = "This", GridView view = null)
|
|
|
+ {
|
|
|
+ if (view == null)
|
|
|
+ view = grid.MainView as GridView;
|
|
|
+ var edit = new RepositoryItemSearchLookUpEdit();
|
|
|
+ edit.DataSource = onGetData().Result;
|
|
|
+ Control ctrl = grid;
|
|
|
+ while (ctrl.Parent != null)
|
|
|
+ {
|
|
|
+ ctrl = ctrl.Parent;
|
|
|
+ if (ctrl is UserControl) break;
|
|
|
+ }
|
|
|
+ var searchEdit1View = new DevExpress.XtraGrid.Views.Grid.GridView();
|
|
|
+ searchEdit1View.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
|
|
|
+ searchEdit1View.OptionsSelection.EnableAppearanceFocusedCell = false;
|
|
|
+ searchEdit1View.OptionsView.ShowGroupPanel = false;
|
|
|
+
|
|
|
+ edit.Name = $"{ctrl.Name}_{grid.Name}_{columnFieldName}_SearchEdit";
|
|
|
+ edit.AutoHeight = false;
|
|
|
+ edit.PopupView = searchEdit1View;
|
|
|
+ edit.ValueMember = "Id";
|
|
|
+ edit.KeyMember = "Id";
|
|
|
+ edit.DisplayMember = displayField;
|
|
|
+ edit.UseDefault().SetData(onGetData);
|
|
|
+ view.Columns[columnFieldName].Visible = true;
|
|
|
+ view.Columns[columnFieldName].ColumnEdit = edit;
|
|
|
+
|
|
|
+ searchEdit1View.GridControl.UseDefault<T>(null);
|
|
|
return grid;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 为GirdControl启用右键清除所有数据的功能
|
|
|
/// </summary>
|
|
@@ -517,12 +868,11 @@ public static class GridControlEx
|
|
|
if (e.Column.ColumnType == typeof(DateTime) || e.Column.ColumnType == typeof(DateTime?))
|
|
|
{
|
|
|
if (e.Value == null) return;
|
|
|
- if (e.Column.FieldName == "SigTime")
|
|
|
- e.DisplayText = Convert.ToDateTime(e.Value).ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|
|
- else
|
|
|
+ if (e.Column.ColumnType == typeof(DateTime))
|
|
|
e.DisplayText = Convert.ToDateTime(e.Value).ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
public static GridControl ShowColumn(this GridControl grid, params string[] colField)
|
|
|
{
|
|
@@ -536,11 +886,14 @@ public static class GridControlEx
|
|
|
}
|
|
|
return grid;
|
|
|
}
|
|
|
- public static GridControl UseMultiSelect(this GridControl grid)
|
|
|
+ public static GridControl UseMultiSelect(this GridControl grid, bool useCheckBoxSelect = false)
|
|
|
{
|
|
|
var view = grid.MainView as GridView;
|
|
|
view.OptionsSelection.MultiSelect = true;
|
|
|
- view.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
|
|
|
+ if (useCheckBoxSelect)
|
|
|
+ view.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CheckBoxRowSelect;
|
|
|
+ else
|
|
|
+ view.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
|
|
|
return grid;
|
|
|
}
|
|
|
public static GridControl UseGroup(this GridControl grid)
|
|
@@ -561,17 +914,18 @@ public static class GridControlEx
|
|
|
return grid;
|
|
|
|
|
|
}
|
|
|
- public static GridControl UseEdit(this GridControl grid)
|
|
|
+ public static GridControl UseEdit(this GridControl grid, bool columnsEdit = false)
|
|
|
{
|
|
|
var view = grid.MainView as GridView;
|
|
|
view.OptionsBehavior.Editable = true;
|
|
|
view.OptionsSelection.EnableAppearanceFocusedCell = true;
|
|
|
foreach (GridColumn item in view.Columns)
|
|
|
{
|
|
|
- item.OptionsColumn.AllowEdit = false;
|
|
|
+ item.OptionsColumn.AllowEdit = columnsEdit;
|
|
|
}
|
|
|
return grid;
|
|
|
}
|
|
|
+
|
|
|
public static GridControl UseRowNumber(this GridControl grid)
|
|
|
{
|
|
|
var view = grid.MainView as GridView;
|
|
@@ -603,17 +957,19 @@ public static class GridControlEx
|
|
|
return grid;
|
|
|
|
|
|
}
|
|
|
-
|
|
|
- public static GridControl UseNullValueText(this GridControl grid, string nullText = "--")
|
|
|
+ private static GridControl UseNullValueText(this GridControl grid, string nullText = "--")
|
|
|
{
|
|
|
var view = grid.MainView as GridView;
|
|
|
view.CustomColumnDisplayText += (sender, e) =>
|
|
|
{
|
|
|
- if (e.Value == null) e.DisplayText = nullText;
|
|
|
+ if (e.Value == null || e.Value.ToString().Trim() == "")
|
|
|
+ e.DisplayText = nullText;
|
|
|
};
|
|
|
return grid;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
public static GridControl SetDisplayText(this GridControl grid, string columnFieldName, Func<object, string> action)
|
|
|
{
|
|
|
var view = grid.MainView as GridView;
|
|
@@ -625,19 +981,30 @@ public static class GridControlEx
|
|
|
};
|
|
|
return grid;
|
|
|
}
|
|
|
+ /// <summary>
|
|
|
+ /// 隐藏指定字段的列
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="grid"></param>
|
|
|
+ /// <param name="colField"></param>
|
|
|
+ /// <returns></returns>
|
|
|
public static GridControl HideColumn(this GridControl grid, params string[] colField)
|
|
|
{
|
|
|
-
|
|
|
+ colField = colField.Select(t => t.ToLower()).ToArray();
|
|
|
var view = grid.MainView as GridView;
|
|
|
if (colField == null || colField.Length == 0) return grid;
|
|
|
foreach (GridColumn item in view.Columns)
|
|
|
{
|
|
|
- if (colField.Contains(item.FieldName))
|
|
|
+ if (colField.Contains(item.FieldName.ToLower()))
|
|
|
item.Visible = false;
|
|
|
}
|
|
|
return grid;
|
|
|
}
|
|
|
-
|
|
|
+ /// <summary>
|
|
|
+ /// 根据条件隐藏列(只会处理ColumnEdit==null的列)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="grid"></param>
|
|
|
+ /// <param name="condition"></param>
|
|
|
+ /// <returns></returns>
|
|
|
public static GridControl HideColumn(this GridControl grid, Func<GridColumn, bool> condition)
|
|
|
{
|
|
|
var view = grid.MainView as GridView;
|
|
@@ -648,8 +1015,6 @@ public static class GridControlEx
|
|
|
}
|
|
|
return grid;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// GridControl添加单行元素选择后的右键菜单
|
|
|
/// </summary>
|
|
@@ -907,10 +1272,18 @@ public static class GridControlEx
|
|
|
{
|
|
|
if (action != null)
|
|
|
{
|
|
|
- var ds = grid.DataSource as List<T>;
|
|
|
- if (!showWithNoneDataRow && (ds == null || ds.Count == 0)) return;
|
|
|
- item.Enabled = false;
|
|
|
- action.Invoke(ds);
|
|
|
+ if (grid.DataSource is List<T> ds)
|
|
|
+ {
|
|
|
+ if (!showWithNoneDataRow && (ds == null || ds.Count == 0)) return;
|
|
|
+ item.Enabled = false;
|
|
|
+ action.Invoke(ds);
|
|
|
+ }
|
|
|
+ else if (grid.DataSource is BindingList<T> bl)
|
|
|
+ {
|
|
|
+ if (!showWithNoneDataRow && (bl == null || bl.Count == 0)) return;
|
|
|
+ item.Enabled = false;
|
|
|
+ action.Invoke(bl.ToList());
|
|
|
+ }
|
|
|
item.Enabled = true;
|
|
|
}
|
|
|
});
|
|
@@ -954,10 +1327,18 @@ public static class GridControlEx
|
|
|
{
|
|
|
if (action != null)
|
|
|
{
|
|
|
- var ds = grid.DataSource as List<T>;
|
|
|
- if (!showWithNoneDataRow && (ds == null || ds.Count == 0)) return;
|
|
|
- item.Enabled = false;
|
|
|
- await action.Invoke(ds);
|
|
|
+ if (grid.DataSource is List<T> ds)
|
|
|
+ {
|
|
|
+ if (!showWithNoneDataRow && (ds == null || ds.Count == 0)) return;
|
|
|
+ item.Enabled = false;
|
|
|
+ await action.Invoke(ds);
|
|
|
+ }
|
|
|
+ else if (grid.DataSource is BindingList<T> bl)
|
|
|
+ {
|
|
|
+ if (!showWithNoneDataRow && (bl == null || bl.Count == 0)) return;
|
|
|
+ item.Enabled = false;
|
|
|
+ await action.Invoke(bl.ToList());
|
|
|
+ }
|
|
|
item.Enabled = true;
|
|
|
}
|
|
|
});
|