首页 > 解决方案 > DataGridView 的特定格式

问题描述

我正在尝试在格式化 DataGridView 方面复制一个类似的程序。
这就是我希望它看起来的样子:

考勤表的图像

我试过这个:

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    ' Initial Styling of Cell Selection
    For i As Integer = 0 To DataGridView1.Rows.Count - 1
        For j As Integer = 0 To DataGridView1.Columns.Count - 1
            DataGridView1.Rows(i).Cells(j).Style.BackColor = Color.FromKnownColor(KnownColor.Control)
        Next
    Next
    Dim current As Integer = 0
    current = DataGridView1.SelectedCells(0).ColumnIndex
    Dim target As Integer = current
    Dim looping As Boolean = True
    While looping
        If target = 0 Then
            looping = False
        End If
        DataGridView1.SelectedRows(0).Cells(target).Style.BackColor = Color.Orange
        target = target - 1
    End While
    current = DataGridView1.SelectedRows(0).Index
    target = current
    Dim column As Integer = DataGridView1.SelectedCells(0).ColumnIndex
    looping = True
    While looping
        If target = 0 Then
            looping = False
        End If
        DataGridView1.SelectedRows(target).Cells(column).Style.BackColor = Color.Orange
    End While
End Sub

我还将选择模式设置为CellSelect并将该颜色更改为浅蓝色,如图所示。图片

我可能在某个地方搞砸了我的代码,但我的 VS 有一个错误,它会停止并让应用程序中断,但它不会向我显示异常。
有人知道我可以从这里做些什么来修复它吗?

标签: .netvb.netwinformsdatagridview

解决方案


Lokks 好的,你在行循环中错过了一个目标 = 目标 - 1

    target = current
    Dim column As Integer = DataGridView1.SelectedCells(0).ColumnIndex
    looping = True
    While looping
        If target = 0 Then
            looping = False
        End If
        DataGridView1.SelectedRows(target).Cells(column).Style.BackColor =        Color.Orange
        target -= 1
    End While

推荐阅读