ComponentOne
FlexGrid for WinForms
创建用户自定义控件 在用户自定义控件中添加 C1FlexGrid,我们使用 C1Command's MainMenu 和 DockingTab 控件去模拟 Excel 菜单和 Sheets Tab。使用 Label 和 TextBox 去展示当前选中索引和内容
拖拽 Excel 到 C1FlexGrid 是指 C1FlexGrid.DropMode 为 Manual。添加 C1FlexGrid DragEnter 和 DragDrop 事件。在 DragEnter 事件中更改 光标形状。我们我需要创建 “file” 变量去存储添加的文件名称string file; private void _flex_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true) e.Effect = DragDropEffects.Move; else e.Effect = DragDropEffects.None; }
现在我们需要在 DragDrop 事件中操作拖拽到 C1FlexGrid 中的 Excel 文件。Drop 事件可以捕捉到文件名和路径。这时我们可以使用 C1FlexGrid .LoadExcel 方法去加载 Excel 文件。但是我们需要去检查拖拽的文件是否为 BIFF8(.XlS) 或者 OpenXML(xlsx)文件。private void _flex_DragDrop(object sender, DragEventArgs e) { file = string.Empty; string[] str = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (string s in str) file += s; LoadExcelInFlex(); } private void LoadExcelInFlex() { if ((file.EndsWith(".xls")) || (file.EndsWith(".xlsx"))) { _flex.DataSource = null; _flex.LoadExcel(file); AddTabs(); UpdateFields(); } else { MessageBox.Show("Please select an Excel file"); } }