FlexGrid not only lets you save the grid to various formats but also lets you load data from various formats such text, excel, XML or databases.
To load data from a text file, FlexGrid provides LoadGrid method of the Extensions class. The method has parameters that let you choose delimiter, encoding etc. You can also load text files saved using SaveGrid method. When loading text files, rows and columns are added to the grid if needed to accommodate the file contents. The method supports formats such as comma-delimited text files (CSV format), tab-delimited text files, and even MS Excel files (.xls).
Following code shows how to populate data in the WinForms FlexGrid by loading it from a text file.
To load grid from an excel file, you can use the abovementioned LoadGrid method and simply set the format parameter to FileFormatEnum.Excel. You don't need to have Microsoft Excel installed on your computer. However, the LoadGrid method can only load the data from first worksheet of a workbook.
To get an additional control over how you load your data from an excel file, you can use the LoadExcel method instead. In this case, the process of loading excel files converts most data types and formatting information, including row and column dimensions, fonts, colors, formats, and cell alignment. However, there are still some exceptions. For example, the grid loads the values in excel cells, but cannot load the underlying formulas. Other features such as frozen and merged cells, images, data maps, and cell borders are not translated either.
Use the code below to load contents in the WinForms FlexGrid from an excel file.
To load grid data from a database, you can use the DataReader objects. This process is different from data binding, which keeps a live connection between one or more controls and the underlying data source.
Below code demonstrates how to load contents in the WinForms FlexGrid from the database.
To deserialize grid contents from an XML document, you can simply call ReadXml method of the C1FlexGrid class. The ReadXml method allows you to parse path of the XML document as its parameters along with the choice of the FlexGrid element to be loaded using XmlOptions enumeration. Using XmlOptions parameter, you can load ColumnInfo, RowInfo, Ranges, Control, Styles, Maps, Tree, Glyphs, and Images from XML.
Use the following code to load contents in the WinForms FlexGrid from an XML document.
Further, sorted, filtered, or grouped elements of FlexGrid that are saved to an XML file can be retrieved using the ReadXml method. The XML file loads the elements in the same order as they were stored.