Up until now, the Select column in the grid hasn't done anything useful. You might want to allow users to select a row and display information about the selected row. For example, you might want to display the category name associated with the selected item in the grid. Displaying this information requires a little bit of code, as you might imagine. Actually, selecting an item in the DataGrid control takes no code at all simply clicking the Select link highlights the row for you, based on the formatting you applied to the grid. Retrieving the information, however, requires that you react to the ItemCommand event of the grid. Clicking any link on the DataGrid control filters through the same event procedure: grdCatSales_ItemCommand, in this case. How can you differentiate between the various items? When you created the column, you set the CommandName property of the column to be Select, and you can check the CommandName property of the second parameter sent to the event procedure if it's not Select, you simply don't run any code. In order to display the category name in the label on the page, follow these steps: -
With CategorySales.aspx open in the page designer, select the View, Code menu item to view the page's code-behind file. -
At the top of the code editor window, from the drop-down list on the left, select grdCatSales. -
From the drop-down list on the right, select ItemCommand. This inserts the appropriate event-handling procedure into the class for you. -
Modify grdCatSales_ItemCommand so that it looks like this: Private Sub grdCatSales_ItemCommand( _ ByVal source As Object, _ ByVal e As System.Web.UI.WebControls. _ DataGridCommandEventArgs) _ Handles grdCatSales.ItemCommand Dim ds As DataSet Dim dr As DataRow Select Case e.CommandName Case "Select" ' Get stored DataSet ds = CType(Session("DS"), DataSet) ' Get the Row from the DataSet dr = ds.Tables(0).Rows(e.Item.DataSetIndex) ' Display the Category Name column lblCategory.Text = "Category: " & _ dr("CategoryName").ToString Case Else ' No others, right now. End Select End Sub -
Build and browse the page, select a row, and verify that you see the category name displayed in the label on the page. The procedure does its work by taking these actions: -
It checks the CommandName value. The code looks at the CommandName property of the second parameter (e) passed to the procedure. If the value is Select, the procedure can react to the user clicking the Select column on the page: Select Case e.CommandName Case "Select" ... End Select -
It retrieves the DataSet from the Session state and converts the Object to a DataSet: ' Get stored DataSet ds = CType(Session("DS"), DataSet) -
It retrieves the row from the DataSet, in the first DataTable, corresponding to the DataSetIndex property of the currently selected item (DataSetIndex returns the row number of the selected row): ' Get the Row from the DataSet dr = ds.Tables(0).Rows(e.Item.DataSetIndex) -
It displays the CategoryName column value in the label: ' Display the Category Name column lblCategory.Text = "Category: " & _ dr("CategoryName").ToString NOTE Because you'll trigger the ItemCommand event if you select any link within the DataGrid control, you must check the CommandName property of the second parameter (e.CommandName) before running your code. This example used a Select Case statement, so it's easy to add other cases. (For example, if you click any of the pager links, the CommandName property would be Page.) |