Invoking methods from template columns in the GridView
The GridView is a fantastic control for exhibition of tabled data and during most of the time offer ready implementations that satisfy the programmers’ necessities, however, there are situations where we have to demand more than what GridView offers us.
For the exposed cases and for others special cases, the GridView does not provide built-in support, we can rely on the use of the template columns which allow the programmers to implement their own functionalities for the columns of this kind.
Creating a simple report
In order for us to begin our example, I will define a small data model about an employee’s activities record system for an imaginary company. The purpose of our report (using GridView) is to present the activities performed by the employees and from there we will explore the invoking of methods starting from template columns.
The Figure 1 presents the definition of the data model, where:
· The Employee table stores the employees of the imaginary company;
· The Solicitant table stores the people who may solicit activities to be performed and;
· The Tasks table stores the solicited activities, time of start and finish, the employee who performed/is performing it and the person who solicited it.
Figure 1. Data model
In the tasks table the status attribute may take over four situations, which are 0, 1, 2, 3, where 0 represents a new activity, 1 represents an activity in execution, 2 represents a scheduled activity and 3 represents an activity that has been concluded.
Defined our system, we will now ready for the report implementation through the GridView. Having opened an ASP.NET project in Visual Studio 2005, rename the Web Form to “report.aspx”. The next step is to add a GridView and to define its DataSource in such a manner that it obtains as a result the activities’ description and their respective start and finish times, employee who performed/is performing the activity, status and name of solicitant.
In this first case, we will see that the activities’ status codes will not yet be processed by a template column and thus will be shown values between 0 and 3. Create a new DataSource throughout the Smart Tag of GridView, accessing the database of the example. Create a query, as Figure 2, using the Query Builder.
Figure 2. Using the Query Builder to define a query
Give the control a format and run the application. We can observe that in Figure 3 the Status column presents numbers to indicate the activity’s status.
Figure 3. GridView result after formatting
In essence, this is correct seen that in the tasks table we have these values to represent the status and have no reference table in our data model from which to obtain the descriptions of each one of them.
Defining the template column for the GridView
Given the problem, we can from now on convert the Status column to a template column and then make it so that it, instead of displaying the codes for the activity status, invokes a method called GetStatus defined in our class and sends it the code referring to each activity.
The method, returns a description for each sent code and the GridView displays a string corresponding to each one of them. In order for us to be able to convert the column into a template column, select the Edit Columns option in the Smart Tag of the GridView, check the Status column and click Convert this field into a TemplateField (Figure 4).
Figure 4. Converting the Status column into a Template
Add the GetStatus method to the class of the Web Form, according to Listing 1.
Listing 1. Definition of the GetStatus method
protected string GetStatus(int StatusCode)
string vStatus = string.Empty;
vStatus = "New";
vStatus = "In Progress";
vStatus = "Waiting";
Invoking the method o f the class
So that we can invoke the GetStatus method, we will have to work a little in the HTML definition of the report.aspx file. To access the file’s HTML definition, click the Visual Studio 2005 Source option.
Having accessed the report’s HTML definition, locate in the source-code the block marked by the tags ... and modify it like Figure 5.
Figure 5. Invoking the GetStatus method from the Template Column
Observing the Figure 5 we can notice that, that which brings the database’s status code and displays it in the GridView, is the code that is included inside the Text attribute of the Label1, which is nothing more than our Status column, therefore, what we have to do is to modify this piece of code so that it invokes the GetStatus method defined in our class.
Notice that the call to the method also takes place inside the code which is in the Text property of the Label1, in such case, for each line that the GridView generates it will invoke the method and with that we will achieve the desired result. Run the application and see the result in Figure 6.
Figure 6. Report result after invoking the method to the Status column
This article’s main intention was to suggest the use of template columns for situations where the GridView cannot accomplish all the work alone or in moments when the programmer wishes to place a personal touch or improve something that he is implementing.
It is worth highlighting that starting form the example presented, we can infer the idea and make use of the template columns for various other situations. Besides templates for the displaying of information, you can create templates for edition and the insertion of data, for the heading and the foot of the column, amongst others. Use your imagination, research and be creative!