Blogs

Paul Kimmel's Blog

Creating and Using a Persistent Repository with Code

     

DevExpress controls are feature rich. Hundreds of properties can present a challenge for new programmers, but we are proud of the rich and diverse features that let you customize almost every aspect of our controls. Here is a couple of things newer users may not know. DevExpress controls support skins for WinForms and WebForms (ASP.NET). This means by setting a skin through the LookAndFeel property you can broadly set the look in one shot. Container controls like the XtraGrid use editors for editing data, like Date Fields. Instead of a plain text entry field, if you bind a DateTime value to a column then a DateEdit control (with a calendar) will be displayed and available when you want to edit the data. The second piece of information some of you may not know about is that DevExpress created the Editor Repository. The Editor Repository let’s you define what an editor will look like and how it will behave and then assign that repository editor wherever you want that particular look and feel. So, instead of changing three date column’s properties for example, simply define a repository DateEdit value and associate it with the DateTime column’s ColumnEdit property. All of the changes are automagically populated when the editor is in play. If you need to make changes just change the repository editor’s properties and all associated editors in the container will get the changes. (This beats the heck out of meticulously and tediously changing multiple properties for multiple editors by hand.)

If you want repository editors for a single control, like a single XtraGrid, then you can use the internal repository capabilities available in the XtraGrid designer. If you want external repository editors for spanning multiple container controls like two XtraGrid or an XtraGrid and an XtraTreeList then you can use an external repository editor via the PersistentRepository component. Repository settings whether internal or external are persistent—stored—so you make the changes in one place and the changes are stored and used each time your application is run.

You also have the option of creating repository items with code. The code in Listing 1 demonstrates how to create an instance of the PersistentRepository, a PersistentItem—a DateEdit repository item—set that object’s properties and then associate the repository editor with all of the DateTime columns (from the Orders table in the Northwind database.)

Listing 1: Sample code that demonstrates how to create and use a DateEdit item in the PersistentRepository.

Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraGrid.Columns

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  'TODO: This line of code loads data into the 'NorthwindDataSet.Orders' table. You can move, or remove it, as needed.
  Me.OrdersTableAdapter.Fill(Me.NorthwindDataSet.Orders)

  CreateDateEditRepositoryItem()

End Sub

Private repository As PersistentRepository

Private Sub CreateDateEditRepositoryItem()

  repository = New PersistentRepository(Me)
  Dim repositoryItem As RepositoryItem = repository.Items.Add("DateEdit")
  repositoryItem.Name = "repositoryDateEdit"
  repositoryItem.Appearance.BackColor = Color.GreenYellow

  Dim DateEditor As DevExpress.XtraEditors.DateEdit = _
    DirectCast(repositoryItem.CreateEditor(),  _
    DevExpress.XtraEditors.DateEdit)

  DateEditor.Properties.LookAndFeel.SetSkinStyle("Money Twins")
  DateEditor.Properties.LookAndFeel.UseDefaultLookAndFeel = False

  Dim column As GridColumn

  For Each column In GridView1.Columns
    If (column.FieldName.Contains("Date")) Then
      column.ColumnEdit = repositoryItem
    End If
  Next

End Sub

End Class

CreateEditRepositoryItem creates a new instance of PersistentRepository and adds a DateEdit item to the repository. The Name, Appearance.BackColor are set. The middle block of code creates the DateEdit instance and applies a known skin. Finally, the for loop walks through each column in the GridView—the default view in the XtraGrid—and every Date column’s ColumnEdit property is assigned the to the repositoryItem, consequently adopting the properties defined in the repositoryItem (see Figure 1 for the results).

image
Figure 1: Shows the editor background and the applied skin for the DateEdit control.

To create the Demo solution drag and drop an XtraGrid onto a WinForm and use the smart tags menu to bind to the Northwind database’s Orders table.

Sure, there are a lot of possibilities when it comes to customizing DevExpress controls. But, if you use skins or the repository editor then you can set them in one place. Doing one of anything isn’t hard at all.

Published Mar 12 2010, 09:13 PM by Paul Kimmel (DevExpress)
Bookmark and Share

Comments

 

Sigurd Decroos said:

Another nice solution is to make a form, place a persistent repository on it, make the editors wanted with all settings. Then, make a public readonly property for each of the editors. Do the same for the persistent repository. If you put this in a seperate dll, you could make a global instance of the form (non-visible) and use the repository through whole your project, no matter how many forms or grids or whatever you have. The public property of the persistent repository can be used as the external repository of a grid (done in code, like the form_load). I'm working on a project which uses this technique with more than 500 forms now.

March 14, 2010 9:24 AM
 

Steve Sharkey said:

Nice and helpful article...

I don't suppose you'd liek to ellaborate on:

"If you want external repository editors for spanning multiple container controls like two XtraGrid or an XtraGrid and an XtraTreeList then you can use an external repository editor via the PersistentRepository component."?

March 16, 2010 3:17 AM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.