When the AutoGenerateEditButton property is set to true, a column (represented by a CommandField object) with an Edit button for each data row is automatically added to the GridView control. Clicking an Edit button for a row puts that row in edit mode. When a row is in edit mode, each column field in the row that is not read-only displays the appropriate input control, such as a TextBox control, for the field's data type. This allows the user to modify the field's value.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="SalesOrderID,SalesDetailID" DataSourceID="DataSource1">
Use the DataFormatString property to specify a custom display format for the values that are displayed in the BoundField object. If the DataFormatString property is not set, the field's value is displayed without any special formatting.
<columns>
<asp:boundfield datafield="CustomerID" readonly="true" headertext="CustID"/>
<asp:boundfield datafield="CompanyName" convertemptystringtonull="true"
headertext="Customer Name"/>
<asp:BoundField DataField="ListPrice" HeaderText="ListPrice"
SortExpression="ListPrice" DataFormatString="{0:C}" />
</columns>
TemplateField: Displays user-defined content for each item in the GridView control according to a specified template. This column field type enables you to create a custom column field.
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img src='<%# Eval("pic_square") %>' alt="" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnbSend" runat="server" CommandArgument='<%# Eval("uid") %>' OnCommand="lnbSend_Command">Send Message</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Why use TemplateField instead of BoundField?
The GridView is composed of a set of fields that indicate what properties from the DataSource are to be included in the rendered output along with how the data will be displayed. The simplest field type is the BoundField, which displays a data value as text. Other field types display the data using alternate HTML elements. The CheckBoxField, for example, renders as a check box whose checked state depends on the value of a specified data field; the ImageField renders an image whose image source is based upon a specified data field. Hyperlinks and buttons whose state depends on an underlying data-field value can be rendered using the HyperLinkField and ButtonField field types, respectively.
While the CheckBoxField, ImageField, HyperLinkField, and ButtonField field types allow for an alternate view of the data, they still are fairly limited with respect to formatting. A CheckBoxField can only display a single check box, whereas an ImageField can display only a single image. What if a particular field must display some text, a check box, and an image, all based upon different data-field values? Or what if we wanted to display the data using a Web control other than the CheckBox, Image, HyperLink, or Button? Furthermore, the BoundField limits its display to a single data field. What if we wanted to show two or more data-field values in a single GridView column?
To accommodate this level of flexibility, the GridView offers the TemplateField, which renders using a template. A template can include a mix of static HTML, Web controls, and data-binding syntax. 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.
To summarize here are the applications of TemplateFields”
1. Combining two or more data-field values into one column
2. Expressing a data-field value using a Web control instead of text. Example: Displaying hire date using calendar control by setting the VisibleDate and SelectedDate properties to hiredate data field.
3. Used in displaying metadata about the GridView's underlying data. In addition to showing the employees' hire dates, for example, we might also want to have a column that displays how many total days they've been on the job.
4. Another use of TemplateFields arises in scenarios in which the underlying data must be displayed differently in the Web page report from the format in which it's stored in the database. 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".
1 comment:
Excelente explicación. Muchas gracias amigo!
Post a Comment