这个示例展示了如何使用 IDataDictionary 接口,来改变显示在 IDE 报表设计器或 最终用户设计器 中的 Field List(字段列表) 项的名称。 展示了 DataSet 类的子类如何实现 IDataDictionary 接口。 那样,将把实现了 IDataDictionary 接口的数据源中的名称,自动取回到“字段列表”中。

Note注意

如果 IDataDictionary.GetObjectDisplayName 方法返回 null (在 Visual Basic 中为 Nothing) 或者 String.Empty,那么这些数据项将被隐藏。

下面的插图举例说明了“字段列表”在实现 IDataDictionary 接口之前(左图) 和之后(右图) 的外观。

任何被用作报表数据源的对象都可以实现此接口。

C#CopyCode image复制代码
using System;
using DevExpress.Data;
// ...

public class DataSet1 : DataSet, IDataDictionary {
//...

    // Implement the GetDataSourceDisplayName method.
    string IDataDictionary.GetDataSourceDisplayName() {
        return "Northwind Traders";
    }

    // Implement the GetObjectDisplayName method.
    string IDataDictionary.GetObjectDisplayName(string dataMember) {

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

        // Hide the 'Products' table, as its fields are accessible via
        // the 'CategoriesProducts' table only.
        if(dataMember.StartsWith("Products")) {
            return null;
        }

        // Find a dot in the data member's name. 
        string[] names = dataMember.Split('.');

        // Get a field name form the data member's name. 
        string fieldName = names[names.Length - 1];

        // 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;
    }

   // ...
}
Visual BasicCopyCode image复制代码
Imports System
Imports DevExpress.Data
' ...
 
Public Class DataSet1
   Inherits DataSet
   Implements IDataDictionary
   '...

    ' Implement the GetDataSourceDisplayName method.
    Function GetDataSourceDisplayName() As String _
    Implements IDataDictionary.GetDataSourceDisplayName
        Return "Northwind Traders"
    End Function

    ' Implement the GetObjectDisplayName method.
    Function GetObjectDisplayName(ByVal dataMember As String) As String _
    Implements IDataDictionary.GetObjectDisplayName

        ' Hide the data member, which name ends with 'ID'.
        If dataMember.EndsWith("ID") Then
            Return Nothing
        End If

        ' Hide the 'Products' table, as its fields are accessible via
        ' the 'CategoriesProducts' table only.
        If dataMember.StartsWith("Products") Then
            Return Nothing
        End If

        ' Find a dot in the data member's name. 
        Dim names As String() = dataMember.Split("."c)

        ' Get a field name form the data member's name. 
        Dim fieldName As String = names((names.Length - 1))

        ' Insert spaces between separate words of a field name.
        Return ChangeNames(fieldName)
    End Function

    Public Function ChangeNames(ByVal name As String) As String
        Dim result As String = String.Empty
        Dim isPrevLow As Boolean = False

        Dim symb As Char
        For Each 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) And isPrevLow Then
                result += " " + symb
            Else
                result += symb
            End If
            isPrevLow = [Char].IsLower(symb)
        Next symb
        Return result
    End Function
 
   ' ...
End Class

CodeCentralShow Me

在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E459。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。

Expand image参阅