Home Forums WinForms controls Xceed Grid for WinForms cell select Reply To: cell select

User (Old forums)
Member
Post count: 23064

Here is what I have done so far. I hope it is OK that I’m posting so much code in the forum rather than providing a link to the file.

This should be a good start. It is not designed to work with most of the features that the xceed grid can do, so for learning / testing purposes I would stick to a non master/detail grid.

I am very new to the xceed grid so please let me know if you see anything that can be done in a much better way!

<pre>
‘=============================================================

‘ CellSelect Class

‘ NOTE: This is not intended to be anything miraculous at all
‘ it merely shows that this sort of thing can be done


‘ USE: Create a new instance of the CellSelect object passing
‘ in the instance of the grid you wish to use this on

‘ After data has filled the grid, call the initialization
‘ function

‘ Again, this is probably not anything you can use in
‘ its exact form, but it should be a good start to help
‘ you achieve what you are looking for. I hope someone
‘ finds this moderately useful

‘=============================================================

Public Class CellSelect
Dim grdGRID As Xceed.Grid.GridControl
Dim SELECTEDCELLS As New ArrayList ‘ An arraylist containing all of the currently selected cells
Dim startX As Integer
Dim starty As Integer
Dim MOUSEDOWN As Boolean = False
Dim BGColor As Color = Color.Fuchsia ‘just to remind me that I need to set a decent color

Sub New(ByVal ingrid As Xceed.Grid.GridControl, ByVal newcolor As Color)
grdGRID = ingrid
BGColor = newcolor
ingrid.SelectionMode = SelectionMode.None
End Sub

Public Function initialize()
Dim CurrCell As Xceed.Grid.Cell
Dim row As Xceed.Grid.DataRow

‘======


‘ Add mouse events to each cell


‘======
For Each row In grdGRID.DataRows
For Each CurrCell In row.Cells
AddHandler CurrCell.MouseDown, AddressOf CellMouseDown
AddHandler CurrCell.MouseUp, AddressOf CellMouseUp
AddHandler CurrCell.MouseMove, AddressOf CellMouseMove
AddHandler CurrCell.CellViewerChanged, AddressOf CellEdit
Next
Next
‘—======

End Function

Private Sub CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim inCell As Xceed.Grid.Cell
Dim x As Integer

MOUSEDOWN = True
For x = 0 To SELECTEDCELLS.Count – 1
CType(SELECTEDCELLS(x), Xceed.Grid.Cell).ResetBackColor()
Next
SELECTEDCELLS.Clear()

inCell = sender
startX = inCell.ParentColumn.VisibleIndex

Dim r As Xceed.Grid.DataRow
Dim c As Xceed.Grid.Cell

starty = Me.grdGRID.GetSortedDataRows(True).IndexOf(CType(inCell.ParentRow, Xceed.Grid.DataRow))
End Sub

Private Sub CellEdit(ByVal sender As Object, ByVal e As System.EventArgs)
MOUSEDOWN = False
End Sub

Private Sub CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim inCell As Xceed.Grid.Cell
Dim X As Integer
Dim Y As Integer
Dim tmp As Integer
Dim newY As Integer
Dim newX As Integer
‘Dim count As Integer
Dim r As Xceed.Grid.DataRow
Dim c As Xceed.Grid.Cell

MOUSEDOWN = False

inCell = sender

newY = Me.grdGRID.GetSortedDataRows(True).IndexOf(CType(inCell.ParentRow, Xceed.Grid.DataRow))
newX = inCell.ParentColumn.VisibleIndex

‘==========================

‘ Set startX/startY = lowest value

‘==========================
If startX > newX Then
tmp = startX
startX = newX
newX = tmp
End If
If starty > newY Then
tmp = starty
starty = newY
newY = tmp
End If

For Y = starty To newY
For X = startX To newX
SELECTEDCELLS.Add(Me.grdGRID.GetSortedDataRows(True)(Y).Cells(X))
Next
Next

For X = 0 To SELECTEDCELLS.Count – 1
CType(SELECTEDCELLS(X), Xceed.Grid.Cell).BackColor = Me.BGColor
Next

End Sub

Private Sub CellMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If MOUSEDOWN Then
Dim inCell As Xceed.Grid.Cell
Dim X As Integer
Dim Y As Integer
Dim tmp As Integer
Dim newY As Integer
Dim newX As Integer
Dim tmpStartX As Integer
Dim tmpStartY As Integer
Dim r As Xceed.Grid.DataRow
Dim c As Xceed.Grid.Cell

inCell = sender

For X = 0 To SELECTEDCELLS.Count – 1
CType(SELECTEDCELLS(X), Xceed.Grid.Cell).ResetBackColor()
Next

SELECTEDCELLS.Clear()

tmpStartX = startX
tmpStartY = starty

newY = Me.grdGRID.GetSortedDataRows(True).IndexOf(CType(inCell.ParentRow, Xceed.Grid.DataRow))
newX = inCell.ParentColumn.VisibleIndex

‘==========================

‘ Set startX/startY = lowest value

‘==========================
If tmpStartX > newX Then
tmp = tmpStartX
tmpStartX = newX
newX = tmp
End If
If tmpStartY > newY Then
tmp = tmpStartY
tmpStartY = newY
newY = tmp
End If

For Y = tmpStartY To newY
For X = tmpStartX To newX
SELECTEDCELLS.Add(Me.grdGRID.GetSortedDataRows(True)(Y).Cells(X))
Next
Next

For X = 0 To SELECTEDCELLS.Count – 1
CType(SELECTEDCELLS(X), Xceed.Grid.Cell).BackColor = Me.BGColor
Next

End If
End Sub

‘=======================================

‘ ChangeSelectedValues
‘ SDJXCEED

Imported from legacy forums. Posted by DrWurm (had 901 views)