本代码用的DevExpress版本号:17.2.6.0,旧的版本可能有些地方会有些微的变化,比如在旧版本中不需要写特性:[ToolboxItem(true)]。
目前该自定义编辑器中集成了一些比较实用的功能,希望对使用或正在学习DevExpress的同学有所帮助。
等下次有时间再把其他的一些自定义通用控件也发出来。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Windows.Forms; 7 using Comteck.Winforms.Controls.Components; 8 using Comteck.Winforms.Utils; 9 using DevExpress.Data.Filtering; 10 using DevExpress.Utils; 11 using DevExpress.Utils.Serializing; 12 using DevExpress.XtraEditors; 13 using DevExpress.XtraGrid; 14 using DevExpress.XtraGrid.Columns; 15 using DevExpress.XtraGrid.Registrator; 16 using DevExpress.XtraGrid.Views.Base; 17 using DevExpress.XtraGrid.Views.Base.Handler; 18 using DevExpress.XtraGrid.Views.Base.ViewInfo; 19 using DevExpress.XtraGrid.Views.Grid; 20 using DevExpress.XtraGrid.Views.Grid.Drawing; 21 using DevExpress.XtraGrid.Views.Grid.ViewInfo; 22 23 namespace Comteck.Winforms.Controls { 24 #region MyGridControl 25 26 /// <summary> 27 /// A grid view is represented by several classes. 28 /// Please make sure that you inherit the appropriate class to implement a specific functionality: 29 /// GridView - a central grid view class, which manages columns, view infos, a painter, mouse and keyboard handlers. 30 /// GridHandler - processes the mouse and keyboard. 31 /// GridViewInfo - calculates view information for drawing a grid view on screen. 32 /// GridViewPrintInfo - calculates the information necessary to print a grid view. 33 /// 34 /// Assume, you have created a GridView descendant and want to use your class with the GridControl at design time. 35 /// You should do the following to register it: 36 /// 1. Create a Registrator class (please see GridInfoRegistrator in ViewRegistrator.cs). 37 /// Please note: you will need to be careful when overriding the ViewName property, 38 /// because it is used to identify the view within the GridControl‘s descendant. 39 /// Please make certain that you are using the same view name both in the registrator‘s ViewName property 40 /// and within the GridControl‘s CreateDefaultView overridden method. 41 /// 2. Inherit the GridControl class and override the RegisterAvailableViewsCore 42 /// method (it was the RegisterAvailableViews method in XtraGrid 2). 43 /// 3. If you want the GridControl to create an instance of your GridView by default, 44 /// you should override the GridControl.CreateDefaultView method. 45 /// 46 /// 创建自定义的MyGridControl 47 /// <para>https://www.devexpress.com/Support/Center/Search/List/1?searchString=treelist</para> 48 /// <para>http://www.dxper.net/thread-555-1-1.html</para> 49 /// </summary> 50 [ToolboxItem(true)] 51 public class MyGridControl : GridControl { 52 /// <summary> 53 /// Create a new MyGridControl object 54 /// </summary> 55 public MyGridControl() 56 : base() { 57 // 使用内置的分页 58 this.CustomInit(); 59 } 60 61 /// <summary> 62 /// 创建初始化的GridView视图(请参照第3点) 63 /// </summary> 64 /// <returns></returns> 65 protected override BaseView CreateDefaultView() { 66 return base.CreateView("MyGridView"); 67 } 68 69 /// <summary> 70 /// 向容器中注册所有的控件 71 /// </summary> 72 /// <param name="collection"></param> 73 protected override void RegisterAvailableViewsCore(InfoCollection collection) { 74 base.RegisterAvailableViewsCore(collection); 75 collection.Add(new MyGridViewInfoRegistrator()); 76 collection.Add(new MyBandedGridInfoRegistrator()); 77 } 78 79 /// <summary> 80 /// 初始化内置的分页,如果要使用增、删、改功能,则把下面的代码注释即可 81 /// </summary> 82 private void InitNagigator() { 83 this.EmbeddedNavigator.TextStringFormat = "记录 {0} / {1}"; 84 this.EmbeddedNavigator.Buttons.Append.Visible = false; 85 this.EmbeddedNavigator.Buttons.Remove.Visible = false; 86 this.EmbeddedNavigator.Buttons.Edit.Visible = false; 87 this.EmbeddedNavigator.Buttons.EndEdit.Visible = false; 88 this.EmbeddedNavigator.Buttons.CancelEdit.Visible = false; 89 } 90 91 private void CustomInit() { 92 this.InitNagigator(); 93 } 94 } 95 96 #endregion 97 98 #region 创建自定义的GridView 99 100 /// <summary> 101 /// 创建自定义的GridView 102 /// </summary> 103 public class MyGridView : GridView { 104 /// <summary> 105 /// DevExpress选择列的默认fieldname 106 /// </summary> 107 public const string DEVEXPRESS_SELECTION_NAME = "DX$CheckboxSelectorColumn"; 108 /// <summary> 109 /// 当光标在末行最后一个指定字段时继续回车的跳转方式 110 /// </summary> 111 public enum EnumForwardType { 112 [Description("跳转到首行首列")] 113 Circle = 0, 114 [Description("停止跳转,直接返回")] 115 Stop = 1, 116 [Description("新增一行,光标定位到新行指定列")] 117 NewRow = 2, 118 } 119 120 /// <summary> 121 /// Default View Name 122 /// </summary> 123 protected override string ViewName => "MyGridView"; 124 125 public delegate void AddNewLineDelegate(); 126 /// <summary> 127 /// 新增行自定义事件 128 /// </summary> 129 public AddNewLineDelegate AddNewLineEventHandler; 130 131 #region 自定义属性 132 133 // 134 // 摘要: 135 // 当 GridView 没有数据时是否显示提示,提示的值由 EmptyForegroundText 属性设置 136 [DefaultValue(false),] 137 [DXCategory("自定义属性")] 138 [Description("当 GridView 没有数据时是否显示提示,提示的值由 EmptyForegroundText 属性设置")] 139 [XtraSerializableProperty] 140 public virtual bool EnableShowEmptyForeground { get; set; } 141 // 142 // 摘要: 143 // 当 GridView 没有数据时的默认提示. 144 [DefaultValue(typeof(string), "")] 145 [DXCategory("自定义属性")] 146 [Description("当 GridView 没有数据时显示的值,默认显示\"没有查询到你所想要的数据!\"")] 147 [XtraSerializableProperty] 148 public virtual string EmptyForegroundText { get; set; } 149 // 150 // 摘要: 151 // GridView 回车时按指定字段跳转到相应的单元格. 152 // 调用方法如:new List<string>() { "SKUCODE", "PRICE", "DESCRIPTION" }; 153 [DefaultValue(typeof(List<string>), "")] 154 [DXCategory("自定义属性")] 155 [Description("指定可以跳转的列(FieldName),回车时按值先后顺序跳转")] 156 [XtraSerializableProperty] 157 [TypeConverter(typeof(CollectionConverter))] 158 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 159 [Browsable(false)] 160 public virtual List<string> ForwardColumnList { get; set; } 161 // 162 // 摘要: 163 // 当光标在末行最后一个指定字段时继续回车的跳转方式. 164 [DefaultValue(typeof(EnumForwardType), "Circle")] 165 [DXCategory("自定义属性")] 166 [Description("当光标在末行最后一个指定字段时继续回车的跳转方式")] 167 [XtraSerializableProperty] 168 public virtual EnumForwardType ForwardType { get; set; } = EnumForwardType.Circle; 169 // 170 // 摘要: 171 // 当末行最后一列回车新增行时,指定光标在新行定位的列(FieldName). 172 [DefaultValue(typeof(string), "")] 173 [DXCategory("自定义属性")] 174 [Description("当末行最后一列回车新增行时,指定光标在新行定位的列(FieldName),默认为新增行首个可编辑列")] 175 [XtraSerializableProperty] 176 public virtual string LocatedColumn { get; set; } 177 178 #endregion 179 180 #region 构造函数 181 182 /// <summary> 183 /// 默认构造 184 /// </summary> 185 public MyGridView() { 186 this.OptionsFilter.ColumnFilterPopupMode = DevExpress.XtraGrid.Columns.ColumnFilterPopupMode.Classic; 187 } 188 189 /// <summary> 190 /// 含参构造 191 /// </summary> 192 /// <param name="grid"></param> 193 public MyGridView(GridControl ownerGrid) 194 : base(ownerGrid) { 195 this.OptionsFilter.ColumnFilterPopupMode = DevExpress.XtraGrid.Columns.ColumnFilterPopupMode.Classic; 196 } 197 198 #endregion 199 200 #region 自定义方法 201 202 /// <summary> 203 /// 初始化,设置默认值 204 /// </summary> 205 public void Initialize() { 206 // 设置为多选 207 this.OptionsSelection.MultiSelect = true; 208 // 自带勾选列的宽度 209 this.OptionsSelection.CheckBoxSelectorColumnWidth = 35; 210 // 选中序号列是否勾选 211 this.OptionsSelection.UseIndicatorForSelection = false; 212 // 隐藏提示 213 this.OptionsView.ShowGroupPanel = false; 214 // 是否自适应列宽度 215 this.OptionsView.ColumnAutoWidth = false; 216 // 是否启用偶数行外观 217 this.OptionsView.EnableAppearanceEvenRow = true; 218 // 是否启用奇数行外观 219 this.OptionsView.EnableAppearanceOddRow = true; 220 // 是否显示过滤面板 221 this.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never; 222 // 是否显示子报表 223 this.OptionsDetail.EnableMasterViewMode = false; 224 // 筛选模式 225 this.OptionsFilter.ColumnFilterPopupMode = DevExpress.XtraGrid.Columns.ColumnFilterPopupMode.Classic; 226 // 编辑模式 227 this.OptionsBehavior.EditorShowMode = EditorShowMode.MouseUp; 228 // 打印效果 229 this.OptionsPrint.AutoWidth = false; 230 // 字段标题居中 231 this.Appearance.HeaderPanel.TextOptions.HAlignment = HorzAlignment.Center; 232 //选中行颜色 233 this.SetAppearanceColor(); 234 // 是否回车后光标移到下一个单元格 235 this.OptionsNavigation.EnterMoveNextColumn = true; 236 // 是否允许移动列 237 this.OptionsCustomization.AllowColumnMoving = false; 238 // 默认选中第一行 239 this.FocusedRowHandle = 0; 240 // 设置行号宽度 241 this.IndicatorWidth = 28; 242 // 可编辑列设定 243 SetEditableColumns(); 244 } 245 246 /// <summary> 247 /// 设置单元格是否可编辑 248 /// </summary> 249 /// <param name="editableColumnsList">当GridView可编辑时,指定可以编辑的列(FieldName).</param> 250 public void SetEditableColumns(List<string> editableColumnsList) { 251 if (this.OptionsBehavior.Editable && editableColumnsList != null && editableColumnsList.Count > 0) { 252 foreach (GridColumn col in this.Columns) { 253 if (editableColumnsList.Exists(x => x == col.FieldName)) { 254 // 设置标题字体色 255 col.AppearanceHeader.ForeColor = Color.Blue; 256 col.OptionsColumn.AllowEdit = true; 257 } else { 258 // 设置标题字体色 259 col.AppearanceHeader.ForeColor = Color.Black; 260 col.OptionsColumn.AllowEdit = false; 261 } 262 } 263 } 264 } 265 266 /// <summary> 267 /// 判断是否为表格最后一列可编辑列 268 /// </summary> 269 /// <param name="view"></param> 270 /// <param name="focusedColumn"></param> 271 /// <returns></returns> 272 private bool IsLastEditableColumn(GridColumn focusedColumn) { 273 var index = this.VisibleColumns.IndexOf(focusedColumn); 274 275 for (int i = this.VisibleColumns.Count - 1; i >= 0; i--) { 276 GridColumn column = this.VisibleColumns[i]; 277 // 忽略不可编辑Column 278 if (!column.OptionsColumn.AllowEdit) continue; 279 // 当前列如果是最后一列可编辑列,则返回True,否则返回False 280 return index == i; 281 } 282 // 如果都不可编辑,则返回False 283 return false; 284 } 285 286 /// <summary> 287 /// 添加新行 288 /// </summary> 289 private void AddNewLine() { 290 var list = this.GridControl.DataSource; 291 // 获取当前实例的 Type 292 var t = this.DataSource.GetType(); 293 // 动态创建实例 294 var obj = Activator.CreateInstance(t.GetGenericArguments()[0]); 295 // 调用公用方法 Add 296 var method = t.GetMethod("Add"); 297 // 将构建好的实例添加到数据源中 298 method.Invoke(list, new object[] { obj }); 299 // 刷新数据源 300 this.RefreshData(); 301 } 302 303 /// <summary> 304 /// 获取GridView过滤或排序后的数据集 305 /// </summary> 306 /// <typeparam name="T">泛型对象</typeparam> 307 /// <returns></returns> 308 public IEnumerable<T> GetFilteredDataSource<T>() where T : class { 309 var list = new List<T>(); 310 for (int i = 0; i < this.RowCount; i++) { 311 if (this.IsGroupRow(i)) continue; 312 313 var entity = this.GetRow(i) as T; 314 if (entity == null) continue; 315 316 list.Add(entity); 317 } 318 return list; 319 } 320 321 /// <summary> 322 /// 获取GridView的选中行数据集(数据源是 DataTable) 323 /// </summary> 324 /// <returns></returns> 325 public DataTable GetSelectedDataRows() { 326 var dt = (this.DataSource as DataView).Table.Clone(); 327 var rowIds = this.GetSelectedRows(); 328 329 foreach (var id in rowIds) { 330 var row = (this.GetRow(id) as DataRowView).Row; 331 if (row == null) continue; 332 333 dt.Rows.Add(row.ItemArray); 334 } 335 return dt; 336 } 337 338 #endregion 339 340 /// <summary> 341 /// 在初始化后注册一些事件 342 /// </summary> 343 public override void EndInit() { 344 base.EndInit(); 345 346 // 针对MyGridLookUpEdit的情况 347 if (string.IsNullOrWhiteSpace(this.GridControl?.Name)) return; 348 349 // 设置行号 350 this.CustomDrawRowIndicator += new RowIndicatorCustomDrawEventHandler(MyGridView_CustomDrawRowIndicator); 351 // 设置行号宽度 352 this.RowCountChanged += new EventHandler(MyGridView_RowCountChanged); 353 // 在查询得到0条记录时显示自定义的字符提示/显示 354 this.CustomDrawEmptyForeground += new CustomDrawEventHandler(MyGridView_CustomDrawEmptyForeground); 355 // 回车跳转单元格(配合 ForwardColumnList 使用) 356 this.KeyDown += new KeyEventHandler(MyGridView_KeyDown); 357 } 358 359 /// <summary> 360 /// 设置自动行号 361 /// </summary> 362 /// <param name="sender"></param> 363 /// <param name="e"></param> 364 private void MyGridView_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e) { 365 if (e.Info.IsRowIndicator && e.RowHandle >= 0) { 366 e.Info.DisplayText = (e.RowHandle + 1).ToString(); 367 } 368 } 369 370 /// <summary> 371 /// 行号宽度随行数的变化而变化 372 /// </summary> 373 /// <param name="sender"></param> 374 /// <param name="e"></param> 375 private void MyGridView_RowCountChanged(object sender, EventArgs e) { 376 // 根据总行数设置行号宽度 377 this.IndicatorWidth = 28 + (this.RowCount.ToString().Length - 1) * 10; 378 } 379 380 /// <summary> 381 /// 在查询得到0条记录时显示自定义的字符提示/显示 382 /// </summary> 383 /// <param name="sender"></param> 384 /// <param name="e"></param> 385 private void MyGridView_CustomDrawEmptyForeground(object sender, CustomDrawEventArgs e) { 386 if (!this.EnableShowEmptyForeground) return; 387 388 string showText = string.IsNullOrWhiteSpace(this.EmptyForegroundText) ? "没有查询到你所想要的数据!" : this.EmptyForegroundText; 389 390 //方法一(此方法为GridView设置了数据源绑定时,可用) 391 try { 392 var bindingSource = this.DataSource as BindingSource; 393 if (bindingSource.Count == 0) { 394 var str = showText; 395 var f = new Font("宋体", 10, FontStyle.Bold); 396 var r = new Rectangle(e.Bounds.Top + 5, e.Bounds.Left + 5, e.Bounds.Right - 5, e.Bounds.Height - 5); 397 e.Graphics.DrawString(str, f, Brushes.Black, r); 398 } 399 } catch { 400 //方法二(此方法为GridView没有设置数据源绑定时使用,一般使用此种方法) 401 if (this.RowCount == 0) { 402 var str = showText; 403 var f = new Font("宋体", 10, FontStyle.Bold); 404 var r = new Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 5, e.Bounds.Width - 5, e.Bounds.Height - 5); 405 e.Graphics.DrawString(str, f, Brushes.Black, r); 406 } 407 } 408 } 409 410 #region 单元格回车事件 411 412 /// <summary> 413 /// 默认情况下,回车或Tab按可编辑列跳转 414 /// </summary> 415 /// <param name="sender"></param> 416 /// <param name="e"></param> 417 private void OnDefaultKeyDown(object sender, KeyEventArgs e) { 418 if (!((e.KeyCode == Keys.Enter && this.OptionsNavigation.EnterMoveNextColumn) || e.KeyCode == Keys.Tab)) return; 419 420 GridColumn column = null; 421 var index = this.VisibleColumns.IndexOf(this.FocusedColumn); 422 423 // 光标是否在最后一列可编辑列 424 if (IsLastEditableColumn(this.FocusedColumn)) { 425 #region 判断是否到达末行 426 427 if (this.FocusedRowHandle >= this.RowCount - 1) { 428 // 此处可选择跳转到首行或者直接返回或者新增行 429 switch (this.ForwardType) { 430 case EnumForwardType.Circle: // 首行首列 431 this.MoveFirst(); 432 for (int i = 0; i < this.VisibleColumns.Count; i++) { 433 column = this.VisibleColumns[i]; 434 // 忽略不可编辑Column 435 if (!column.OptionsColumn.AllowEdit) continue; 436 // 当前列可编辑,则光标定位到该列 437 this.FocusedColumn = column; 438 return; 439 } 440 break; 441 case EnumForwardType.Stop: // 停止跳转,直接返回 442 e.Handled = true; 443 break; 444 case EnumForwardType.NewRow: // 新增行,并跳转到新行首列 445 if (this.AddNewLineEventHandler == null) 446 AddNewLine(); 447 else 448 AddNewLineEventHandler(); 449 450 this.MoveNext(); 451 // 没有指定定位字段 452 if (string.IsNullOrWhiteSpace(this.LocatedColumn)) { 453 for (int i = 0; i < this.VisibleColumns.Count; i++) { 454 column = this.VisibleColumns[i]; 455 // 忽略不可编辑Column 456 if (!column.OptionsColumn.AllowEdit) continue; 457 // 当前列可编辑,则光标定位到该列 458 this.FocusedColumn = column; 459 return; 460 } 461 } else { 462 this.FocusedColumn = this.Columns[this.LocatedColumn]; 463 } 464 break; 465 } 466 return; 467 } 468 469 #endregion 470 471 this.MoveNext(); 472 for (int i = 0; i < this.VisibleColumns.Count; i++) { 473 column = this.VisibleColumns[i]; 474 // 忽略不可编辑Column 475 if (!column.OptionsColumn.AllowEdit) continue; 476 // 如果是系统自带的编辑列,则直接跳过 477 if (column.FieldName == DEVEXPRESS_SELECTION_NAME) continue; 478 479 // 当前列可编辑,则光标定位到该列 480 this.FocusedColumn = column; 481 return; 482 } 483 } else { 484 // 从当前定位列跳转到下一可编辑列 485 for (int i = index + 1; i < this.VisibleColumns.Count; i++) { 486 column = this.VisibleColumns[i]; 487 // 忽略不可编辑Column 488 if (!column.OptionsColumn.AllowEdit) continue; 489 // 如果是系统自带的编辑列,则直接跳过 490 if (column.FieldName == DEVEXPRESS_SELECTION_NAME) continue; 491 492 // 当前列可编辑,则光标定位到该列 493 this.FocusedColumn = column; 494 return; 495 } 496 497 // 如果光标在不可编辑列 498 for (int i = 0; i <= index; i++) { 499 column = this.VisibleColumns[i]; 500 // 忽略不可编辑Column 501 if (!column.OptionsColumn.AllowEdit) continue; 502 // 如果是系统自带的编辑列,则直接跳过 503 if (column.FieldName == DEVEXPRESS_SELECTION_NAME) continue; 504 505 // 当前列可编辑,则光标定位到该列 506 this.FocusedColumn = column; 507 return; 508 } 509 } 510 } 511 512 /// <summary> 513 /// 自定义跳转,按指定列字段跳转 514 /// </summary> 515 /// <param name="sender"></param> 516 /// <param name="e"></param> 517 private void OnCustomerKeyDown(object sender, KeyEventArgs e) { 518 if (!((e.KeyCode == Keys.Enter && this.OptionsNavigation.EnterMoveNextColumn) || e.KeyCode == Keys.Tab)) return; 519 520 var fieldName = this.FocusedColumn.FieldName; 521 522 if (this.ForwardColumnList.Contains(fieldName)) { 523 var index = this.ForwardColumnList.IndexOf(fieldName); 524 525 // 光标不在当前行指定列集合的最后一列 526 if (index != this.ForwardColumnList.Count - 1) { 527 this.FocusedColumn = this.Columns[this.ForwardColumnList[index + 1]]; 528 } else // 光标定位当前行指定列集合的最后一列 529 { 530 #region 判断是否到达末行 531 532 if (this.FocusedRowHandle >= this.RowCount - 1) { 533 // 此处可选择跳转到首行或者直接返回或者新增行 534 switch (this.ForwardType) { 535 case EnumForwardType.Circle: // 首行首列 536 this.MoveFirst(); 537 this.FocusedColumn = this.Columns[this.ForwardColumnList[0]]; 538 break; 539 case EnumForwardType.Stop: // 停止跳转,直接返回 540 e.Handled = true; 541 break; 542 case EnumForwardType.NewRow: // 新增行,并跳转到新行首列 543 if (this.AddNewLineEventHandler == null) 544 AddNewLine(); 545 else 546 AddNewLineEventHandler(); 547 548 fieldName = string.IsNullOrWhiteSpace(this.LocatedColumn) ? this.ForwardColumnList[0] : this.LocatedColumn; 549 550 this.MoveNext(); 551 this.FocusedColumn = this.Columns[fieldName]; 552 break; 553 } 554 return; 555 } 556 557 #endregion 558 559 this.MoveNext(); 560 this.FocusedColumn = this.Columns[this.ForwardColumnList[0]]; 561 } 562 } else { 563 this.FocusedColumn = this.Columns[this.ForwardColumnList[0]]; 564 } 565 } 566 567 /// <summary> 568 /// 回车跳转单元格 569 /// </summary> 570 /// <param name="sender"></param> 571 /// <param name="e"></param> 572 private void MyGridView_KeyDown(object sender, KeyEventArgs e) { 573 if (!((e.KeyCode == Keys.Enter && this.OptionsNavigation.EnterMoveNextColumn) || e.KeyCode == Keys.Tab)) return; 574 575 if (this.ValidateEditor() == false) { 576 return; 577 } 578 579 // 未设置,则按默认规则跳转 580 if (this.ForwardColumnList == null || this.ForwardColumnList.Count == 0) { 581 OnDefaultKeyDown(sender, e); 582 } else { 583 OnCustomerKeyDown(sender, e); 584 } 585 } 586 587 #endregion 588 589 #region GridLookUpEdit 模糊匹配 590 591 /// <summary> 592 /// SetGridControlAccessMethod,主要是了MyGridLookUpEdit用 593 /// </summary> 594 /// <param name="newControl"></param> 595 protected internal virtual void SetGridControlAccessMethod(GridControl newControl) { 596 SetGridControl(newControl); 597 } 598 599 /// <summary> 600 /// GridLookUpEdit的模糊匹配 601 /// </summary> 602 /// <param name="text"></param> 603 /// <param name="displayMember"></param> 604 /// <returns></returns> 605 protected override string OnCreateLookupDisplayFilter(string text, string displayMember) { 606 var subStringOperators = new List<CriteriaOperator>(); 607 foreach (var sString in text.Split(‘ ‘)) { 608 var columnsOperators = new List<CriteriaOperator>(); 609 610 foreach (GridColumn col in this.Columns) { 611 columnsOperators.Add(new FunctionOperator(FunctionOperatorType.Contains, new OperandProperty(col.FieldName), sString)); 612 } 613 614 subStringOperators.Add(new GroupOperator(GroupOperatorType.Or, columnsOperators)); 615 } 616 617 return new GroupOperator(GroupOperatorType.And, subStringOperators).ToString(); 618 } 619 620 /// <summary> 621 /// 模糊匹配的字符串 622 /// </summary> 623 protected virtual internal string GetExtraFilterText => base.ExtraFilterText; 624 625 #endregion 626 627 #region 重写属性 628 629 /// <summary> 630 /// 631 /// </summary> 632 /// <returns></returns> 633 protected override ColumnViewOptionsView CreateOptionsView() { 634 return new CustomGridOptionsView(); 635 } 636 637 #endregion 638 } 639 640 #endregion 641 642 #region MyGridHandler processes the mouse and keyboard. 643 644 /// <summary> 645 /// processes the mouse and keyboard. 646 /// </summary> 647 public class MyGridHandler : DevExpress.XtraGrid.Views.Grid.Handler.GridHandler { 648 /// <summary> 649 /// Constructor 650 /// </summary> 651 /// <param name="gridView"></param> 652 public MyGridHandler(GridView gridView) 653 : base(gridView) { 654 } 655 656 /// <summary> 657 /// 重写事件 OnKeyDown 658 /// </summary> 659 /// <param name="e"></param> 660 protected override void OnKeyDown(KeyEventArgs e) { 661 try { 662 base.OnKeyDown(e); 663 } catch { } 664 665 // Ctrl + C 复制单元格 666 if (e.Control & e.KeyCode == Keys.C) { 667 try { 668 Clipboard.SetDataObject(this.View.GetFocusedRowCellDisplayText(this.View.FocusedColumn)); 669 e.Handled = true; 670 } catch { } 671 } 672 } 673 } 674 675 #endregion 676 677 #region MyGridViewInfoRegistrator 678 679 /// <summary> 680 /// GridInfoRegistrator,注册MyGridView 681 /// </summary> 682 public class MyGridViewInfoRegistrator : GridInfoRegistrator { 683 protected const string MyGridViewName = "MyGridView"; 684 685 /// <summary> 686 /// Default View Name 687 /// </summary> 688 public override string ViewName => MyGridViewName; 689 690 /// <summary> 691 /// 创建GridView 692 /// </summary> 693 /// <param name="grid"></param> 694 /// <returns></returns> 695 public override BaseView CreateView(GridControl grid) { 696 // 主要为了GridLookUpEdit用 697 var view = new MyGridView(); 698 view.SetGridControlAccessMethod(grid); 699 view.Initialize(); 700 return view; 701 } 702 703 /// <summary> 704 /// CreateViewInfo 705 /// </summary> 706 /// <param name="view"></param> 707 /// <returns></returns> 708 public override BaseViewInfo CreateViewInfo(BaseView view) { 709 return new MyGridViewInfo(view as MyGridView); 710 } 711 712 /// <summary> 713 /// 创建自定义的事件 714 /// </summary> 715 /// <param name="view"></param> 716 /// <returns></returns> 717 public override BaseViewHandler CreateHandler(BaseView view) { 718 return new MyGridHandler(view as MyGridView); 719 } 720 721 /// <summary> 722 /// 创建自定义的绘画 723 /// </summary> 724 /// <param name="view"></param> 725 /// <returns></returns> 726 public override BaseViewPainter CreatePainter(BaseView view) { 727 return new MyGridPainter(view as GridView); 728 } 729 } 730 731 #endregion 732 733 #region MyGridViewInfo calculates view information for drawing a grid view on screen. 734 735 /// <summary> 736 /// calculates view information for drawing a grid view on screen. 737 /// </summary> 738 public class MyGridViewInfo : GridViewInfo { 739 /// <summary> 740 /// Constructor 741 /// </summary> 742 /// <param name="gridView"></param> 743 public MyGridViewInfo(GridView gridView) 744 : base(gridView) { } 745 } 746 747 #endregion 748 749 #region GridPainter 750 751 /// <summary> 752 /// 自定义画笔 753 /// </summary> 754 public class MyGridPainter : GridPainter { 755 /// <summary> 756 /// Constructor 757 /// </summary> 758 /// <param name="view"></param> 759 public MyGridPainter(GridView view) : base(view) { } 760 761 /// <summary> 762 /// 创建视图 763 /// </summary> 764 public virtual new MyGridView View => (MyGridView)base.View; 765 766 /// <summary> 767 /// 绘制行信息,主要为了MyGridLookUpEdit 768 /// </summary> 769 /// <param name="e"></param> 770 /// <param name="cell"></param> 771 protected override void DrawRowCell(GridViewDrawArgs e, GridCellInfo cell) { 772 cell.ViewInfo.MatchedStringUseContains = true; 773 // 匹配表达式 774 cell.ViewInfo.MatchedString = this.View.GetExtraFilterText; 775 // 单元格状态 776 cell.State = GridRowCellState.Dirty; 777 // 更新行 778 e.ViewInfo.UpdateCellAppearance(cell); 779 // Draw 780 base.DrawRowCell(e, cell); 781 } 782 } 783 784 #endregion 785 }
原文:https://www.cnblogs.com/geping/p/10395934.html