Datagrid edit template in asp.net




















To accommodate this level of flexibility the GridView offers the TemplateField, which renders using a template. Furthermore, the TemplateField has a variety of templates that can be used to customize the rendering for different situations. For example, the ItemTemplate is used by default to render the cell for each row, but the EditItemTemplate template can be used to customize the interface when editing data.

In the preceding tutorial we saw how to customize the formatting based on the underlying data using the DataBound and RowDataBound event handlers. Another way to customize the formatting based on the underlying data is by calling formatting methods from within a template.

We'll look at this technique in this tutorial as well. For this tutorial we will use TemplateFields to customize the appearance of a list of employees. Specifically, we'll list all of the employees, but will display the employee's first and last names in one column, their hire date in a Calendar control, and a status column that indicates how many days they've been employed at the company.

In reporting scenarios where you need to use TemplateFields to customize the appearance, I find it easiest to start by creating a GridView control that contains just BoundFields first and then to add new TemplateFields or convert the existing BoundFields to TemplateFields as needed. Therefore, let's start this tutorial by adding a GridView to the page through the Designer and binding it to an ObjectDataSource that returns the list of employees.

These steps will create a GridView with BoundFields for each of the employee fields. Open the GridViewTemplateField. To remove these BoundFields you can:. Take a moment to view our progress in a browser. At this point you should see a table with a record for each employee and four columns: one for the employee's last name, one for their first name, one for their title, and one for their hire date.

Currently, each employee's first and last names are displayed in a separate column. It might be nice to combine them into a single column instead. To accomplish this we need to use a TemplateField. Both approaches net the same result, but personally I like converting BoundFields to TemplateFields when possible because the conversion automatically adds an ItemTemplate and EditItemTemplate with Web controls and databinding syntax to mimic the appearance and functionality of the BoundField.

The benefit is that we'll need to do less work with the TemplateField as the conversion process will have performed some of the work for us. Select the BoundField to convert from the list in the lower left corner and then click the "Convert this field into a TemplateField" link in the bottom right corner. After this change there's no perceptive difference in the Designer. This can be accomplished either by hand or through the Designer. To do it by hand, simply add the appropriate declarative syntax to the ItemTemplate :.

This will display the GridView's template editing interface. In this interface's smart tag is a list of the templates in the GridView. Since we only have one TemplateField at this point, the only templates listed in the drop-down list are those templates for the FirstName TemplateField along with the EmptyDataTemplate and PagerTemplate.

The EmptyDataTemplate template, if specified, is used to render the GridView's output if there are no results in the data bound to the GridView; the PagerTemplate , if specified, is used to render the paging interface for a GridView that supports paging. We need to change this so that this property is bound to the value of the LastName data field instead. It also has the advantage of hiding the value from prying eyes. NET to use the Edit Template when the data for the row is rebound along with the Cancel and Update links in the edit command column.

It simply sets the EditItemIndex to -1 to display the DataGrid in normal mode when the data is rebound. It extracts the edited data, updates the data in the database, and resets the DataGrid to normal mode when the data is rebound see comments in the code for more details.

Skip to main content. NET Cookbook by. Start your free trial. Editing Data Within a DataGrid. Example DataGrid with editing. Eval Container. Item Cint DataBinder. DataGrid with editing code-behind. Collections Imports System. Configuration Imports System. Database Manipulation Using DataGrid. Creating Columns in a DataGrid Control. Adding Images Using Image Control.

Using MD5 Hash Algorithm. NET Practice Exercise. Your email address will not be published. Save my name, email, and website in this browser for the next time I comment. Eval Container,"DataItem. Parse ty. Parse tz. Web Forms Data Controls. Sign in to vote. User posted error message: specified argument was out of range of valid values. Parameter name: Values i get this error message when i click on the edit button of the datagrid.

Monday, September 19, PM. User posted Hi trinity, What line of code is this error happening on? The code before and after that line would be helpful for us to see also.

Thanks, Marcie. User posted I have 2 dropdownlist in my datagrid. But when i include the 2nd dropdownlist and click on the edit button it gives me the following error during the databind event. Specified argument was out of the range of valid values.



0コメント

  • 1000 / 1000