mercredi 6 mai 2015

Find Whether Specific Row Contains a Selected (Highlighted) Cell

At the moment, I've created a loop which runs through every cells in my datagridview to find if any cells are selected (highlighted), and if so that cell's row/ column header backcolor changes. The problem I have with this is that once the row and column count start to become a large amount, I start experiencing lag. So I wondering does anybody know a way of instantly being able to find whether a row or column contains a selected or highlighted cell without using a loop.

I was hoping that something like me.datagridview1.rows(1).selectedcells.count would exist, but haven't been able to find something like that, that works. The other option (which I'd prefer not to do), is color the headercells as the selection is made.

Here is what I am using to find the selections. I have it looping through only the displayed cells to reduce the lag (which works), but I need the headercells colored, even when the selected cells are out of view (which then doesn't work).

            a = .FirstDisplayedCell.RowIndex
        Do While a < .FirstDisplayedCell.RowIndex + .DisplayedRowCount(True)
            b = .FirstDisplayedCell.ColumnIndex
            Do While b < .FirstDisplayedCell.ColumnIndex + .DisplayedColumnCount(True)
                If .Rows(a).Cells(b).Selected = False Then
                    .Rows(a).HeaderCell.Style.BackColor = colorfieldheader
                    .Columns(b).HeaderCell.Style.BackColor = colorfieldheader
                End If
                b += 1
            Loop
            a += 1
        Loop

        a = .FirstDisplayedCell.RowIndex
        Do While a < .FirstDisplayedCell.RowIndex + .DisplayedRowCount(True)
            b = .FirstDisplayedCell.ColumnIndex
            Do While b < .FirstDisplayedCell.ColumnIndex + .DisplayedColumnCount(True)
                If .Rows(a).Cells(b).Selected = True Then
                    .Rows(a).HeaderCell.Style.BackColor = colorfieldheaderhighlight
                    .Columns(b).HeaderCell.Style.BackColor = colorfieldheaderhighlight
                End If
                b += 1
            Loop
            a += 1
        Loop

Aucun commentaire:

Enregistrer un commentaire