【问题标题】:Specific formatting for DataGridViewDataGridView 的特定格式
【发布时间】:2019-02-24 17:34:22
【问题描述】:

我正在尝试在格式化 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,并将该颜色更改为浅蓝色,如图所示。 Image

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

【问题讨论】:

  • 调试你的代码。设置断点并逐行执行代码并检查其行为是否符合您的预期。如果您还没有调试,那么在这里发布还为时过早。如果您已经调试过,那么您可以在此处提供更具体的信息。

标签: .net vb.net winforms datagridview


【解决方案1】:

好吧,你在行循环中错过了一个目标 = 目标 - 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2014-02-27
    • 2017-07-24
    • 2023-03-22
    相关资源
    最近更新 更多