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:
- Create a WinForms application
- Place an XtraTreeList (TreeList from the DevExpress Data tab) from the Toolbox onto the form and dock it
- Click on the TreeList smart tag (upper right corner) and expand Choose Data Source
- Click Add Project Data Source and walk through the wizard picking the Orders column. (You have a basic functioning at application at this point.)
- Right-click on the TreeList and click Run Designer
- 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.)
- In the Main tab click Columns
- Click on the ShipRegion Column and pick repositoryItemComboBox1 for the ShipRegion’s ColumnEdit property—see Figure 2.
- Close the designer
- Switch to the form’s code-behind and implement the code to populate the repository combobox—see Listing 1.
Figure 1: Add and configure the editors you want in the Property editor for the TreeList.
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.
Figure 3: The repositoryItemComboBox with the populate region values.