"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Adding a GridView Column of Checkboxes (VB)

by Scott Mitchell

Download Sample App or Download PDF

This tutorial looks at how to add a column of check boxes to a GridView control to provide the user with an intuitive way of selecting multiple rows of the GridView.

Introduction

In the preceding tutorial we examined how to add a column of radio buttons to the GridView for the purpose of selecting a particular record. A column of radio buttons is a suitable user interface when the user is limited to choosing at most one item from the grid. At times, however, we may want to allow the user to pick an arbitrary number of items from the grid. Web-based email clients, for example, typically display the list of messages with a column of checkboxes. The user can select an arbitrary number of messages and then perform some action, such as moving the emails to another folder or deleting them.

In this tutorial we will see how to add a column of checkboxes and how to determine what checkboxes were checked on postback. In particular, we’ll build an example that closely mimics the web-based email client user interface. Our example will include a paged GridView listing the products in the Products database table with a checkbox in each row (see Figure 1). A Delete Selected Products button, when clicked, will delete those products selected.

Each Product Row Includes a Checkbox

Figure 1: Each Product Row Includes a Checkbox (Click to view full-size image)

Step 1: Adding a Paged GridView that Lists Product Information

Before we worry about adding a column of checkboxes, let s first focus on listing the products in a GridView that supports paging. Start by opening the CheckBoxField.aspx page in the EnhancedGridView folder and drag a GridView from the Toolbox onto the Designer, setting its ID to Products. Next, choose to bind the GridView to a new ObjectDataSource named ProductsDataSource. Configure the ObjectDataSource to use the ProductsBLL class, calling the GetProducts() method to return the data. Since this GridView will be read-only, set the drop-down lists in the UPDATE, INSERT, and DELETE tabs to (None) .

Create a New ObjectDataSource Named ProductsDataSource

Figure 2: Create a New ObjectDataSource Named ProductsDataSource (Click to view full-size image)

Configure the ObjectDataSource to Retrieve Data Using the GetProducts() Method

Figure 3: Configure the ObjectDataSource to Retrieve Data Using the GetProducts() Method (Click to view full-size image)

Set the Drop-Down Lists in the UPDATE, INSERT, and DELETE Tabs to (None)

Figure 4: Set the Drop-Down Lists in the UPDATE, INSERT, and DELETE Tabs to (None) (Click to view full-size image)

After completing the Configure Data Source wizard, Visual Studio will automatically create BoundColumns and a CheckBoxColumn for the product-related data fields. Like we did in the previous tutorial, remove all but the ProductName, CategoryName, and UnitPrice BoundFields, and change the HeaderText properties to Product, Category, and Price. Configure the UnitPrice BoundField so that its value is formatted as a currency. Also configure the GridView to support paging by checking the Enable Paging checkbox from the smart tag.

Let s also add the user interface for deleting the selected products. Add a Button Web control beneath the GridView, setting its ID to DeleteSelectedProducts and its Text property to Delete Selected Products. Rather than actually deleting products from the database, for this example we’ll just display a message stating the products that would have been deleted. To accommodate this, add a Label Web control beneath the Button. Set its ID to DeleteResults, clear out its Text property, and set its Visible and EnableViewState properties to False.

After making these changes, the GridView, ObjectDataSource, Button, and Label s declarative markup should similar to the following:

[!code-aspxMain]

   1:  <p>
   2:      <asp:GridView ID="Products" runat="server" AutoGenerateColumns="False" 
   3:          DataKeyNames="ProductID" DataSourceID="ProductsDataSource" 
   4:          AllowPaging="True" EnableViewState="False">
   5:          <Columns>
   6:              <asp:BoundField DataField="ProductName" HeaderText="Product" 
   7:                  SortExpression="ProductName" />
   8:              <asp:BoundField DataField="CategoryName" HeaderText="Category" 
   9:                  ReadOnly="True" SortExpression="CategoryName" />
  10:              <asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" 
  11:                  HeaderText="Price" HtmlEncode="False" 
  12:                  SortExpression="UnitPrice" />
  13:          </Columns>
  14:      </asp:GridView>
  15:      <asp:ObjectDataSource ID="ProductsDataSource" runat="server" 
  16:          OldValuesParameterFormatString="original_{0}" 
  17:          SelectMethod="GetProducts" TypeName="ProductsBLL">            
  18:      </asp:ObjectDataSource>
  19:  </p>
  20:  <p>
  21:      <asp:Button ID="DeleteSelectedProducts" runat="server" 
  22:          Text="Delete Selected Products" />
  23:  </p>
  24:  <p>
  25:      <asp:Label ID="DeleteResults" runat="server" EnableViewState="False" 
  26:          Visible="False"></asp:Label>
  27:  </p>

