Home Forums WinForms controls Xceed Grid for WinForms How to select cell or row on right click?

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #13115 |

    Hi everyone,

    Got a question about the grid.
    My grid is populated and has a context menu – one of the menu items is ‘Remove from grid’ that will allow you to remove the select row or rows from the grid.
    I have assigned the display of the context menu (it is conditional based on a number of factors) to the mouseup event for the grid.
    So far, so good.

    If the user has selected one or more rows already, then it works exactly as expected – the menu is displayed, the user selected remove and the items are removed.

    Now – what I want to do is automatically select the row that the user right clicked over (currently you have to first left click to select then right click for the menu) and I have no idea how to identify which row needs to be selected based on the mouse point position.

    I do similar things with the standard windows tree view control and use:

    Dim p As Point = New Point(e.X, e.Y)
    Dim node As TreeNode = treeMinifigs.GetNodeAt(p)

    Is there something similar with the grid control?
    Hope someone can help as I am currently stuck!

    Richard.

    Imported from legacy forums. Posted by rjmorton (had 5065 views)

    User (Old forums)
    Member
    Post count: 23064

    You could use the <b>GetVisualGridElementAtPoint</b>-method. You can pass it a Point-object or 2 integers (int x, int y). It returns an object of type <b>VisualGridElement</b>. Check if you can cast the object to a Cell or a Row. If you can cast it to a cell, you can get the row from the <b>ParentRow</b>-property.

    Imported from legacy forums. Posted by Tommy (had 355 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Tommy

    Could you please explain it with a code!

    Thank You.

    Imported from legacy forums. Posted by PETE (had 434 views)

    User (Old forums)
    Member
    Post count: 23064

    I’m used to programming in C#, so the following piece of VB.NET could be wrong, but you get the idea:
    <code>
    Dim element As Xceed.Grid.VisualGridElement
    Dim row As Xceed.Grid.Row
    element = grid.GetVisualGridElementAtPoint(x, y)
    If element Is Xceed.Grid.Row Then row = element As Xceed.Grid.Row
    ElseIf element Is Xceed.Grid.Cell Then row = (element As Xceed.Grid.Cell).ParentRow
    Else row = null
    EndIf
    </code>

    Imported from legacy forums. Posted by Tommy (had 432 views)

    User (Old forums)
    Member
    Post count: 23064

    Hmm, still no closer. As a test, I tried the following in the cell_mousedown event:

    Dim element As Xceed.Grid.VisualGridElement
    Dim row As Xceed.Grid.DataRow
    element = gridInventory.GetVisualGridElementAtPoint(e.X, e.Y)

    No matter which cell I click in, element is always an Xceed.Grid.GroupByRow and I can’t see a way to get from the groupbyrow to the datarow to select it.

    This MUST be possible, I bet I’m doing something wrong. Anyone else got any ideas?

    Imported from legacy forums. Posted by rjmorton (had 518 views)

    User (Old forums)
    Member
    Post count: 23064

    Okay – finally managed to get this to work and thought I would post the results here, incase anyone else is struggling with this.

    I simply used the following code in the mousedown event for the cell (obviously making sure that I added the handler for each cell during grid population):

    ‘————————————————–
    Private Sub Cell_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If e.Button = MouseButtons.Right Then
    ‘Check to see if the cells datarow is already selected or not
    Dim oDataRow As DataRow = sender.ParentRow
    If Not oDataRow.IsSelected Then
    gridInventory.SelectedRows.Clear()
    gridInventory.SelectedRows.Add(oDataRow)
    End If
    End If
    End Sub
    ‘————————————————–

    This code also checks to see if the row you right clicked on was already selected or not. Obviously, if you are allowing multiple selections within a grid and the row you right clicked on was one of those selected, then you don’t want to clear the other rows that were selected.

    Hope someone finds this useful.
    Richard.

    Imported from legacy forums. Posted by rjmorton (had 5765 views)

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.