The RiaGridView control gives you the opportunity to set the contents of the data cells by templates. Applying a template to a column allows you to display data from a linked table, instead of data from the source-table. Editing data in such columns is performed through a drop-down list that contains the records from the linked table. When you change an item from the list, the data in the data source, bound to the RiaGridView control, is changed accordingly.
To create such column, use a field of the TemplateField type. A field of this type, just like any other type of fields, may define headers and a field for sorting. And binding to linked table is done with the template.
To make a template in the RiaGridView, simply specify its EditItemTemplate, placing there the DropDownList component. The items from the drop-down list receive data text and data value from linked and main tables. As a result, a selected item from the list has data text for display and a corresponding data value for editing.
An example of creating a template
A data source, that provides data from the OderDetailDemo table, is bound to the RiaGridView control. Another data source represents the ProductsDemo table with the ProductID column as a primary key, and the ProductName column with the names of the products. In the OderDetailDemo table, there is a ProductID column which is a foreign key to the ProductsDemo table.
You need to display the ProductID column in the OderDetailDemo table, so that it displayed the names of the products instead of corresponding keys. When editing this column, there should appear a drop-down list with the names of the products. Selecting an element from the list causes changing the product key in the OderDetailDemo table in accordance to its name.
To do that, you should:
- In the RiaGridView Tasks menu, select Edit Columns.
- Creat a field of the TemplateField type, or, if there is a ProductID field in the Selected fields list, convert it to TemplateField. To do that, select the field and press Convert this field into a TemplateField.
- When defining the FooterText and HeaderText properties, you need to put a “#” and specify a field (for which you’re creating a pattern ProductID) from a data source, bound to RiaGridView control, after the header.
- Click OK to close the Fields dialogue window.
- From the RiaGridView Tasks menu, select Edit Templates.
- Select EditItemTemplate from the Display drop-down list.
- Right-click the default TextBox control in the template and select Delete to remove it.
- From the Standard tab of the Toolbox, drag a DropDownList control onto the template.
- In the DropDownList Tasks menu, select Choose Data Source.
- Select a data source that provides data from the ProductsDemo table.
- Select a data field (ProductName) to display in the DropDownList.
- Select a data field (ProductID) for the value of the DropDownList.
- Click OK.
- Select ItemTemplate in the Display drop-down list.
- Right-click the default Label control in the template and select Delete to remove it.
- In the DropDownList Tasks menu, select Edit DataBindings. The SelectedValue property of the DropDownList control is selected in the DataBindings dialog box.
- Click the Field Binding radio button and select a field for Bound To (ProductID).
- Uncheck Two-way databinding check box.
- Click OK.
- In the RiaGridView Tasks menu, click End Template Editing.
<asp:TemplateField
HeaderText="ProductName#ProductID" SortExpression = "ProductID">
<EditItemTemplat>
<asp:DropDownList ID="ddlProductName" runat="server"
DataSourceID="AccessDataSource1" DataTextField="ProductName"
DataValueField="ProductID" SelectedValue='<%# Eval("ProductID") %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
The table presents display and editing column values in the TemplateField and BoundField:
The field in the ProductName column is of the TemplateField type |
The field in the ProductID column is of the BoundField type |
 |
 |
 |
 |
A few recommendations for setting the RiaGridView control and the data source, when using a template:
The column with a TemplateField should not contain any empty records. This is why, upon making a new record, you should define the DefaultValue for the TemplateField in the InsertParameters list. In this example the InsertParameters list looks like this:
<InsertParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="OrderID" Type="Int32"/>
<asp:Parameter Name="ProductID" DefaultValue="1" Type="Int32"/>
<asp:Parameter Name="UnitPrice" Type="Single"/>
<asp:Parameter Name="Quantity" Type="Int16"/>
<asp:Parameter Name="Discount" Type="Single"/>
</InsertParameters>