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
- Form1.cs (VB: Form1.vb)
- MySqlDataSource.cs (VB: MySqlDataSource.vb)
More Examples
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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();
}
}
}
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;
}
}
}