ComponentOne True DBGrid for WinForms
Cell Editing Techniques / Handling Editing Events / Changing Cell Contents with a Single Keystroke
In This Topic
    Changing Cell Contents with a Single Keystroke
    In This Topic

    You can use the BeforeColEdit event to customize the editing behavior of True DBGrid for WinForms controls. 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 for WinForms control enters edit mode in one of four ways:

    The BeforeColEdit event fires in the first three cases, but not in the last case, since True DBGrid for Winforms 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:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    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
    

    To write code in C#

    C#
    Copy Code
    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();
        }
    }
    

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