Blogs

Paul Kimmel's Blog

More Editor Repository Goodness

     

A little boy stops a policeman on the 48th street and asks, ‘how do I get to Carnegie Hall?” The police man responds, “practice! practice! practice!”

That’s how I learned C++ twenty years ago. I read everything I could get my hands on, re-wrote and tweaked all the code I could find and practiced. (Let me tell you that overloaded operators like >> for output gave me fits. I just happened to be taken a C++ class at Moo U—Go State!—and the teaching assistant was trying to explain templates (generics). She said literally, “don’t worry about templates; they aren’t that important.”. Of course, never knowing when to leave well enough alone I tried to correct her. Uh, yeah, they are pretty important. I didn’t say it like that, but I am sure though I was trying to be helpful that’s probably how she took it. (Some times the TA’s try to stay one step ahead in the book. Some times it shows a little.) Anyway I still learn that way. I bisect, dissect, vivisect and digest something until I have got it plugged in my head. Fortunately, we work in an industry where there is always something to learn.

If you haven’t seen Amanda’s great presentation of my video Benefits of the Editor Repository at http://tv.devexpress.com/#XtraGridRepository.movie then it is worth checking out (and you can see that I have been chewing on editor repositories lately). The example in this blog demonstrates one way to use the in-place editor repository for the XtraTreeList.

If you are one of those people saying what is an in-place editor repository, here are the Cliff notes.  One of the cool features about DevExpress container controls like the XtraTreeList and XtraGrid is that these controls automatically look at the data and try to figure out the best editor control. So instead of editing all items with a simple text control you get a calendar for DateTime values and there about 30 other kinds of editors available depending on the data type. The in-place editor repository is accessible in the designer. The in-place editor repository let’s you expressly pick an editor, assign values to its properties and then associate that editor with columns the the ColumnEdit property. So, suppose you have two combo boxes representing states (or regions). You can define one editor and its properties and associate it with the two date columns; instead of meticulously setting properties and items twice (or many times) just define one editor and reuse it. Making changes also then only has to happen in one place, reducing the amount of time you spend tweaking properties.

The example uses the Northwind.Orders table. An RepositoryItemComboBox is defined and associated with the Region column. The code-behind demonstrates how to use LINQ to DataTables to populate the combobox. Here are the steps:

  1. Create a WinForms application
  2. Place an XtraTreeList (TreeList from the DevExpress Data tab) from the Toolbox onto the form and dock it
  3. Click on the TreeList smart tag (upper right corner) and expand Choose Data Source
  4. Click Add Project Data Source and walk through the wizard picking the Orders column. (You have a basic functioning at application at this point.)
  5. Right-click on the TreeList and click Run Designer
  6. On the Main tab click In-place Editor Repository, next to the Add button click the drop down ComboBoxEdit. (This will add repositoryItemCombobox1 to the repository-see Figure 1.)
  7. In the Main tab click Columns
  8. Click on the ShipRegion Column and pick repositoryItemComboBox1 for the ShipRegion’s ColumnEdit property—see Figure 2.
  9. Close the designer
  10. Switch to the form’s code-behind and implement the code to populate the repository combobox—see Listing 1.

image
Figure 1: Add and configure the editors you want in the Property editor for the TreeList.

image 
Figure 2: Associate the configured editor with the column or columns that will use that editor.

Listing 1: The code-behind populates the repository editor item using ADO.NET and LINQ to DataTables.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RepositoryTreeList
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      // TODO: This line of code loads data into the 'northwindDataSet.Orders' table. You can move, or remove it, as needed.
      this.ordersTableAdapter.Fill(this.northwindDataSet.Orders);
      LoadRepositoryEditor();
    }

    private void LoadRepositoryEditor()
    {
      //treeList1.RepositoryItems[0] is repositoryItemComboBox too

      string connectionString =
        @"Data Source=FRANKLIN\SQLEXPRESS;Initial Catalog=Northwind;"+
        "Integrated Security=True";

      string sql = "SELECT DISTINCT ShipRegion FROM Orders WHERE ShipRegion IS NOT NULL";
      DataTable table = new DataTable();

      using (SqlConnection connection = new SqlConnection(connectionString ))
      {
        connection.Open();
        SqlCommand command = new SqlCommand(sql, connection);
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        adapter.Fill(table);
      }

      var regions = from region in table.AsEnumerable()
                    select region.Field<string>("ShipRegion");
      repositoryItemComboBox1.Items.AddRange(regions.ToArray<string>());   
    }
  }
}

The first two thirds of LoadRepositoryEditor uses vanilla ADO.NET to populate a DataTable. The LINQ to DataTable query is a variation on using a SqlDataReader and while loop. I like this approach a little better. The repositoryItemComboBox is an actual control you can access directly or through the treeList.RepositoryItems property. The LINQ query returns an IEnumerable<string> object and Items.AddRange adds all the items at once.

The video and other blog talk about saving time with repository items by setting properties just once—in the repository editor. That will save you a lot of repetitive effort. This approach let’s you write (or use code) just once to populate a ComoBox with selectable items. The alternative would be using an event handler for—ShowingEditor—figuring out what editor is being shown for every editor and then populating the ComboBox. Again this will save you time and speed up your application because users won’t be waiting.

A spin on this approach would be using a worker thread to populate the editor at startup. Figure 3 shows the TreeList with the ComboBox dropped and the items present. A real world solution would be to look up and limit Cities based on Region or smart-pick a Region based on the City picked and extend that intelligence to the postal code too.

image
Figure 3: The repositoryItemComboBox with the populate region values.

Published Mar 18 2010, 08:59 PM by Paul Kimmel (DevExpress)
Bookmark and Share

Comments

 

Neal said:

Now I'd like to see a demo on creating a custom repository editor.  Here's an example for you:

Pilots logging flight time sometimes want to log in a format of 1 + 30 for one hour and 30 minutes.  They may also log it as 1.5 or 1:30.  Regardless of this format the underlying data should be recorded as 90 minutes to the database.  This same scenario can be applied to time tracking applications, i.e. work time, time sheets, etc.  

Show us how to create our own repository items and feel free to use the above scenario.  

March 18, 2010 6:38 PM
 

Paul Kimmel (DevExpress) said:

Pilot log editor repository item. That's a good sample, one near and dear to my heart. Thanks for the idea.

March 18, 2010 8:37 PM
 

Sigurd Decroos said:

How about a video of a multi checkbox multicolumn dropdown gridlookupedit. Add to that an editor with a treelist as dropdowncontrol :)

March 19, 2010 6:00 AM
 

Paul Kimmel said:

Neal:

I have a solution for you. Will post as a blog today (or tonight).

Paul

March 19, 2010 12:58 PM
 

Neal said:

Thanks Paul.  Looking forward to it.  I think many would benefit on learning how to create their own repository editors, it's a great capability!

March 19, 2010 3:09 PM
 

Sigurd Decroos said:

Paul: what about mine? Another one: a multicheck imagecombobox (urgently needed).

March 19, 2010 3:41 PM
 

Paul Kimmel (DevExpress) said:

Sigurd:

Let me see if what I can do.

Paul

March 19, 2010 4:22 PM
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.