Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Fixing an annoying GridView problem

439 views

Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Fixing an annoying GridView problem

  1. 1. Solving a frustrating GridView shortcoming By Roger Pence How to solve a frustrating GridView problem… while learning a cool OO technique: decorator classes.
  2. 2. The problem <ul><li>The .NET 1.1 DataGrid let you </li></ul><ul><ul><li>specify CommandNames in ButtonColumns (to determine what column got clicked) </li></ul></ul><ul><ul><li>The ItemCommand event fired when you clicked on a button and the e argument provided the row selected </li></ul></ul><ul><ul><li>You could therefore easily have multiple actions (one in each column) and in the ItemCommand event determine what row and what button was selected. </li></ul></ul>
  3. 3. The problem <ul><li>The GridView lets you either </li></ul><ul><ul><li>Add as many ButtonColumns as desired </li></ul></ul><ul><ul><li>Use the SelectedItemChanged when a ButtonColumn get clickes to determine what row was seleced (through the event’s e argument) </li></ul></ul><ul><ul><li>However, the only command name that gets you into the SelectedItemChanged event is SELECT. Thus, while you can have many ButtonColumns, you can’t determine which one was clicked because the CommandName must be SELECT to get into the SelectedItemChanged event </li></ul></ul>
  4. 4. Or… <ul><li>Add several different ButtonColumns, each with a different command name </li></ul><ul><li>Use the RowCommand event to determine which column got clicked </li></ul><ul><li>But, sigh, the RowCommand can’t tell you what row was selected </li></ul><ul><ul><li>The GridView’s SelectedIndex property isn’t set until after the RowCommand event fires </li></ul></ul>
  5. 5. Fixing the problem… the first pass BegSr gvCust_RowCreated Access(*Private) + Event(*This.gvCust.RowCreated) DclSrParm sender Type(*Object) DclSrParm e Type(GridViewRowEventArgs) DclFld lb Type( LinkButton ) DclFld RowCounter Type( *Integer4 ) Static( *Yes ) If ( e.Row.RowType = DataControlRowType.DataRow ) ForEach tc Type( TableCell ) Collection( e.Row.Cells ) If ( tc.Controls.Count > 0 ) lb = tc.Controls[ 0 ] *As LinkButton If ( lb <> *Nothing ) lb.CommandArgument = RowCounter EndIf EndIf EndFor RowCounter += 1 EndIf EndSr In the RowCreated event, set each LinkButton’s CommandArgument property to the ordinal row number
  6. 6. Fixing the problem… the first pass BegSr gvCust_RowCommand Access(*Private) + Event(*This.gvCust.RowCommand) DclSrParm sender Type(*Object) DclSrParm e Type( GridViewCommandEventArgs ) // Column clicked available in e.CommandName. lblColumnSelected.Text = e.CommandName // Row clicked available in e.CommandArgument. lblRowSelected.Text = e.CommandArgument EndSr This makes the CommandButton’s CommandName available in e’s CommandName property and the row clicked in e’s CommandArgument property.
  7. 7. Fixing the problem with a decorator class <ul><li>“ Decorator” classes provide a way to extend an existing class without subclassing that class </li></ul><ul><li>While subclassing is often quite a good thing, the decorator technique is often good for customer problems because the customer only needs to be given a secondary class, not a wholesale replacement for a given component </li></ul>
  8. 8. Decorator classes <ul><li>Instanced by passing a reference to the class needing decorating (or extended) </li></ul><ul><li>This makes all members (including events) of that “parent” class intrinsically available to the decorator </li></ul><ul><li>You only need declare the decorator… no other code is necessary in the parent class </li></ul><ul><li>The parent class, for all intents and purposes, thinks it has been subclassed </li></ul>
  9. 9. Put patterns on your to-do list! <ul><li>Borrow Head First Design Patterns from me </li></ul><ul><li>Borrow Design Patterns from Walter or Sharad </li></ul>

×