Skip to main content

Provide Custom Names to the Field List Items

  • 3 minutes to read

This example demonstrates how to change the item names in the Field List. It applies to the Visual Studio Report Designer and the End-User Report Designer for WinForms.

To customize the Field List, assign a custom data source that implements the IDisplayNameProvider interface to the report. This interface has two methods - the GetDataSourceDisplayName() method returns the new data source name, and the GetFieldDisplayName method returns the new field name. Note that the Field List does not show fields for which the GetFieldDisplayName method returns null or Empty.

The following image shows the original Field List (on the left) and the customized Field List (on the right):

IDataDictionary1

using System;
using DevExpress.Data;
using DevExpress.DataAccess.Sql;
// ...

    public class MySqlDataSource : SqlDataSource, IDisplayNameProvider
    {
        public MySqlDataSource() {
        }

        public MySqlDataSource(SqlDataSource copyFrom) {
            this.LoadFromXml(copyFrom.SaveToXml());
        }

        // ...
        string IDisplayNameProvider.GetDataSourceDisplayName() {
            // Replace the default datasource display name
            // with a custom name.
            return "Northwind Traders";
        }

        string IDisplayNameProvider.GetFieldDisplayName(string[] fieldAccessors) {
            // Get the field name from the data member name. 
            string fieldName = fieldAccessors[fieldAccessors.Length - 1];

            // Hide the data member if its name ends with 'ID'.
            if (fieldName.EndsWith("ID")) {
                return null;
            }

            // Hide the 'Products' table, because its fields are accessible 
            // from the 'CategoriesProducts' relation only.
            if (fieldAccessors[0].StartsWith("Products")) {
                return null;
            }

            // Insert spaces between separate words in the field name.
            return ChangeNames(fieldName);
        }
        public string ChangeNames(string name) {
            string result = string.Empty;
            bool isPrevLow = false;

            foreach (char c in name) {
                // Check if a character is upper case.
                // To avoid spaces between the letters of acronyms, 
                // check if the previous character is upper case.
                if (Char.IsUpper(c) && isPrevLow) {
                    result += " " + c;
                }
                else {
                    result += c;
                }
                isPrevLow = Char.IsLower(c);
            }
            return result;
        }
        // ...
    }
See Also