Luke Luke - 5 months ago 18
Vb.net Question

First 30 % displayed columns in DataGridView

First of all: 30% doesn't matter. That's a question of design. We can also say the first 3 Displayed Columns.

In my

DataGridView
I am using
BackgroundColors
for
Rows
to pass the User some information.

To keep this information visible to the user while Rows are being selected the first 30% of the columns should get the same
SelectionBack/ForeColor
as the
Back/ForeColor
.

So far that has never been a problem using


  • .cells(0).Style.SelectionBackColor = .cells(0).Style.Backcolor

  • (and so on).



Now I added the function that allows the user to reorder the Columns which makes the following Statement become true:


  • ColumnIndex != DisplayedIndex
    .



That statement beeing true makes the SelectionBackColor-Changed cells be somewhere mixed in the row and not in the first columns anymore. It still does the job, but looks terrible.

Is there something like a "DisplayedColumns" collection in order of the
.DisplayedIndex
Value that i could use to call the first few DisplayedColumns? If not, how could I effectivly create one my own?




Edit:

The user can also hide specific columns, that do not matter for him. So we have to be aware of
Column.DisplayedIndex
and
Column.Visble





Got it working with the following code:

Try
' calculate what is thirty percent
Dim colcount As Integer = oDic_TabToGridview(TabPage).DisplayedColumnCount(False)
Dim thirtyPercent As Integer = ((colcount / 100) * 30)
' Recolor the first 30 % of the Columns
Dim i As Integer = 0
Dim lastCol As DataGridViewColumn = oDic_TabToGridview(TabPage).Columns.GetFirstColumn(DataGridViewElementStates.Visible)
While i < thirtyPercent
.Cells(lastCol.Index).Style.SelectionBackColor = oCol(row.Item("Farb_ID") - 1)
.Cells(lastCol.Index).Style.SelectionForeColor = Color.Black
lastCol = oDic_TabToGridview(TabPage).Columns.GetNextColumn(lastCol, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
i += 1
End While
Catch ex As Exception
MsgBox(ex.Message & vbNewLine & ex.StackTrace)
End Try

Answer

Let us first assume you are coloring your rows somehow resembling the following manner:

Me.dataGridView1.Rows(0).DefaultCellStyle.BackColor = Color.PowderBlue
Me.dataGridView1.Rows(1).DefaultCellStyle.BackColor = Color.Pink
' ...etc.

In the DataGridView.CellPainting event handler, you can determine if the painting cell falls within the first N columns by utilizing the DataGridViewColumnCollection.GetFirstColumn and DataGridViewColumnCollection.GetNextColumn methods.

If the cell belongs to one these columns, set the cell's SelectionBackColor to the cell's BackColor. Otherwise set it to the default highlighting color.

Dim column As DataGridViewColumn = Me.dataGridView1.Columns.GetFirstColumn(DataGridViewElementStates.Visible)
e.CellStyle.SelectionBackColor = Color.FromName("Highlight")

' Example: Loop for the first N displayed columns, where here N = 2.
While column.DisplayIndex < 2
    If column.Index = e.ColumnIndex Then
        e.CellStyle.SelectionBackColor = e.CellStyle.BackColor
        Exit While
    End If

    column = Me.dataGridView1.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
End While

Example GIF reording columns but first 2 remain row colored

As a side note: You may want to consider changing the ForeColor on these cells for readability - depending on your row's BackColor choices. Likewise, if only a single cell is selected from one these first N columns, it can be difficult to notice.

Comments