VB.NET•Support DataGrid's Events

Listing 5. The code-behind file for the Authors example has all the event handlers to manage the DataGrids' inline editing, paging, and sorting events.

Protected Sub DataGrid1_ItemDataBound _
   (ByVal sender As System.Object, _
   ByVal e As DataGridItemEventArgs) _
   Handles pubsDataGrid.ItemDataBound
   Dim strSortField As String
   Dim boolSortAscending As Boolean
   Dim lblSortArrow As Label
   Dim Cell As TableCell
   Dim strOldString As String

   ' If it is a header row item, 
   ' then make sure we put the right sort characters
   Select Case e.Item.ItemType
      Case ListItemType.Item
      Case ListItemType.AlternatingItem
      Case ListItemType.Footer
      Case ListItemType.Pager

      Case ListItemType.Header
         ' Add a little up/down arrow next 
         ' to the sort field to indicate which
         ' way the data is being sorted
         strSortField = SortField
         boolSortAscending = SortAscending
         lblSortArrow = New Label
         lblSortArrow.Font.Name = "Webdings"
         If SortAscending Then
            lblSortArrow.Text = "6"
         Else
            lblSortArrow.Text = "5"
         End If
         Select Case strSortField
            Case "au_lname"
               Cell = e.Item.Cells(0)
            Case ""
            ' Use the default sort (au_lname)
               Cell = e.Item.Cells(0)
            Case "au_fname"
               Cell = e.Item.Cells(1)
            Case "city"
               Cell = e.Item.Cells(3)
            Case "state"
               Cell = e.Item.Cells(4)
            Case "zip"
               Cell = e.Item.Cells(5)
         End Select

         Try
            Cell.Controls.Add(lblSortArrow)
         Catch
            ' Put error handling here
         End Try

      Case Else

   End Select
End Sub

Private Sub pubsDataGrid_EditCommand _
   (ByVal sender As System.Object, _
   ByVal e As DataGridCommandEventArgs) _
   Handles pubsDataGrid.EditCommand
   ' Mark the selected item ready for edits
   pubsDataGrid.EditItemIndex = e.Item.ItemIndex
   BindDataGrid()
End Sub

Private Sub pubsDataGrid_CancelCommand _
   (ByVal sender As System.Object, _
   ByVal e As DataGridCommandEventArgs) _
   Handles pubsDataGrid.CancelCommand
   ' Cancel the edits
   pubsDataGrid.EditItemIndex = -1
   BindDataGrid()
End Sub

Private Sub pubsDataGrid_DeleteCommand _
   (ByVal sender As System.Object, _
   ByVal e As DataGridCommandEventArgs) _
   Handles pubsDataGrid.DeleteCommand
   ' Put your delete code here

   ' Reset the selected index
   pubsDataGrid.EditItemIndex = -1
   BindDataGrid()
End Sub

Private Sub pubsDataGrid_UpdateCommand _
   (ByVal sender As System.Object, _
   ByVal e As DataGridCommandEventArgs) _
   Handles pubsDataGrid.UpdateCommand
   ' Put your update code here

   ' Reset the selected index
   pubsDataGrid.EditItemIndex = -1
   BindDataGrid()
End Sub

Private Sub pubsDataGrid_Sort _
   (ByVal sender As System.Object, _
   ByVal e As DataGridSortCommandEventArgs) _
   Handles pubsDataGrid.SortCommand
   ' Save the old sort field
   Dim strCurrentSortField As String = SortField

   ' Save the new sort expression 
   SortField = e.SortExpression.ToString()
   If (strCurrentSortField.Equals(SortField)) Then
      SortAscending = Not SortAscending
   Else
      SortAscending = True
   End If

   ' Change to the first page for the new sort
   pubsDataGrid.CurrentPageIndex = 0
   BindDataGrid()

End Sub

Private Sub pubsDataGrid_Page _
   (ByVal sender As System.Object, _
   ByVal e As DataGridPageChangedEventArgs) _
   Handles pubsDataGrid.PageIndexChanged
   ' Change the current data grid page
   pubsDataGrid.CurrentPageIndex = e.NewPageIndex

   ' Reset the selected index
   pubsDataGrid.EditItemIndex = -1
   BindDataGrid()
End Sub

Private Sub BindDataGrid()
   Dim dt As DataTable
   Dim dv As DataView
   Dim strSort As String

   ' Set a pointer to the data table
   SqlDataAdapter.Fill(AuthorsDataSet)
   dt = AuthorsDataSet.Tables("Authors")

   ' Create a new view space
   dv = New DataView(dt)

   ' Set the sort to what the user selected
   strSort = SortField
   If (SortAscending = False) Then
      strSort += " desc"
   Else
      strSort += " asc"
   End If

   dv.Sort = strSort
   pubsDataGrid.DataSource = dv
   pubsDataGrid.DataBind()
End Sub