using DevExpress.Utils; using DevExpress.XtraEditors; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace IStation.WinFrmUI { /// /// GridView的拓展类 /// public static class GridContorlExtend { #region 默认 /// /// 普通视图设置 /// /// public static void ContextMenuStrip(this GridControl grid) { if (grid.Views.Count < 1) return; var view = grid.Views[0] as GridView; var contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(); var copyItem = new System.Windows.Forms.ToolStripMenuItem(); copyItem.Text = "复制单元格"; copyItem.Click += new System.EventHandler((object sender, EventArgs e) => { var text = view.GetFocusedDisplayText(); Clipboard.SetData(DataFormats.Text, text); }); contextMenuStrip.Items.Add(copyItem); var exportExcelItem = new System.Windows.Forms.ToolStripMenuItem(); exportExcelItem.Text = "导出Excel"; exportExcelItem.Click += new System.EventHandler((object sender, EventArgs e) => { var dlg = new SaveFileDialog(); dlg.Filter = "excel|.xlsx"; if (dlg.ShowDialog() != DialogResult.OK) return; var options = new DevExpress.XtraPrinting.XlsxExportOptionsEx(); options.ExportType = DevExpress.Export.ExportType.DataAware;//所见即所得 options.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text; view.ExportToXlsx(dlg.FileName, options); }); contextMenuStrip.Items.Add(exportExcelItem); grid.ContextMenuStrip = contextMenuStrip; } #endregion } }