开发者

Determine the shape of multiple selected cells within a data grid

开发者 https://www.devze.com 2023-04-08 01:23 出处:网络
I\'m using the Infragistics UltraWinGrid to present some data. I ne开发者_开发知识库ed some Excel copy/paste functionality. That\'s the easy bit.

I'm using the Infragistics UltraWinGrid to present some data. I ne开发者_开发知识库ed some Excel copy/paste functionality. That's the easy bit.

  1. What I want to do is determine the shape of the selection, and ensure it's a rectangle of adjacent cells. If it's not, an error is to be shown (as pasting said data will just lead to errors).

  2. I then need to detect all the boundary edge cells so I can put a "you've copied this" border around the cells, ala Excel.

I'm hoping to achieve this by comparing a list of Point structures or something along those lines.

To illustrate the problem better, here are some pictures:

Determine the shape of multiple selected cells within a data grid

1. This is fine, see the edge cells (duh)

Determine the shape of multiple selected cells within a data grid

]

2. This won't work, I've tried to copy too much, show an error

Determine the shape of multiple selected cells within a data grid

3. The user hasn't learnt by now that this just won't work. You can even see where I've copied the "wrong" cell out of sheer lazyness.

I've got a Dictionary of Points and Cells, ready to work with. Any ideas? C#/VB is fine.

UPDATE: This might help?

Taking the second diagram:

0,0 1,0 2,0
0,1 1,1 2,1
0,2 1,2 2,2
0,3 1,3 2,3
    1,4     <<< wrong

Thanks, Tom


Ok I've had a good think about this and GertArnold has pointed me in the right direction, simply by saying "range of cells with a hole in it allowed?"

This is what happens when you spend 6 months not coding or problem solving, due to massive paperwork and auditing requirements, you forget how to even begin a simple algorithm.

It's so easy it hurts. Firstly I get the cells from the datasheet and reformat them so they're based at zero (as opposed to their actual position on the grid). I do this by simply negating the value of the cell at {0,0} and applying this to all the cells.

To calculate the gaps, do this:

Private Function HasGapsInCells(points As List(Of CellPoint)) As Boolean

    Dim colCount As Integer = points.GroupBy(Function(x) x.Column).Count()
    Dim rowCount As Integer = points.GroupBy(Function(x) x.Row).Count()

    For row As Integer = 0 To rowCount - 1
        Dim rowL As Integer = row
        For col As Integer = 0 To colCount - 1
            Dim colL As Integer = col
            If Not points.Exists(Function(x) x.Row = rowL And x.Column = colL) Then
                Return True '-- There are gaps'
            End If
        Next
    Next

    Return False

End Function

So easy.

0

精彩评论

暂无评论...
验证码 换一张
取 消