Click here to Skip to main content
Click here to Skip to main content

Data Entry Control

, 31 Jan 2011
Rate this:
Please Sign up or sign in to vote.
Quick reflection based data entry control in one line of code
image001.png

Introduction

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.
image002.jpg

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; }        
[Browsable(false)]
public decimal number { get; set;}

Alternatives

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

Alternative 1: Visual Studio Designer

image003.jpg

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.

Problems

  • 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

image004.png

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();
}

Problems

  • 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.

Problems

  • 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.

image007.jpg

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

History

  • Initial release: January 23 2011

License

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

Share

About the Author

Mehdi Gholam
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 Platinums on CodeProject (13th Jan'12)

Comments and Discussions

 
GeneralFinally! Pinmemberlaughingandliving2-Mar-14 17:13 
GeneralMy vote of 5 PinmemberArmando Airo'11-Sep-13 2:37 
GeneralMy vote of 5 Pinmembermjbohn23-Nov-11 23:34 
GeneralRe: My vote of 5 PinmvpMehdi Gholam10-Feb-12 1:12 
QuestionData validation PinmemberLoic Berthollet31-Jan-11 20:52 
AnswerRe: Data validation PinmemberMehdi Gholam2-Feb-11 8:54 
GeneralMy vote: 3 PinmemberFabio Malpezzi31-Jan-11 10:08 
GeneralRe: My vote: 3 PinmvpPete O'Hanlon31-Jan-11 10:59 
GeneralRe: My vote: 3 PinmemberMehdi Gholam2-Feb-11 8:56 
GeneralRe: My vote: 3 Pinmemberpophelix3-Apr-12 17:57 
SB!鉴定完毕!

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 31 Jan 2011
Article Copyright 2011 by Mehdi Gholam
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid