ComponentOne TrueDBGrid for WinForms is a robust, easy-to-use .NET grid control that allows you to create complex bound and unbound grid applications quickly. TrueDBGrid for WinForms's strength is data binding; with an ADO.NET managed database interface. C1TrueDBGrid includes dozens of advanced features like data presentation, data access, filtering, sorting, etc. Sometimes it is required to save the sorting and filtering condition of C1TrueDBGrid in such a way that it can be used for future purposes. Basically, the idea is to save the state of the grid so that it can be retained as it is. C1TrueDBGrid provides functionality to export the grid using ExportTo method which exports the data of the grid. In this blog, we will discuss saving and retaining the complete state of C1TrueDBGrid including DataSource, Layout, Filter and SortDefinition in two steps.

  • Step 1- Save the state of C1TrueDBGrid into files.
  • Step 2- Load these files into another C1TrueDBGrid so as to restore the last saved state of the grid.

Yes, complete state of C1TrueDBGrid can be saved in files.

avi_SaveLoadC1TrueDBGrid

Step 1

Basically, the idea is to save the DataSource of C1TrueDBGrid in an Excel file, Layout, Filter and SortDefinition in Text files. So, the complete grid gets saved in three files. The approach includes the following steps: 1. First the DataSource of the C1TrueDBGrid is to be saved in an Excel file using 'ExportToExcel' method. To use this method, you need to add 'C1.Win.C1Report.4.dll' assembly in the References of the project.

C1TrueDBGrid1.ExportToExcel("..\\..\\DataSource.xls")

2. Now comes the point to save the Layout of the C1TrueDBGrid in a Text file. This can be done by using the 'SaveLayout' method of the grid.

C1TrueDBGrid1.SaveLayout("..\\..\\Layout.txt")

3. After saving the DataSource and the Layout, declare a String array and save the Filter and Sort Definitions in it. Then the text from the array will be written to the Text file. Here is the code snippet for implementing the same: Declare the String array:

Dim strFilterSortDef(1) As String

Use the 'AfterSort' event in order to save the sorting of C1TrueDBGrid:

Private Sub C1TrueDBGrid1_AfterSort(sender As Object, e As FilterEventArgs) Handles C1TrueDBGrid1.AfterSort  
strFilterSortDef(1) = e.Condition.ToUpper()  
End Sub

Save the Filter definition on Click event of a Button:

strFilterSortDef(0) = C1TrueDBGrid1.Splits(0).FilterDefinition  
System.IO.File.WriteAllLines("..\\..\\FilterSortDefinition.txt", strFilterSortDef)

Now the grid has been saved in three files which will be residing in the project folder.

Step 2

Here comes the next step after saving the C1TrueDBGrid into files. How these files will be loaded into another C1TrueDBGrid? To implement it, load the excel file of DataSource, Text files of Layout, Filter and Sort Definitions into a grid. 1. Use ADO.NET functions to load the Excel file of DataSource into C1TrueDBGrid.


Dim dt As DataTable  
conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\\..\\\DataSource.xls;Extended Properties=""Excel 12.0;HDR=YES;IMEX=1""")  
conn.Open()  
dt = New DataTable()  
adap = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", conn)  
adap.Fill(dt)  
C1TrueDBGrid2.DataSource = dt

2. After loading the DataSource in the grid, the Layout of the last saved grid will be loaded to the new C1TrueDBGrid. It can be achieved by using the 'LoadLayout' method of C1TrueDBGrid.

C1TrueDBGrid2.LoadLayout("..\\..\\Layout.txt")

3. After loading the DataSource and the Layout in the grid, the third step is to load the Filter and Sort Definitions in the grid. Here is the code snippet for implementing the same:

strFilterSortDef = System.IO.File.ReadAllLines("..\\..\\FilterSortDefinition.txt")  
C1TrueDBGrid2.Splits(0).FilterDefinition = strFilterSortDef(0)  
dt.DefaultView.Sort = strFilterSortDef(1)

The new C1TrueDBGrid has restored the complete state of the previous grid. But the new grid will not be showing the Sort glyphs in the header of the columns on which the sorting is applied. This can be implemented using the Bitmap images. Check which sorting is applied on which column and apply the image accordingly.


Dim strArr() As String  
strArr = strFilterSortDef(1).Split(" ")  
If strArr(0).Length > 0 Then  
    strArr(0) = strArr(0).Trim("[", "]")  
    If strArr.Length > 1 Then  
        If strArr(1).Equals("DESC") Then  
            C1TrueDBGrid2.Splits(0).DisplayColumns(strArr(0)).HeadingStyle.ForegroundImage = Me._sortdn  
      C1TrueDBGrid2.Splits(0).DisplayColumns(strArr(0)).HeadingStyle.ForeGroundPicturePosition = ForeGroundPicturePositionEnum.RightOfText  
        End If  
    Else  
        C1TrueDBGrid2.Splits(0).DisplayColumns(strArr(0)).HeadingStyle.ForegroundImage = Me._sortup  
      C1TrueDBGrid2.Splits(0).DisplayColumns(strArr(0)).HeadingStyle.ForeGroundPicturePosition = ForeGroundPicturePositionEnum.RightOfText  
    End If  
End If  

This will set the image on the sorted columns according to the type of sorting performed on the column. And, we will get the exactly same grid :-) Download Sample - CS Download Sample - VB