Here you can drag and drop the column header to group data by the xamDataGrid field name or column name. The result will group by data on the ShipName column. Now, the records are grouped based on two columns. Column sorting can be easily accessed by a single click by mouse on the column header. You can even sort records on multiple columns by holding the control key Ctrl. Sorting is enabled on the xamDataGrid by default. To sort a column value, click on the column header and it will sort the values in ascending order and clicking again will sort the column values in descending order.
We can override the default behavior of column sorting. In this example, I am going to set the Direction value to Descending. You only have to write a few lines of code to add a new record or update and delete existing records in a database via the grid.
On this screen, we will add, update, and delete categories. The above code creates the following UI in Figure The data grid is also editable and when you click on a cell, the cell will become editable. The following code in Listing is the code behind event handlers where we can write code that will be executed when these events are fired.
You use code in the above event handlers when a new record is being added or updated in the UI. For example, if a new record is updated and you want to notify a customer, this will be the event where you will write the notification functionality.
The RecordUpdating and the RecordUpdated events are fired when a record is being updated and when a record has been updated in the xamDataGrid respectively. This is where you also want to save your changes to the backend database. The code snippet in Listing implements these two event handlers and the code for adding and updating backend is written there. The event handler code written in Listing will also save the record in the database.
Once a record is added successfully, you will see a popup message confirming the action. Now, if you double-click on a row and update the value of a cell or multiple cells, and hit ENTER, the xamDataGrid update events will be fired, the code executes, and the data is updated in the data grid as well as in the backend database. Similar to add and update events, there are two delete events that are fired when a record is being deleted in a xamDataGrid. To disable add, update, and delete operations in xamDataGrid, you can make it read-only by setting its AllowEdit property to false.
Often, developers have a need to export DataGrid records into an Excel sheet for reporting purposes. The xamDataGrid, with the support of other classes, can easily be exported into an Excel sheet.
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. To accomplish this click on the Label control's smart tag and choose the Edit DataBindings option.
This will bring up the DataBindings dialog box. From here you can select the property to participate in databinding from the list on the left and choose the field to bind the data to from the drop-down list on the right.
The DataBindings dialog box allows you to indicate whether to perform two-way databinding. Creates a duplicate copy of the current DataControlField -derived object. Copies the properties of the current TemplateField -derived object to the specified DataControlField object.
Creates a new TemplateField object. Extracts the value of the data control fields as specified by one or more two-way binding statements DataBind from the current table cell and adds the values to the specified IOrderedDictionary collection.
Gets the Type of the current instance. Creates a shallow copy of the current Object. Saves the changes made to the DataControlField view state since the time the page was posted back to the server. Returns a string that represents this DataControlField object. Causes the DataControlField object to track changes to its view state so they can be stored in the control's ViewState property and persisted across requests for the same page. Determines whether the controls contained in a TemplateField object support page callbacks.
Gets or sets the schema associated with this DataControlField object. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Template Field Class Reference Is this page helpful? Please rate your experience Yes No. Any additional feedback? Namespace: System. WebControls Assembly: System. Represents a field that displays custom content in a data-bound control.
Note Some data-bound controls such as the GridView control can show or hide only the entire header section of the control. In this article. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta.
0コメント