Multiple Selection in MSFlexgrid

Which property of MSFlexgrid allows the user to select multiple rows for processing?
scmmicroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimCotteeHead of Software ServicesCommented:
Hi scmmicro,

You can only select consecutive rows by setting the .Row and .RowSel properties. You cannot by default select non-consecutive rows unless you manage all of this yourself.

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
dbrckoviCommented:
Hi!

First set AllowBigSelection = True which will allow a selection by single click on a row or a column.
Set SelectionMode to 1,2 or 3 to allow selection of only rows, only columns, or both.

After that you can use properties row, col, rowsel and colsel to determine what is selected

Try this example:
Place Msflexgrid and 4 textboxes to a form and paste this code.
Run it and try selecting something.
----------------------------------------------------------------------------------
Private Sub Form_Load()
    MSFlexGrid1.Cols = 10
    MSFlexGrid1.Rows = 10
End Sub

Private Sub MSFlexGrid1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    Text1.Text = "Row: " & MSFlexGrid1.Row
    Text2.Text = "RowSel: " & MSFlexGrid1.RowSel
    Text3.Text = "Col: " & MSFlexGrid1.Col
    Text4.Text = "ColSel: " & MSFlexGrid1.ColSel
End Sub
--------------------------------------------------------------


0
dbrckoviCommented:
If you wish to select non-consecutive, then you could use an array which will hold an information for each row (if it is selected or not).

Try example from:    http://webwarper.net/ww/~GZ/www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20803581.html      
to see a possible approach.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

scmmicroAuthor Commented:
Can u send me the sample code for the same (using .row and .rowsel properties) ?
0
dbrckoviCommented:
Here is example which selects only rows.

Just add msflexgrid to form, execute and try selecting something.

----------------------------------------------------------------
Private Sub Form_Load()
    MSFlexGrid1.Cols = 10               'Set dimensions of flexgrid and set selection behaviour
    MSFlexGrid1.Rows = 10
   
    MSFlexGrid1.AllowBigSelection = True
    MSFlexGrid1.SelectionMode = flexSelectionByRow
       
    For x = 0 To 9                      'Used for filling a flexgrid with some data
        For y = 0 To 9
            MSFlexGrid1.Col = x
            MSFlexGrid1.Row = y
            MSFlexGrid1.Text = y
        Next y
    Next x

End Sub

Private Sub MSFlexGrid1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim StartRow As Integer
    Dim EndRow As Integer
   
    'Depending on the selection direction, Row can be either smaller or larger than RowSel
    'So we must determine which is smaller and set StartRow and EndRow variables accordingly
   
    If MSFlexGrid1.Row < MSFlexGrid1.RowSel Then
        StartRow = MSFlexGrid1.Row
        EndRow = MSFlexGrid1.RowSel
    Else
        StartRow = MSFlexGrid1.RowSel
        EndRow = MSFlexGrid1.Row
    End If
   
    message = "Selected rows:" & Chr(13)                'chr(13) is used to enter new line
    For x = StartRow To EndRow                          'Add some text to message
        message = message & x & Chr(13)
    Next x
   
    MsgBox message

End Sub
-----------------------------------------------------------------
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dancebertCommented:
Here is my library code for managing row selection.  It requires use of a fixed column in the grid.

Private m_IsOnFixedPart As Boolean
Private m_IsZeroRow As Boolean

Private Sub oGrid_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

    ' Are we on fixed column?  If so, highlight row(s)
   
    With oGrid
        If .Rows = 0 And .Cols = 0 Then
            Exit Sub
        End If
       
        ' After deselecting rows, need these to get back to original cell
        Dim lRowOriginal As Long: lRowOriginal = .MouseRow
        Dim lColOriginal As Long: lColOriginal = .MouseCol
   
        ' Don't understand this, the code was downloaded.
        Dim l As Long
        Dim lWidth As Long
        For l = 0 To .Cols - 1
            If .ColIsVisible(l) Then
                lWidth = lWidth + .ColWidth(l)
            End If
        Next
       
        If .RowHeightMin = 0 Then
            m_IsOnFixedPart = (x < .ColWidth(0)) Or _
                       (x > lWidth) Or _
                       (y < .RowHeight(0))
        Else
            m_IsOnFixedPart = (x < .ColWidth(0)) Or _
                       (x > lWidth) Or _
                       (y < .RowHeight(0)) Or _
                       (y > .Rows * .RowHeightMin)
        End If
                       
        m_IsZeroRow = (.RowHeight(0) >= y)
       
       
        '' Highlighting
        If Not m_IsOnFixedPart Or m_IsZeroRow Then
            DeselectAllRows
            .Row = lRowOriginal
            .Col = lColOriginal
        Else
            If (Shift And vbShiftMask) = vbShiftMask Then
                SelectContigious 'Select from the current row to the clicked row
            ElseIf (Shift And vbCtrlMask) = vbCtrlMask Then
                ToggleSelect  'Toggle the select in the clicked row
            Else
                 DeselectAllRows
               
                'select clicked row
                 .RowSel = .Row
                 .ColSel = .Cols - 1
                .CellBackColor = vbHighlight
                .CellForeColor = vbHighlightText

            End If  '' If (Shift And vbShiftMask) = vbShiftMask Then
        End If  '' If Not m_IsOnFixedPart Or m_IsZeroRow Then
    End With
End Sub


Private Sub DeselectAllRows()
   
    Dim lRowOriginal As Long
    With oGrid
        lRowOriginal = .Row
        .Row = .FixedRows
        .Col = .FixedCols
        .RowSel = .Rows - 1
        .ColSel = .Cols - 1
        .FillStyle = flexFillRepeat
       
        .HighLight = flexHighlightNever
        .CellBackColor = .BackColor
        .CellForeColor = .ForeColor
        .Row = lRowOriginal
        .ColSel = .Cols - 1
    End With

End Sub

Private Sub SelectContigious()
'Select from the current row to the clicked row
    Dim lRow As Long
    Dim lMouseRow As Long

    With oGrid
        lRow = .Row
        lMouseRow = .MouseRow
        .FillStyle = flexFillRepeat
        .Row = lRow
        .Col = .FixedCols
        .RowSel = lMouseRow
        .ColSel = .Cols - 1
        .HighLight = flexHighlightNever
        .CellBackColor = vbHighlight
        .CellForeColor = vbHighlightText
    End With
End Sub

Private Sub ToggleSelect()
 'Toggle the select in the clicked row
    With oGrid
        .Row = .MouseRow
        .FillStyle = flexFillRepeat
        .Col = .FixedCols
       
        .ColSel = .Cols - 1
        .HighLight = flexHighlightNever
        If .CellBackColor = vbHighlight Then
            .CellBackColor = .BackColor
            .CellForeColor = .ForeColor
        Else
            .CellBackColor = vbHighlight
            .CellForeColor = vbHighlightText
        End If
    End With
End Sub
0
dbrckoviCommented:
Thanks
0
TamilselvanCommented:
answer I expected is not  this.  when i click  first row . first row selects.  when i click second row, both of first row and second row are selected. my requirement is when i click second row, second row should be  selected only. first row should not be selected
0
TamilselvanCommented:
please , I want proper answer for this question
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.