Take a moment to view the page in a browser (see Figure 5). At this point you should see the name, category, and price of the first ten products.

The Name, Category, and Price of the First Ten Products are Listed

Figure 5: The Name, Category, and Price of the First Ten Products are Listed (Click to view full-size image)

Step 2: Adding a Column of Checkboxes

Since ASP.NET 2.0 includes a CheckBoxField, one might think that it could be used to add a column of checkboxes to a GridView. Unfortunately, that is not the case, as the CheckBoxField is designed to work with a Boolean data field. That is, in order to use the CheckBoxField we must specify the underlying data field whose value is consulted to determine whether the rendered checkbox is checked. We cannot use the CheckBoxField to just include a column of unchecked checkboxes.

Instead, we must add a TemplateField and add a CheckBox Web control to its ItemTemplate. Go ahead and add a TemplateField to the Products GridView and make it the first (far-left) field. From the GridView s smart tag, click on the Edit Templates link and then drag a CheckBox Web control from the Toolbox into the ItemTemplate. Set this CheckBox s ID property to ProductSelector.

Add a CheckBox Web Control Named ProductSelector to the TemplateField s ItemTemplate

Figure 6: Add a CheckBox Web Control Named ProductSelector to the TemplateField s ItemTemplate (Click to view full-size image)

With the TemplateField and CheckBox Web control added, each row now includes a checkbox. Figure 7 shows this page, when viewed through a browser, after the TemplateField and CheckBox have been added.

Each Product Row Now Includes a Checkbox

Figure 7: Each Product Row Now Includes a Checkbox (Click to view full-size image)

Step 3: Determining What Checkboxes Were Checked On Postback

At this point we have a column of checkboxes but no way to determine what checkboxes were checked on postback. When the Delete Selected Products button is clicked, though, we need to know what checkboxes were checked in order to delete those products.

The GridView s Rows property provides access to the data rows in the GridView. We can iterate through these rows, programmatically access the CheckBox control, and then consult its Checked property to determine whether the CheckBox has been selected.

Create an event handler for the DeleteSelectedProducts Button Web control s Click event and add the following code:

[!code-vbMain]

   1:  Protected Sub DeleteSelectedProducts_Click(sender As Object, e As EventArgs) _
   2:      Handles DeleteSelectedProducts.Click
   3:      
   4:      Dim atLeastOneRowDeleted As Boolean = False
   5:      ' Iterate through the Products.Rows property
   6:      For Each row As GridViewRow In Products.Rows
   7:          ' Access the CheckBox
   8:          Dim cb As CheckBox = row.FindControl("ProductSelector")
   9:          If cb IsNot Nothing AndAlso cb.Checked Then
  10:              ' Delete row! (Well, not really...)
  11:              atLeastOneRowDeleted = True
  12:              ' First, get the ProductID for the selected row
  13:              Dim productID As Integer = _
  14:                  Convert.ToInt32(Products.DataKeys(row.RowIndex).Value)
  15:              ' "Delete" the row
  16:              DeleteResults.Text &= String.Format( _
  17:                  "This would have deleted ProductID {0}<br />", productID)
  18:              '... To actually delete the product, use ...
  19:              ' Dim productAPI As New ProductsBLL
  20:              ' productAPI.DeleteProduct(productID)
  21:              '............................................
  22:          End If
  23:      Next
  24:      ' Show the Label if at least one row was deleted...
  25:      DeleteResults.Visible = atLeastOneRowDeleted
  26:  End Sub

The Rows property returns a collection of GridViewRow instances that makeup the GridView s data rows. The For Each loop here enumerates this collection. For each GridViewRow object, the row s CheckBox is programmatically accessed using row.FindControl("controlID"). If the CheckBox is checked, the row s corresponding ProductID value is retrieved from the DataKeys collection. In this exercise, we simply display an informative message in the DeleteResults Label, although in a working application we d instead make a call to the ProductsBLL class s DeleteProduct(productID) method.

