Example E459
Visible to All Users

Reporting for WinForms - How to provide custom names for the Field List data items

This example demonstrates how to use the IDisplayNameProvider interface to change the names of the Field List items shown in both the Visual Studio and End-User report designers.

This example shows an implementation of the IDisplayNameProvider interface for the DevExpress.DataAccess.Sql.SqlDataSource class, which allows the Field List to automatically retrieve the custom names from a data source that implements this interface.

Files to Review

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Form1.cs(vb)
C#
using System; using System.Windows.Forms; using DevExpress.DataAccess.Sql; using DevExpress.XtraReports.UI; // ... namespace docCustomDataItemsNames { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { XtraReport1 report = new XtraReport1(); report.DataSource = new MySqlDataSource((SqlDataSource)report.DataSource); ReportDesignTool designTool = new ReportDesignTool(report); designTool.ShowDesignerDialog(); } } }
MySqlDataSource.cs(vb)
C#
using System; using DevExpress.Data; using DevExpress.DataAccess.Sql; namespace docCustomDataItemsNames { public class MySqlDataSource : SqlDataSource, IDisplayNameProvider { public MySqlDataSource() { } public MySqlDataSource(SqlDataSource copyFrom) { this.LoadFromXml(copyFrom.SaveToXml()); } string IDisplayNameProvider.GetDataSourceDisplayName() { // Substitute the default datasource display name // with a custom one. return "Northwind Traders"; } string IDisplayNameProvider.GetFieldDisplayName(string[] fieldAccessors) { // Get a field name form the data member's 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 // via the 'CategoriesProducts' relation only. if (fieldAccessors[0].StartsWith("Products")) { return null; } // Insert spaces between separate words of a field name. return ChangeNames(fieldName); } public string ChangeNames(string name) { string result = string.Empty; bool isPrevLow = false; foreach (char symb in name) { // Check if a character is of upper case. // To avoid spaces inside abbreviations, // check if the previous character is of upper case, too. if (Char.IsUpper(symb) && isPrevLow) { result += " " + symb; } else { result += symb; } isPrevLow = Char.IsLower(symb); } return result; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.