Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to transfer a full row with data from mshflexgrid1 to mshflexgrid2 after clicking on a texbox keydown event.

MSHFLEXGRID1 has records and MSHFLEXGRID2 is empty.

Please help.

VB
With MSHFlexGrid1
            
For RecNo = 1 To .rows - 1
    .Row = RecNo
    '''''if record is exist in mshflexgrid1 then continue
    If Val(.TextMatrix(.Row, 1)) = Val(Text8.Text) Then
        .Row = RecNo
        .Col = 0
        .FillStyle = flexFillRepeat
        Set .CellPicture = LoadPicture(App.Path & "\Images\CHECKBLU.GIF")
        .CellBackColor = vbGreen
        .Col = 1
        '.ColSel = 0
        .TextMatrix(.Row, 0) = "1"
        Text8.Text = ""
        ''''''''''''''''''and want to remove using below code from mshflexgrid1
        '''MSHFlexGrid1.RemoveItem (MSHFlexGrid1.Row)

        Exit For
    End If
Next


What I have tried:

I tried this, but no luck.

VB
Dim A As Integer
Dim B As Integer
Dim J As Integer

With MSHFlexGrid2
    A = MSHFlexGrid2.rows - 1
    B = MSHFlexGrid1.RowSel

    For J = 1 To MSHFlexGrid1.Cols - 1
        If Val(MSHFlexGrid1.TextMatrix(J, 1)) = Val(Text8.Text) Then
            MSHFlexGrid2.TextMatrix(A, J) = MSHFlexGrid1.TextMatrix(J, J)
        End If
    Next J
    .AddItem ""
End With
Posted
Updated 8-Feb-22 10:37am
v2

1 solution

Your loop should work backwards through the grid. Instead of
VB
With MSHFlexGrid1
     For RecNo = 1 To .rows - 1
use
VB
With MSHFlexGrid1
     For .rows - 1 to 1 Step -1
You might also want to consider if you are processing all of the rows in the grid.

The following tries to explain why you need to go backwards...

Imagine you have these rows
Row Number   Content    Notes
  1          aaaaaa     Original Row 1
  2          bbbbbb     Original Row 2
  3          cccccc     Original Row 3
  4          dddddd     Original Row 4
If we delete the first row this is what happens (Hint - look at the Row Numbers)
Row Number   Content    Notes
  1          bbbbbb     Original Row 2
  2          cccccc     Original Row 3
  3          dddddd     Original Row 4
Delete (what is now) Row 2 now
Row Number   Content    Notes
  1          bbbbbb     Original Row 2
  2          dddddd     Original Row 4
So you thought you were going to delete "aaaaaa" and "bbbbbb" but instead deleted "aaaaaa" and "cccccc".

If you work backwards instead, still trying to delete "aaaaaa" and "bbbbbb" step 1 ends up like this
Row Number   Content    Notes
  1          aaaaaa     Original Row 1
  2          cccccc     Original Row 3
  3          dddddd     Original Row 4
I.e. Original Row 2 is deleted, the subsequent rows (that we are not interested in) move up in rank but Row 1 stays exactly where it was at the start of the loop.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900