Introduction
We'll see in this article how a user can customize a DataGrid, ordering columns or modifying their width, saving those changes for later use (in other words, when the program will show again a specific grid). In the following I'll present a class that does that, with a simple use scenario. The code was commented to be as clear as possible. The function is declared Shared to facilitate the static use of the class. We'll use mainly DataSet and DataTable classes, to benefit from the handy XML access methods, through which we'll realize the storage of column parameters.
Sample Class
- Imports System.Data
- Public Class emDataGridOptions
-
- Const _DATAGRID_OPTIONS_DIR As String = "C:\tmp\"
-
- Const _DATAGRID_OPTIONS_EXT As String = ".di"
-
-
-
-
- Private Shared Function ComposeGridOptionsFile(gridName) As String
-
- Return _DATAGRID_OPTIONS_DIR & gridName & _DATAGRID_OPTIONS_EXT
- End Function
-
-
-
-
- Public Shared Sub SaveGridOptions(dg As DataGrid)
-
- Dim columns As New DataSet(dg.Name)
- Dim coltable As New DataTable("columns")
-
-
-
- With coltable
- .Columns.Add("DisplayIndex", Type.GetType("System.Int32"))
- .Columns.Add("Width", Type.GetType("System.Double"))
- .Columns.Add("Visibility", Type.GetType("System.Int32"))
- .Columns.Add("SortDirection", Type.GetType("System.Int32"))
- End With
-
- columns.Tables.Add(coltable)
-
-
-
- For Each c As DataGridColumn In dg.Columns
- coltable.Rows.Add(New Object() {c.DisplayIndex,
- c.Width.DisplayValue,
- c.Visibility,
- c.SortDirection})
- Next
-
-
- columns.WriteXml(ComposeGridOptionsFile(dg.Name))
- End Sub
-
-
-
-
- Public Shared Sub LoadGridOptions(dg As DataGrid)
-
- If Not (IO.File.Exists(ComposeGridOptionsFile(dg.Name))) Then Exit Sub
-
-
- Dim columns As New DataSet(dg.Name)
- columns.ReadXml(ComposeGridOptionsFile(dg.Name))
-
-
- Dim ii As Integer = 0
- For Each c As DataGridColumn In dg.Columns
-
- c.DisplayIndex = columns.Tables(0).Rows(ii).Item("DisplayIndex")
- c.Width = Double.Parse(columns.Tables(0).Rows(ii).Item("Width"))
- c.Visibility = columns.Tables(0).Rows(ii).Item("Visibility")
-
-
-
- Dim sortDirection As Nullable(Of Integer) = Nothing
- If Not (columns.Tables(0).Rows(ii).Item("SortDirection").Equals(DBNull.Value)) Then sortDirection = Integer.Parse(columns.Tables(0).Rows(ii).Item("SortDirection"))
-
-
-
- If Not (sortDirection Is Nothing) Then
- c.SortDirection = sortDirection
- dg.Items.SortDescriptions.Add(New ComponentModel.SortDescription(c.SortMemberPath, c.SortDirection))
- dg.Items.Refresh()
- End If
- ii += 1
- Next
- End Sub
- End Class
Use scenario
The typical scenario is represented by entering a window (in case of parameters loading) and from closing it at the end of the program's lifecycle (for saving). Assuming we have a Window named MainWindow, on top of which we create a DataGrid named DataGrid1, we could use Loaded() and Closing() events (if in an event-driven model), respectively for the option's loading and saving. In those event's context, it will be possible to call on the static functions, feeding them the grid's name as a parameter.
- Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
-
- emDataGridOptions.LoadGridOptions(DataGrid1)
- End Sub
-
- Private Sub MainWindow_Closing(sender As Object, e As ComponentModel.CancelEventArgs) Handles Me.Closing
- emDataGridOptions.SaveGridOptions(DataGrid1)
- End Sub