We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 122,854 times

Contents

Downloads

Related Categories

Building a Full-Featured Custom DataGrid Control - Building Our Control

DMarko1

Building Our Control

All of our control's code resides in our C# class source file we named mycustDG.cs, that is listed at the end of this article. Here, as aforementioned, we will be defining our Datagrid's custom constructor, wherein we'll setup all our Datagrid properties, set up our events and bind our Datarid. In saving overburdening the reader from an exhaustive play by play of all the properties, I will explain all key concepts used and their context. Once you grasp the concepts all else will be quite easy to follow.

Recall back to our custom control server tag where you noticed some new custom properties. Let's veer into how we did this, and how we can create custom properties in our class and assign them values from our page.

Utilizing properties are similar to variables, but since properties have accessors, they specify the statements to execute in order to read or write their values. For example, in our Datagrid control tag I have a property pageSz. Of course, you can figure this out to be PageSize, which is indeed a standard datagrid base property. However, I simply created this custom name just so I can demonstrate how easy it is in doing so, and in how to assign properties to your control, from an external source. As in our initial layout example, we'll now add our properties to our public GetDataGrid() datagrid constructor method.

//We set our private internal variable
private int _pageSz;
//Our property that will retrieve the value from our external datagrid control tag and assign it to our datagrid method
public int pageSz
{
    get { return _pageSz; }
    set { _pageSz = value; }
}
//Our DataGrid Constructor
public void GetDataGrid()
{
    PageSize = pageSz; //pulled from our property above
}

Here, the Set accessor is assigning the value to our _pageSz variable, and the Get accessor is returning it. Moreover, our pageSz being assigned from our page's custom server control, sets the value to our datagrid property.

Since our control tag property pageSz = "5", that is what our datagrid, when rendered, will display. The rest as you will see in the source code simply includes any properties you want to hard-coded or customize as I have in like manner.

Also within our main GetDataGrid method, we call our two event handlers that are in charge of our paging and the event handler that will enable us the detailed data statistics we want, like the page number, how many records found, etc, as we'll soon see.

Finally, an added note regarding our GetDataGrid() method, is that it's ability to retrieve data rest solely on another method. This other method further is responsible for binding our grid with cached data as well, and I will explain all this as we press on. So with all said let's start off light by dealing with the paging.

To Page or Not to Page

Regarding our paging, as you'll notice has not much to it. Within our GetDataGrid() method we call the PageChanged method to handle our paging.

PageIndexChanged += new DataGridPageChangedEventHandler (PageChanged);

The actual PageChanged method is the commonly used .NET method. It assigns the Datagrid's CurrentPageIndex to correspond with the event argument's NewPageIndex property, and then rebinds our Datagrid. So far so good, I hope. Next we'll examine how we gather our Datagrid's start and end count for each page of data.

You do the math

We mentioned earlier that within our main GetDataGrid() method, we have two event handlers. One for paging, that we just finished discussing, and two, the method that will allow us to figure out the start and end count for each Datagrid page we're on.

ItemCreated += new DataGridItemEventHandler (GridCreated);

ItemCreated simply means that every time the DataGrid control is created, when paging or sorting, whenever, our event handler above get 's called, and in turn calls our method below and we then display our, i.e. "Result No. 16 to 20." statistics.

private void GridCreated(Object sender, DataGridItemEventArgs e)
{
    //Declare the ItemType
    ListItemType elemType = e.Item.ItemType;
    if (elemType == ListItemType.Item || elemType == ListItemType.AlternatingItem)
    {
        //A DataGrid Item represents an item (row) in the DataGrid control
        //e.Item is the table row where the command is raised
        DataGridItem dgRow = (DataGridItem) e.Item;
        //Gets the index number of the DataGridItem object from the bound data source
        int rowCntIncr = dgRow.DataSetIndex + 1;
        //Get the start and end index count from datagrid
        StartCnt = rowCntIncr-dgRow.ItemIndex;
        EndCnt = rowCntIncr;
    }
}

The ListItemType is what represents the different data-bound indexed items found in our Datagrid. What we essentially do is check for and references every row created and get a sequential count. The variables responsible for pulling this data are StartCnt and EndCnt. All this corresponds to all this data displayed to the user with our onPreRender method.

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 desktop applications. Till now Jimmy has authored nearly two dozen .NET articles, published on Dot Net Junkies, 4 Guys From Rolla, Sitepoint, MSDN Academic Alliance, Developers.NET, The Official Microsoft ASP.NET Site, and here on Developer Fusion, covering various unique and advanced techniques on .NET.

Comments

  • Re: [4676] Building a Full-Featured Custom DataGrid Control-Paging not working

    Posted by adwivedi1 on 09 Oct 2006

    Hi,

    I am building a ASP .NET composite control containing a datagrid during which I am 
    finding a problem while paging of the grid.

    The problem is for example if I am having three pa...

  • Building Custom Datagrid control

    Posted by hells_1999 on 02 Feb 2006

    I build my own custom datagrid control using the code from your site.

    I used this control as a reference in another aspx page at you have mentioned in your article.
    I have 400 total number of rec...

  • Posted by wrocca on 22 Dec 2005

    [quote][1]Posted by [b]taxiturner[/b] on 3 May 2005 10:25 PM[/1]
    LiteralControl lc = (LiteralControl) sender;
    DataGridItem container = (DataGridItem) lc.NamingContainer;
    lc.Text = ((DataRowView) co...

  • unable to update and sort on click

    Posted by myselfhemant on 22 Nov 2005

    I have created a Custom control using your article but i am having problem in sorting and update
    as i use a class which implements itemplate interface for this and create the function for update and...

  • selecting multiple items in a checkbox from a dat

    Posted by sneha123 on 31 Oct 2005

    There will be some 20 questions and for each question there will be 4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net
    pls help me

    we ha...