ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

Changing Cell Contents with a Single Keystroke

The BeforeColEdit event is an extremely versatile way to customize the behavior of True DBGrid editing. BeforeColEdit is fired before any other editing events occur, which provides the opportunity to do virtually anything desired before editing begins. For example, cancel the edit request and override the built-in text editor with your own drop-down list box.

A True DBGrid control can enter edit mode in one of four ways:

1.   If the user clicks on the current cell with the mouse, editing begins with the current cell contents.

2.   If the user presses the F2 key, editing also begins using the current cell contents.

3.   If the user begins typing, the typed character replaces the contents of the cell and editing begins.

4.   You can set the EditActive property in your code to force editing to begin.

The BeforeColEdit event fires in cases 1, 2, and 3, but not in case 4, since True DBGrid assumes you will never want to cancel a request made from code.

To differentiate a user's edit request based upon whether he or she used the mouse or the keyboard to start editing, set BeforeColEdit to KeyChar, which will be zero if the user clicked on the cell with the mouse, and will be an ASCII character if the user typed a character to begin editing.

When BeforeColEdit is fired, the ASCII character has not yet been placed into the current cell, so if editing in BeforeColEdit is cancelled, the ASCII key is discarded. This leads to an interesting technique.

Assume a Boolean field called Done exists, and its NumberFormat property is set to specify Yes/No as the display format. Further assume that, when the user presses Y or N, the cell contents change immediately instead of entering edit mode. This process is accomplished in BeforeColEdit as follows:

·      Visual Basic

Private Sub C1TrueDBGrid1_BeforeColEdit(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.BeforeColEditEventArgs) Handles C1TrueDBGrid1.BeforeColEdit

    With Me.C1TrueDBGrid1.Columns(e.ColIndex)

 

        ' If this isn't the "Done" column, or if the user clicked with the mouse, then simply continue.

        If .DataField <> "Done" Or e.KeyChar = Chr(0) Then Exit Sub

 

        ' Cancel normal editing and set the field to the proper result based upon KeyChar. Beep if an invalid character was typed.

        e.Cancel = True

        Select Case UCase(e.KeyChar)

            Case "Y"

                .Value = -1

            Case "N"

                .Value = 0

            Case Else

                Beep()

        End Select

    End With

End Sub

·      C#

private void C1TrueDBGrid1_BeforeColEdit( object sender, C1.Win.C1TrueDBGrid.BeforeColEditEventArgs e)

{

    C1.Win.C1DataColumn col = e.Column.DataColumn;

 

    // If this isn't the "Done" column, or if the user clicked with the mouse, then simply continue.

    if (col.DataField != "Done" || e.KeyChar == 0 ) return;

 

    // Cancel normal editing and set the field to the proper result based upon KeyChar. Beep if an invalid character was typed.

    e.Cancel = true;

    switch (e.KeyChar. .ToUpper())

    {

        case "Y";

            Col.Value = -1;

            break;

        case "N";

            Col.Value = 0;

        default:;

            Beep();

    }

}

·      Delphi

procedure TWinForm.C1TrueDBGrid1_BeforeColEdit(sender: System.Object; e: C1.Win.C1TrueDBGrid.BeforeColEditEventArgs);

begin 

  with Self.C1TrueDBGrid1.Columns[e.ColIndex] do

  begin

 

    // If this isn't the "Done" column, or if the user  clicked with the mouse, then simply continue.

    if (DataField <> 'Done') Or (e.KeyChar = Char(0)) Then

      Exit;

 

    // Cancel normal editing and set the field to the proper result based upon KeyChar. Beep if an invalid character was typed.

    e.Cancel := True;

    if (char.ToUpper(e.KeyChar) = ‘Y') then

      Value := -1;

    else if (char.ToUpper(e.KeyChar) = ‘N') then

      Value := 0;

  end;

end;

Note that the event handler terminates when KeyChar is zero, so mouse editing is still permitted.


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.