Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Define Display Labels as Attributes in Business Entity Class in C#

0.00/5 (No votes)
25 Nov 2015 1  
A way to define display names for class members in BE class that can be used as table headers.

Introduction

Assume that in your Business Entity (BE) class, you have certain properties to match with table fields. I want to expose descriptive name for each of these properties. For example, to show it as column header in grid.

For example, there is a property called FirstName. I want to expose its descriptive name as First Name.

Background

You must be familiar with Object oriented concepts in order to understand this tip.

Using the Code

In BE class, define the descriptive name for the property.

[DisplayName("First Name"), Description("First Name of the Member")]
public string FirstName
{
    get { return _firstName; }
    set { _firstName = value; }
}

You can read these details of each property as below:

PropertyDescriptorCollection propertiesCol = TypeDescriptor.GetProperties(objectBE);

PropertyDescriptor property;

for (int i = 0; i < propertiesCol.Count; i++)
{
    property = TypeDescriptor.GetProperties(objectBE)[i];

    /*
    // Access the Property Name, Display Name and Description as follows
    property.Name          // Returns "FirstName"
    property.DisplayName   // Returns "First Name"
    property.Description   // Returns "First Name of the Member"
    */
}
// *here objectBE is the object instance of BE class.

Points of Interest

This is pretty useful when using ASP.NET WebForms applications. However, this may not be required if you are using ASP.NET MVC application.

History

  • 2015-11-25 - Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here