GridView Control in ASP.NET: Part 6

Please you do read earlier parts of this article for complete understanding in GridView.

Part - 5

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 EnTRyDate column 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 HyperLinkField

Introduction & Demonstration

We use a HyperLinkField to create a link to another page. Assume an example; we have two database tables named MAIN and DETAILS. MAIN table consist only fields like ID, NAME, ADDRESS, CONTACT and DETAILS. DETAILS table consist fields like ID, MONEY and SALARY. Now we have two forms named MAIN.aspx and DETAILS.aspx. MAIN.aspx will display MAIN table's data and DETAILS.aspx will display DETAILS table's data. Now we want a HyperLink in each record in MAIN.aspx form and when use will click on that link it will redirect to DETAILS.aspx page and it will only consist the data based on the ID what you have clicked on MAIN.aspx page. If you run directly DETAILS.aspx page it will not show you any data because it has no parameter passed. Take a look on development. 

Here is MAIN.aspx Page:

Grid6.1.gif

<%@ 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
>

        <asp:GridView 
        ID="GridView1" 
        runat="server" 
        AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display." 
        Width="653px">
            <Columns>
                <asp:BoundField 
                DataField="ID" 
                HeaderText="ID" 
                SortExpression="ID" />
                <asp:BoundField 
                DataField="NAME" 
                HeaderText="NAME" 
                SortExpression="NAME" />
                <asp:BoundField 
                DataField="ADDRESS" 
                HeaderText="ADDRESS" 
                SortExpression="ADDRESS" />
                <asp:BoundField 
                DataField="CONTACT" 
                HeaderText="CONTACT" 
                SortExpression="CONTACT" />
                <asp:HyperLinkField
                HeaderText="DETAILS"
                DataTextField="DETAILS"
                DataNavigateUrlFields="ID"
                DataNavigateUrlFormatString="DETAILS.aspx?id={0}" />
        </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>" 
            SelectCommand="SELECT [ID], [NAME], [ADDRESS], [CONTACT], [DETAILS] FROM [MAIN]">
        </asp:SqlDataSource
>

      </div>
    </form
>
</body>
</
html>

Here is DETAILS.aspx Page:

Grid6.2.gif

<%@ 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
>

        <asp:GridView 
        ID="GridView1" 
        runat="server" 
        DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display." 
        Width="248px">
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>" 
            SelectCommand="SELECT [ID], [MONEY], [SALARY] FROM [DETAILS] WHERE ID=@ID">
            <SelectParameters>
            <asp:QueryStringParameter
            Name="ID"
            QueryStringField="ID" />
            </SelectParameters>
        </asp:SqlDataSource
>

      </div>
    </form
>
</body>
</
html>

Note: Continue in Next Part.

HAVE A GREAT CODING!


Similar Articles