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

  1. Imports System.Data
  2. Public Class emDataGridOptions
  3. ' We declare a certain path in which we'll store the grid's options file
  4. Const _DATAGRID_OPTIONS_DIR As String = "C:\tmp\"
  5. ' As file extension, we'll use ".di"
  6. Const _DATAGRID_OPTIONS_EXT As String = ".di"
  7. ' ===============================================================================
  8. ' Private function to return the full path of options file
  9. ' ===============================================================================
  10. Private Shared Function ComposeGridOptionsFile(gridName) As String
  11. ' Simple concatenation of directory, grid's name, and default extension
  12. Return _DATAGRID_OPTIONS_DIR & gridName & _DATAGRID_OPTIONS_EXT
  13. End Function
  14. ' ===============================================================================
  15. ' Public shared sub to save DataGrid's option
  16. ' ===============================================================================
  17. Public Shared Sub SaveGridOptions(dg As DataGrid)
  18. ' We'll create a DataSet with the same name of the Grid, generating inside it a DataTable named "columns"
  19. Dim columns As New DataSet(dg.Name)
  20. Dim coltable As New DataTable("columns")
  21. ' Here we'll save only some parameters, such as the visibility, the sort direction, etc.
  22. ' we must create in the DataTable a number of columns equals to the number of property we intend to save.
  23. With coltable
  24. .Columns.Add("DisplayIndex", Type.GetType("System.Int32"))
  25. .Columns.Add("Width", Type.GetType("System.Double"))
  26. .Columns.Add("Visibility", Type.GetType("System.Int32"))
  27. .Columns.Add("SortDirection", Type.GetType("System.Int32"))
  28. End With
  29. columns.Tables.Add(coltable)
  30. ' We execute a loop on the DataGrid's columns, adding to the DataTable a number of rows equals to the
  31. ' count of DataGrid's columns, each one of them exposing the value of the property itself
  32. For Each c As DataGridColumn In dg.Columns
  33. coltable.Rows.Add(New Object() {c.DisplayIndex,
  34. c.Width.DisplayValue,
  35. c.Visibility,
  36. c.SortDirection})
  37. Next
  38. ' Then, using the WriteXml() method, we save an XML file which contains the columns extracted values
  39. columns.WriteXml(ComposeGridOptionsFile(dg.Name))
  40. End Sub
  41. ' ===============================================================================
  42. ' Public shared sub to load DataGrid's option
  43. ' ===============================================================================
  44. Public Shared Sub LoadGridOptions(dg As DataGrid)
  45. ' We check if the options file exists...
  46. If Not (IO.File.Exists(ComposeGridOptionsFile(dg.Name))) Then Exit Sub
  47. ' A new DataSet will be generated, and then populated using the readXml() method
  48. Dim columns As New DataSet(dg.Name)
  49. columns.ReadXml(ComposeGridOptionsFile(dg.Name))
  50. ' We execute a loop on grid's columns: for each of them, we read and apply the corresponding property's value from the table in DataSet
  51. Dim ii As Integer = 0
  52. For Each c As DataGridColumn In dg.Columns
  53. c.DisplayIndex = columns.Tables(0).Rows(ii).Item("DisplayIndex")
  54. c.Width = Double.Parse(columns.Tables(0).Rows(ii).Item("Width"))
  55. c.Visibility = columns.Tables(0).Rows(ii).Item("Visibility")
  56. ' The SortDirection case is a particular one: if no sort was set on a column, there will be no corresponding entity
  57. ' in the XML file. So, we must manage possible Nothing values
  58. Dim sortDirection As Nullable(Of Integer) = Nothing
  59. If Not (columns.Tables(0).Rows(ii).Item("SortDirection").Equals(DBNull.Value)) Then sortDirection = Integer.Parse(columns.Tables(0).Rows(ii).Item("SortDirection"))
  60. ' If SortDirection is specified, we need to execute the actual sort. We compile the list of sort's descriptions through the read value, plus the
  61. ' column's SortMemberPath, refreshing the grid's Items.
  62. If Not (sortDirection Is Nothing) Then
  63. c.SortDirection = sortDirection
  64. dg.Items.SortDescriptions.Add(New ComponentModel.SortDescription(c.SortMemberPath, c.SortDirection))
  65. dg.Items.Refresh()
  66. End If
  67. ii += 1
  68. Next
  69. End Sub
  70. 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.
  1. Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
  2. 'Eseguire qui le operazioni preliminari, tra cui il binding dei dati sulla griglia
  3. emDataGridOptions.LoadGridOptions(DataGrid1)
  4. End Sub
  5. Private Sub MainWindow_Closing(sender As Object, e As ComponentModel.CancelEventArgs) Handles Me.Closing
  6. emDataGridOptions.SaveGridOptions(DataGrid1)
  7. End Sub