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

Data Entry Control

0.00/5 (No votes)
31 Jan 2011 2  
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 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