Contents Exit focus mode. Grid View. Sort Expression Property Reference Is this page helpful? Please rate your experience Yes No.
Any additional feedback? Namespace: System. WebControls Assembly: System. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Gridview sorting with custom template fields Ask Question. Asked 12 years, 2 months ago. Active 9 years, 2 months ago. Viewed 28k times. I can't seem to figure out how to sort my gridview with both databound AND custom fields.
Eval Container. Can someone help me in the right direction please? Below is my full customgrid code. Data; using System. Configuration; using System. Linq; using System. Web; using System. Security; using System. UI; using System. Imagine that the Employees table had a Gender field that stored the character M or F to indicate the sex of the employee. When displaying this information in a web page we might want to show the gender as either "Male" or "Female", as opposed to just "M" or "F".
Both of these scenarios can be handled by creating a formatting method in the ASP. NET page's code-behind class or in a separate class library, implemented as a static method that is invoked from the template. Such a formatting method is invoked from the template using the same databinding syntax seen earlier. The formatting method can take in any number of parameters, but must return a string. This returned string is the HTML that is injected into the template.
To illustrate this concept, let's augment our tutorial to show a column that lists the total number of days an employee has been on the job. This formatting method will take in a Northwind. EmployeesRow object and return the number of days the employee has been employed as a string. This method can be added to the ASP. NET page's code-behind class, but must be marked as protected or public in order to be accessible from the template. To utilize this method we need to invoke it from a TemplateField in the GridView using the databinding syntax.
Its Row property returns the strongly-typed Northwind. This databinding syntax can appear directly in the ItemTemplate as shown in the declarative syntax below or can be assigned to the Text property of a Label Web control. However, the Eval method returns an object , so we'd have to change our DisplayDaysOnJob method signature to accept an input parameter of type object , instead.
Therefore, we'd need to accept an object as the input parameter for the DisplayDaysOnJob method, check to see if it had a database NULL value which can be accomplished using Convert. Due to these subtleties, I've opted to pass in the entire EmployeesRow instance. In the next tutorial we'll see a more fitting example for using the Eval "columnName" syntax for passing an input parameter into a formatting method.
The TemplateField in the GridView control allows for a higher degree of flexibility in displaying data than is available with the other field controls. TemplateFields are ideal for situations where:. In addition to customizing the display of data, TemplateFields are also used for customizing the user interfaces used for editing and inserting data, as we'll see in future tutorials.
The next two tutorials continue exploring templates, starting with a look at using TemplateFields in a DetailsView. Following that, we'll turn to the FormView, which uses templates in lieu of fields to provide greater flexibility in the layout and structure of the data. Scott works as an independent consultant, trainer, and writer. NET 2. He can be reached at mitchell 4GuysFromRolla.
This tutorial series was reviewed by many helpful reviewers. Lead reviewer for this tutorial was Dan Jagers. Interested in reviewing my upcoming MSDN articles? If so, drop me a line at mitchell 4GuysFromRolla. Previous Next. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Is this page helpful? Please rate your experience Yes No. Any additional feedback?
0コメント