Why are all rows selected in MSFlexGrid when I select the last row?

1.3k Views Asked by At

I have an MSFlexGrid control with 2 columns and all of my data is displayed on this grid. When I click on the Add button, I add a new row, and I select my new row.

The problem is that all the rows get selected and I need to select only my new row.

Here's my code:

Private Sub Add_Click()
    FGrid.Rows = FGrid.Rows + 1
    FGrid.RowSel = FGrid.Rows - 1
    FGrid.ColSel = 0

    If FGrid.Rows > 1 Then ' > 10
        FGrid.TopRow = FGrid.Rows - 1
    Else
        FGrid.TopRow = 1 
    End If

    FGrid.TextMatrix(FGrid.RowSel, 0) = Format(Date, "DD/MM/YYYY")
    FGrid.SetFocus
End Sub  
1

There are 1 best solutions below

0
41686d6564 On

Looks like you're looking for the Row property, instead of RowSel. You can use a combination of the two though depending on your requirements.

Row Property:

Returns/sets the active cell in a FlexGrid.

RowSel property:

Determines the starting or ending row or column for a range of cells.

When you set the RowSel value it's treated as the ending of the selection because the Row value is still 0 (unchanged). Therefore, you need to use something like this:

FGrid.Row = FGrid.Rows - 1
'FGrid.RowSel = FGrid.Rows - 1   ' Optional.

One more thing, instead of incrementing .Rows and then using TextMatrix() to assign a value to the cell, you can use AddItem to add a row with a value in its first cell. In this case, your code would look something like this:

FGrid.AddItem Format(Date, "dd/MM/yyyy")

FGrid.Row = FGrid.Rows - 1
'FGrid.RowSel = FGrid.Rows - 1   ' Optional.
FGrid.ColSel = 0

If FGrid.Rows > 10 Then
    FGrid.TopRow = FGrid.Rows - 1
End If

FGrid.SetFocus