With the addition of this event handler, clicking the Delete Selected Products button now displays the ProductID s of the selected products.

When the Delete Selected Products Button is Clicked the Selected Products ProductIDs are Listed

Figure 8: When the Delete Selected Products Button is Clicked the Selected Products ProductID s are Listed (Click to view full-size image)

Step 4: Adding Check All and Uncheck All Buttons

If a user wants to delete all products on the current page, they must check each of the ten checkboxes. We can help expedite this process by adding a Check All button that, when clicked, selects all of the checkboxes in the grid. An Uncheck All button would be equally helpful.

Add two Button Web controls to the page, placing them above the GridView. Set the first one s ID to CheckAll and its Text property to Check All ; set the second one s ID to UncheckAll and its Text property to Uncheck All .

[!code-aspxMain]

   1:  <asp:Button ID="CheckAll" runat="server" Text="Check All" />
   2:   
   3:  <asp:Button ID="UncheckAll" runat="server" Text="Uncheck All" />

Next, create a method in the code-behind class named ToggleCheckState(checkState) that, when invoked, enumerates the Products GridView s Rows collection and sets each CheckBox s Checked property to the value of the passed in checkState parameter.

[!code-vbMain]

   1:  Private Sub ToggleCheckState(ByVal checkState As Boolean)
   2:      ' Iterate through the Products.Rows property
   3:      For Each row As GridViewRow In Products.Rows
   4:          ' Access the CheckBox
   5:          Dim cb As CheckBox = row.FindControl("ProductSelector")
   6:          If cb IsNot Nothing Then
   7:              cb.Checked = checkState
   8:          End If
   9:      Next
  10:  End Sub

Next, create Click event handlers for the CheckAll and UncheckAll buttons. In CheckAll s event handler, simply call ToggleCheckState(True); in UncheckAll, call ToggleCheckState(False).

[!code-vbMain]

   1:  Protected Sub CheckAll_Click(sender As Object, e As EventArgs) _
   2:      Handles CheckAll.Click
   3:      
   4:      ToggleCheckState(True)
   5:  End Sub
   6:  Protected Sub UncheckAll_Click(sender As Object, e As EventArgs) _
   7:      Handles UncheckAll.Click
   8:      
   9:      ToggleCheckState(False)
  10:  End Sub

With this code, clicking the Check All button causes a postback and checks all of the checkboxes in the GridView. Likewise, clicking Uncheck All unselects all checkboxes. Figure 9 shows the screen after the Check All button has been checked.

Clicking the Check All Button Selects All Checkboxes

Figure 9: Clicking the Check All Button Selects All Checkboxes (Click to view full-size image)

[!NOTE] When displaying a column of checkboxes, one approach for selecting or unselecting all of the checkboxes is through a checkbox in the header row. Moreover, the current Check All / Uncheck All implementation requires a postback. The checkboxes can be checked or unchecked, however, entirely through client-side script, thereby providing a snappier user experience. To explore using a header row checkbox for Check All and Uncheck All in detail, along with a discussion on using client-side techniques, check out Checking All CheckBoxes in a GridView Using Client-Side Script and a Check All CheckBox.

Summary

In cases where you need to let users choose an arbitrary number of rows from a GridView before proceeding, adding a column of checkboxes is one option. As we saw in this tutorial, including a column of checkboxes in the GridView entails adding a TemplateField with a CheckBox Web control. By using a Web control (versus injecting markup directly into the template, as we did in the previous tutorial) ASP.NET automatically remembers what CheckBoxes were and were not checked across postback. We can also programmatically access the CheckBoxes in code to determine whether a given CheckBox is checked, or to chnage the checked state.

This tutorial and the last one looked at adding a row selector column to the GridView. In our next tutorial we’ll examine how, with a bit of work, we can add inserting capabilities to the GridView.

Happy Programming!

About the Author

Scott Mitchell, author of seven ASP/ASP.NET books and founder of 4GuysFromRolla.com, has been working with Microsoft Web technologies since 1998. Scott works as an independent consultant, trainer, and writer. His latest book is Sams Teach Yourself ASP.NET 2.0 in 24 Hours. He can be reached at mitchell@4GuysFromRolla.com. or via his blog, which can be found at http://ScottOnWriting.NET.

Previous Next



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-forms/overview/data-access/enhancing-the-gridview/adding-a-gridview-column-of-checkboxes-vb.htm
< THANKS ME>