Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Invoking methods from template columns in the GridView

Invoking methods from template columns in the GridView.

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 example, the filling in of certain columns with data obtained through the return of a call to a class method, use of components as CheckBoxes, TextBoxes, calendars, images, calculated fields, data concatenation, link formatting with special values or associated to JavaScript functions, texts’ formatting starting from values associated to certain attributes depending on a x or y situation, presentation of grouped values, amongst others.

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.

 

image001.jpg

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.

 

image003.jpg

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.

 

image004.jpg

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

 

image005.jpg

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;

    switch (StatusCode)

    {

        case 0:

            vStatus = "New";

            break;

        case 1:

            vStatus = "In Progress";

            break;

        case 2:

            vStatus = "Waiting";

            break;

        case 3:

            vStatus =

                "Finished";

            break;

        }

    return vStatus;

}

 

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.

 

image006.jpg

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.

 

image007.jpg

Figure 6. Report result after invoking the method to the Status column

 

Conclusion

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!

 



colunista nao disponivel

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login