Skip to main content Skip to footer

Drag Excel Files on C1FlexGrid

When loading Excel files in Windows applications, the most commonly used option is to use Streams to read/write the files. Another easier method would be dragging the excel file directly from the explorer onto the grid. This sample uses the C1Flexgrid’s dragdrop feature to load excel files directly from explorer to the grid in an Excel like UI. To start with create a user control and place C1Flexgrid onto the application. For the menu and the tabs, we will be using C1Command’s MainMenu and DockingTab controls. Also place a label and textbox to display the selected cell and cell data. Place them as shown in the picture below :

Drag Drop in C1FlexGrid

To enable drop feature in C1Flexgrid set the C1Flexgrid.DropMode to Manual. Next step is to subscribe the C1Flexgrid’s DragEnter and DragDrop events. In the Dragenter event, change the mouse cursor when the file is being dropped. We also need to create a ‘file’ variable of string type to save the file name:

_

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;

}



_

Now we need to wire the DragDrop event to handle the file being dropped. The drop event would fetch the file name and path of the file being dropped. This we can use in our C1Flexgrid.LoadExcel method to load the excel file. But here we need to check whether the dragged file is in BIFF8 (.xls) or OpenXML(.xlsx) format.

_

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();

}

//Load Excel File in C1Flexgrid

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");

}

}



_

The LoadExcelInFile() method uses two methods : AddTabs() and UpdateFields(). These methods are used to create an excel like spreadsheet on the form. The AddTabs() method adds the tabs with available sheets in the dragged excel file. The UpdateField() method would update the selected cell and selected cell data in the label and textbox respectively.

_

//Method to update the fields in the UI

private void UpdateFields()

{

SetRowColNames();

int selcol = _flex.Col;

int selrow = _flex.Row;

label1.Text = _flex.GetDataDisplay(0, selcol) + selrow.ToString();

try { textBox1.Text = _flex.GetDataDisplay(selrow, selcol); }

catch(Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message);}

}



_

The SetRowColNames() method to rename the cells in the C1Flexgrid based on the loaded excel file.

Loading Selected Sheet in C1FlexGrid

Now we need to load the selected Sheet in the C1Flexgrid. For this we need to subscribe the C1DockingTab.SelectedIndexChanged event.

_

//Loads the selected sheet

private void _sheetTabs_SelectedIndexChanged(object sender, EventArgs e)

{

if (_sheetTabs.TabPages.Count > 0)

{

string sheetname = _sheetTabs.SelectedTab.Text;

_flex.LoadExcel(file, sheetname);

UpdateFields();

}

}



_

You can also have a menu with Load and save options to load and save the excel files in/from C1Flexgrid. Download Sample

MESCIUS inc.

comments powered by Disqus