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).
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.