CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2002
    Posts
    16

    How to edit a grid cell during run time?

    Hi, I was trying to use MSFlexGrid or MSHflexGird to edit cell data during runtime. But I found that this can only be done in my coding. I can't click the cell and edit data in my application.

    Can anyone advise me how to do it? Or should I use some other VB components that have the cell like feature?

    Thanks in advance.

  2. #2
    Join Date
    Apr 2000
    Location
    Southampton, UK
    Posts
    329
    Well looks like you have found the two possible answers already. Either use an alternative grid, the DataGrid which ships with VB allows for in-cell editing but doesn't have the fancy formatting that is available with the Flexgrid. Alternatively you can purchase a grid which does, either the TrueDBFGrid (which is similar to DataGrid and DBGrid) or the VS Flexgrid (which is the one that was cut down to be the MSFlexgrid) both from Component One Or you can handle the coding to simulate the in-cell editing. Here is a simple tutorial to get started with this:

    Editing Grid Data
    MSFlexGrid does not have a built-in cell editing capability, but it provides the hooks to make it easy for you to add that capability programmatically. The advantage of this approach is that you can tailor editing behavior to your taste. The basic technique involves smoke and mirrors: the editing occurs not in MSFlexGrid at all, but in a standard Textbox control that is positioned precisely over the cell being edited.

    In this example, we will give the user two ways to get into the edit mode, either by double-clicking on a cell, or by simply starting to type in the current cell. The following two routines implement this:

    Private Sub MSFlexGrid1_DblClick()
    GridEdit Asc(" ")
    End Sub

    Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
    GridEdit KeyAscii
    End Sub

    In each case we call a grid edit subroutine and pass it a keystroke. In the case of double-clicking, we pass the space character as a flag. The GridEdit routine initializes the edit box and moves it into position:

    Sub GridEdit(KeyAscii As Integer)
    'use correct font
    Text1.FontName = MSFlexGrid1.FontName
    Text1.FontSize = MSFlexGrid1.FontSize
    Select Case KeyAscii
    Case 0 To Asc(" ")
    Text1 = MSFlexGrid1
    Text1.SelStart = 1000
    Case Else
    Text1 = Chr(KeyAscii)
    Text1.SelStart = 1
    End Select

    'position the edit box
    Text1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
    Text1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
    Text1.Width = MSFlexGrid1.CellWidth
    Text1.Height = MSFlexGrid1.CellHeight
    Text1.Visible = True
    Text1.SetFocus
    End Sub

    For demonstration purposes, the Case statement in the GridEdit routine shows two different behaviors when entering the edit mode. In practice you would probably only use one of them, or a different one of your own creation. If the edit mode is entered by virtue of a double-click or a control key press, we copy the contents of the grid cell to the exit box and place the cursor at the end of the string. If the edit mode is entered by pressing a normal key, we ignore the original cell contents and insert the pressed key into the edit box. The positioning of the exit box could be done on one line with the Move method. Here we have used four lines so that it reads more easily in this article. Notice that MSFlexGrid conveniently gives us all the coordinate information we need.

    Next, we need a couple of routines that handle housekeeping when the user moves to a different cell or moves focus back to the grid from another control. The LeaveCell event is also the place where you would put any data validation code that might be applicable.

    Private Sub MSFlexGrid1_LeaveCell()
    If Text1.Visible Then
    MSFlexGrid1 = Text1
    Text1.Visible = False
    End If
    End Sub

    Private Sub MSFlexGrid1_GotFocus()
    If Text1.Visible Then
    MSFlexGrid1 = Text1
    Text1.Visible = False
    End If
    End Sub

    Next we place some navigation code in the KeyDown event of the edit box so that, for instance, the user can leave the edit mode by pressing ESC, and move to a different row by pressing an arrow key:

    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyEscape
    Text1.Visible = False
    MSFlexGrid1.SetFocus
    Case vbKeyReturn
    MSFlexGrid1.SetFocus
    Case vbKeyDown
    MSFlexGrid1.SetFocus
    DoEvents
    If MSFlexGrid1.Row < MSFlexGrid1.Rows - 1 Then
    MSFlexGrid1.Row = MSFlexGrid1.Row + 1
    End If
    Case vbKeyUp
    MSFlexGrid1.SetFocus
    DoEvents
    If MSFlexGrid1.Row > MSFlexGrid1.FixedRows Then
    MSFlexGrid1.Row = MSFlexGrid1.Row - 1
    End If
    End Select
    End Sub

    Finally we need a line of code to suppress the Beep that occurs when ENTER is pressed in a Textbox:

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    'noise suppression
    If KeyAscii = vbKeyReturn Then KeyAscii = 0
    End Sub

    In order for the edit box to merge seamlessly into the grid, you need to set several Textbox properties at design-time: set Appearance = 0 (flat), and BorderStyle = 0 (none). Also set Visible = False so that the edit box is not initially visible. To really fine-tune this code, the edit box needs a slight additional offset to the southeast (with a corresponding reduction in size) so that the text in it lines up exactly with the text in the cell beneath. You would probably also want to write some code behind the scroll event of the grid since clicking on the grid's scroll bar will not cause the edit box to loose focus.

    Note that this technique is not limited to using a Textbox as your edit box. You could modify the sample code to use a ComboBox, a CheckBox, or even a calendar control for editing, based on the column being edited.

    MSFlexGrid is a very flexible control indeed, and this article just touches on some of the things you can do with it. As you gain familiarity with it, it will become a more regular part of your toolbox. Cell merging and pivoting are two more unique features of the MSFlexGrid that give it tremendous power and bear investigation.
    TimCottee
    I know a little about a lot of things and a lot about very little.

    Brainbench MVP For Visual Basic
    http://www.brainbench.com

    MCP, MCSD, MCDBA, CPIM

  3. #3
    Join Date
    May 2002
    Posts
    16

    Thumbs up

    Thanks. I tried out the code and it works! That's exactly what I want to do in my project.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured