GridView Control in ASP.NET - Part 3

Using Fields with GridView Control


As to solve some of problems like enabling the GridView to render its columns automatically is that we give up any control over column formatting. For example, the BoxOfficeTotals column is displayed as a decimal amount without any currency formatting. The EnTRyDatecolumn always displays in short-date and long-time format. The solution to such problems is to specify explicitly the fields that a GridView displays. The GridView control supports the following types of fields:

·         BoundField Enables us to display the value of a data item as text.
·         CheckBoxField Enables us to display the value of a data item as a check box.
·         CommandField Enables us to display links for editing, deleting, and selecting rows.
·         ButtonField Enables us to display the value of a data item as a button (image button, link button, or push button).
·         HyperLinkField Enables us to display the value of a data item as a link.
·         ImageField Enables us to display the value of a data item as an image.
·         TemplateField Enables us to customize the appearance of a data item.

Using BoundField


BoundField always displays the value of a data item as text when a row is in normal display mode. When a row is selected for editing, aBoundField displays the value of a data item in a single line text field. The most important three properties of the BoundField class are theDataFieldDataFormatString, and HeaderText properties.


<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>
       
        <asp:GridView ID="GridView1"
        runat="server"
        AutoGenerateColumns="False"
        DataSourceID="SqlDataSource1"
        EmptyDataText="There are no data records to display."
        Width="495px">
            <Columns>
                <asp:BoundField
                DataField="ID"
                HeaderText="Serial Number"
                SortExpression="ID"/>
                <asp:BoundField
                DataField="NAME"
                HeaderText="Name of Candidate"
                SortExpression="NAME" />
                <asp:BoundField
                DataField="ADDRESS"
                HeaderText="Address of Candidate"
                SortExpression="ADDRESS" />
                <asp:BoundField
                DataField="MOBILE"
                HeaderText="MOBILE"
                SortExpression="MOBILE" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
            SelectCommand="SELECT [ID], [NAME], [ADDRESS], [MOBILE] FROM [MYTB]">
        </asp:SqlDataSource>
       
    </div>
    </div>
    </form>
</body>
</html>

Notice that the GridView control includes an AutoGenerateColumns property that is assigned the value False. If we don't disable automatically generated columns, then both columns represented by the BoundFields and all the columns from the data source are displayed redundantly. BoundFields also supports several useful properties as listed below.

·         AccessibleHeaderText Enables we to add an HTML abbr attribute to the column header.
·         ApplyFormatInEditMode Enables us to apply the DataFormatString to the field when the row is in edit display mode.
·         ConvertEmptyStringToNull Enables we to convert an empty string "" into the value Nothing (null) when editing a column.
·         DataField Enables we to specify the name of the field that the BoundField displays.
·         DataFormatString Enables we to use a format string to format a data item.
·         FooterStyle Enables we to format the column footer.
·         FooterText Enables we to display text in the column footer.
·         HeaderImageUrl Enables we to display an image in the column header.
·         HeaderStyle Enables we to format the column header.
·         HeaderText Enables we to display text in the column header.
·         HtmlEncode Enables we to HTML-encode the value of a data item, which enables you to avoid script injection attacks.
·         InsertVisible Enables we to not display a column when inserting a new record (does not apply to the GridView control).
·         ItemStyle Enables we to format a data item.
·         NullDisplayText Enables we to specify text that is displayed when a data item has the value Nothing (null).
·         ReadOnly Enables we to prevent the data item from being edited in edit mode.
·         ShowHeader Enables we to display the column header.
·         SortExpression Enables we to associate a sort expression with the column.
·         Visible Enables we to hide a column.

Note: Continue in Next Part. 

Comments

Popular posts from this blog

Customize User's Profile in ASP.NET Identity System

Migrating database from ASP.NET Identity to ASP.NET Core Identity