using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Grid; namespace IStation.Win { /// /// 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 ContextMenuStrip(); var copyItem = new ToolStripMenuItem(); copyItem.Text = "复制单元格"; copyItem.Click += new EventHandler((sender, e) => { var text = view.GetFocusedDisplayText(); Clipboard.SetData(DataFormats.Text, text); }); contextMenuStrip.Items.Add(copyItem); var exportExcelItem = new ToolStripMenuItem(); exportExcelItem.Text = "导出Excel"; exportExcelItem.Click += new EventHandler((sender, 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 } }