Introduction
We'll see in this tip how a user can customize a DataGrid
, ordering columns or modifying their width, saving those changes for a later use (i.e., when the program will show again a particular grid). Below I'll present a class apt to that end, with a simple use scenario. The code was commented to be the most clear as possible. Function was 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 columns 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 most typical scenario is certainly represented by entering a Window (in case of parameters loading), and from closing it at the end of the program's life-cycle (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 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
History
- 2015-01-03: First release for CodeProject