Click here to Skip to main content
15,558,027 members
Articles / Programming Languages / C#
Posted 31 Jan 2011

Tagged as


47 bookmarked

Data Entry Control

Rate me:
Please Sign up or sign in to vote.
4.86/5 (23 votes)
31 Jan 2011CPOL3 min read
Quick reflection based data entry control in one line of code


This is another piece of code born out of necessity which I didn't find a solution for on the web, (well not exactly because I did, but I wanted to do it anyway). The long and short of it is a way to simplify the UI design aspects of data entry forms and delegate all the grunt work to the control, i.e., data entry control shows this object.

What It Does

The control takes whatever you give it in the DataSource property and displays a control that suits the underlying data types for data entry.

Some neat features are the following:

  • The control recognizes data types and displays the appropriate editing control, i.e., TextBox, ComboBox, CheckBox and DatePicker.
  • Below the data entry portion of the control is an area which displays a description for the property.
  • If you have used the Description, DisplayName and Browsable attributes on your data entity, the control will use them.
  • If you have set the RightToLeft property, the control will display and align correctly.
  • Mouse over changes the child control background.
  • The control will auto populate combo boxes from the underlying enumeration property.

Requirements and How to Use It

All you have to do to use this control is to create the object you want to edit and give it to the controls DataSource property and everything magically happens.

Customer customer = new Customer();
private void Form2_Load(object sender, EventArgs e)
     dataEntry1.DataSource = customer;

If you like to use the full feature for the control, try the Description, DisplayName and Browsable attributes like this:

[Description("Please enter the Customer Name here")]
[DisplayName("customer name")]
public string Name { get; set; }        
public decimal number { get; set;}


But there are alternatives you say… and yes, there are a few of them below:

Alternative 1: Visual Studio Designer


Probably the most accessible solution is to use the data sources toolbox and bind to the object in question and drag the fields onto your forms like the picture above, and let Visual Studio create the controls straight on to your forms. Then you can tweak the controls to your liking.


  • Generates a lot of code boiler plate which bloats your application.
  • You will probably waste a lot of time tweaking the form.
  • Change your data source and you have to go through all that again.
  • Doesn’t help you with enumerations, and you have to handle that yourself.

Alternative 2: PropertyGrid Control


This is a pretty good, and probably the most undervalued control in the toolbox which handles all sorts of data types as you have all seen when using Visual Studio. It’s very good and you can get it up and running in one line of code.

private void Form1_Load(object sender, EventArgs e)
     propertyGrid1.SelectedObject = new Customer();


  • Tab keys don’t work when you want to move between the properties.
  • Properties get bunched together, and you have no control over the spacing.

Alternative 3: Commercial Products

There are commercial products that do similar things to what is shown here. You will have to search around to find them as I am not allowed to say here.


  • You have to pay.

Points of Interest

You can extend the control to show your own data types very easily by inserting the appropriate code in the switch statement. Also for the ability to alter the viewing cosmetics, you can change the PropertyItemControl to your own liking. It's basically the below picture.


There is also a Validation label on the above control which you can use to show required or invalid prompts.


  • Initial release: January 23 2011


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Architect -
United Kingdom United Kingdom
Mehdi first started programming when he was 8 on BBC+128k machine in 6512 processor language, after various hardware and software changes he eventually came across .net and c# which he has been using since v1.0.
He is formally educated as a system analyst Industrial engineer, but his programming passion continues.

* Mehdi is the 5th person to get 6 out of 7 Platinum's on Code-Project (13th Jan'12)
* Mehdi is the 3rd person to get 7 out of 7 Platinum's on Code-Project (26th Aug'16)

Comments and Discussions

GeneralMy vote of 1 Pin
KingofHeartz2-Jun-20 1:31
KingofHeartz2-Jun-20 1:31 
GeneralFinally! Pin
JakeSays2-Mar-14 18:13
JakeSays2-Mar-14 18:13 
GeneralMy vote of 5 Pin
Armando Airo'11-Sep-13 3:37
Armando Airo'11-Sep-13 3:37 
GeneralMy vote of 5 Pin
Michel [mjbohn]24-Nov-11 0:34
Michel [mjbohn]24-Nov-11 0:34 
GeneralRe: My vote of 5 Pin
Mehdi Gholam10-Feb-12 2:12
Mehdi Gholam10-Feb-12 2:12 
QuestionData validation Pin
Loic Berthollet31-Jan-11 21:52
Loic Berthollet31-Jan-11 21:52 
AnswerRe: Data validation Pin
Mehdi Gholam2-Feb-11 9:54
Mehdi Gholam2-Feb-11 9:54 
GeneralMy vote: 3 Pin
Fabio Malpezzi31-Jan-11 11:08
professionalFabio Malpezzi31-Jan-11 11:08 
GeneralRe: My vote: 3 Pin
Pete O'Hanlon31-Jan-11 11:59
mvaPete O'Hanlon31-Jan-11 11:59 
GeneralRe: My vote: 3 Pin
Mehdi Gholam2-Feb-11 9:56
Mehdi Gholam2-Feb-11 9:56 
GeneralRe: My vote: 3 Pin
pophelix3-Apr-12 18:57
pophelix3-Apr-12 18:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.