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