Building a Full-Featured Custom DataGrid Control

Control Outline

As mentioned earlier, below we have our custom control tag responsible for our Datagrid:

<DMGrid:myCustDG ID="MyDataGrid" runat="server" autoCols="true" bgClr="Red" frClr="White" />

Now, by looking at the above Datagrid control tag you'll certainly notice some unrecognizable and uncommon properties that do not come from a standard out-of-the-box Datagrid. Well, that's the amazing thing. By virtue of building your own custom control, you can produce the same data results as shown in the image above from something as insignificant looking as the aforementioned control tag. This simple example should easily inspire you to create even other types of controls as well.

Before we dive right into creating out control, keep in mind that it is not mandatory to include any properties at all. All it could really contain is "<DMGrid:myCustDG ID="MyDataGrid" runat="server" />," and you could still get the exact same results as shown before, as long as you pre-assigned these in your code. I thought I'd demonstrate assigning properties both from without and within.

Now where do we begin? Ah yes, the class. When beginning to undertake the task of creating a custom control, you need to keep it mind that this is an ordinary object-oriented class. A class is like an upscale function or method that encapsulates any number of properties and functions, all within a unique name or namespace.

The control's C# code residing in our source file is a typical public class with a constructor (the main function called when the newly created object gets initialized). The full-fledged control (assembly) we'll create inherits all the common features of a Datagrid control from its properties and methods to its event handling.

Custom class creation in .NET, as in our article, implements this skeletal layout, or three typical steps:

1) Namespace setup and import (optional)
2) Variable declarations
3) Class Construction

// we define our aliases representing any namespaces we'll be using
namespace CustDataGrid
{
    // We import all the namespaces we'll require for our control
    using System;
    using System.Data;

    // Create our main class and our inherited control
    public class myCustDG : DataGrid
    { //Inherit all the features of a Datagrid base class
        // Declare any variables here
        private int _pageSz;
        //... Our main constructor and all required methods are placed here
    // Then close everything off
    } // End Class
} //End Namespace

In summary, there is not much to it as far as creating a class in .NET. Ours will contain all the functionality we need, and within this you will learn how to take away from it or add to it to your liking, depending on what you would like to accomplish in particular.

Having said that, within this class component we end up doing the following:

  • Set up the variables and properties that we'll be retrieving and assigning from our page, thus customizing our control
  • Access the database and return our cached data results, circumventing any initial Datagrid limitations that are right out of the box and were mentioned in my previously listed articles.
  • Implement our paging
  • Write out our data statistics to our page control

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.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie