GridView Control in ASP.NET: Part 4

In this article we will discuss how to use CheckBoxField in GridView Control.

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

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 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 valueof a data item as an image.
  • TemplateField Enables us to customize the appearance of a data item.

Using CheckBoxFields

A CheckBoxField, as we can probably guess, displays a check box. When a row is not in edit mode, the check box is displayed but it is disabled.

Grid.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 id="Head1" 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="709px">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <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="MOBILE" HeaderText="MOBILE" 
                    SortExpression="MOBILE" />
                <asp:CheckBoxField DataField="MALE_FEMALE" HeaderText="MALE_FEMALE" 
                    SortExpression="MALE_FEMALE" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>" 
            SelectCommand="SELECT [ID], [NAME], [ADDRESS], [MOBILE], [MALE_FEMALE] FROM [MYTB]" 
            UpdateCommand="UPDATE MYTB SET ID =@ID, NAME =@NAME, ADDRESS =@ADDRESS, MOBILE =@MOBILE, MALE_FEMALE =@MALE_FEMALE WHERE ID=@ID">
        </asp:SqlDataSource>
     </div>
    </div>
    </form>
</body>
</
html>

Note: Continue in Next Part.

HAVE A GREAT CODING!