Building a Full-Featured Custom DataGrid Control

Introduction

.NET, among its other amazing features, allows developers and programmers a rich platform with the capability to build custom controls deriving from already common base server controls, i.e. The Datagrid, the most commonly used and full-featured server control in .NET. One of the key components of .NET is OOP or Object-Oriented Programming, that focuses on creating reusable "black box" code enabling collective reuse throughout a website, significantly reducing development time when any changes have to be made affecting this one control. To gather a good grasp on this, take the time and read The Quick and Dirty .NET Guide to C#/VB Object-Oriented Programming.

In this article, we will examine and implement these very features and demonstrate how you can build your very own Datagrid control component, one that you will be able to customize, and more importantly reuse. Based on this ability, you will then end up with one powerful control that will have many implementations, from which you could learn about creating almost any other types of custom controls.

Now, some may question why would you attempt such a thing or why not simply use the common Datagrid? Well, good question! The quick and direct answer is reusability. Although requiring a lot more skill to create, the more all-inclusive answer would be custom controls promote lightly coded pages making for a lot less code to debug, offer greater flexibility, and more importantly, completely protected your code from anyone poking around.

Our example discussed here could easily be contained with the confines of a code-behind file, but this would usurp the latter aforementioned paradigms we have illustrated. With a custom control, you have a lot of power and control over what you render to the user or even restrict.

Having said that, this article will ultimately enable you to create a .NET page, incorporating a search form, with a pageable, and excitedly so a cacheable Datagrid custom control. Furthermore, learn various methods of interacting with your control, and learn how to assign and retrieve values from it as well!

Therefore, in this article we'll be learning how to:

  1. Create a .NET search page that will contain our custom control
  2. Learn how code-behind and compiling works
  3. Actually build our customizable Datagrid control class
  4. Demonstrate the ease of assigning and retrieving custom values to our control
  5. Enhance our control's behavior paging capabilities and the ability to displaying page and result numbers
  6. Demonstrate how to cache our Datagrid for added performance and scalability, all from within our control!
  7. Finally, show you how to enhance our Datagrid with four cool enhancements.

In turn, our result will be a page with no client side code or methods, only contain our code-behind reference and custom control DLL that we have created and compiled. After you finish this article, you will have amassed enough information in giving you the ability to create a custom server control class, and some great ideas to take what is here and expand even further. We are going to cover a lot of ground, but don't worry. In the end, you will end up having a number of solid techniques at your disposal for many various scenarios that may arise in the future.

Nevertheless, not to leave out any RAD VS.NET programmers, I have included this entire example as a C# VS.NET project solution, that includes the .aspx webform page containing our registered dll directive, and our C# code-behind that contains our custom datagrid methods that provide our UI page's interaction with our custom Datagrid control, and with the functionality we've set up when searching.

So with that said, let's get to it then.

You might also like...

Comments

About the author

Dimitrios Markatos

Dimitrios Markatos United States

Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and des...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“XML is like violence - if it's not working for you, you're not using enough of it.”