更改动态创建的行的颜色

时间:2015-10-08 12:31:53

标签: vb.net colors row

我使用for循环创建了几行,但是我如何更改整行的颜色?

这是我的循环:

For i As Integer = 0 To nameGruppeUmsatzFarbe.Count - 1
        Dim name As String = CType(nameGoupMoneyColor(i)(0), String)
        Dim group As String = CType(nameGoupMoneyColor(i)(1), String)
        Dim money As String = CType(nameGoupMoneyColor(i)(2), String)
        If group= "A" Then
            DataGridViewUmsatzEntwicklung.Rows.Add(name, money)
        End If
        If group= "B" Then
            DataGridViewUmsatzSupport.Rows.Add(name, money)
        End If
        If group= "C" Then
            DataGridViewUmsatzVertrieb.Rows.Add(name, money)
        End If
        If group= "D" Then
            DataGridViewUmsatzAzubis.Rows.Add(name, money)
        End If
        If group= "E" Then
            DataGridViewUmsatzLeitung.Rows.Add(name, money)
        End If
    Next

1 个答案:

答案 0 :(得分:0)

你必须使用DataGridViewRow

如果你想为所有人提供相同的颜色,那就是DataGridViews:

  Dim mRow As New DataGridViewRow

    For i As Integer = 0 To nameGruppeUmsatzFarbe.Count - 1
        Dim name As String = CType(nameGoupMoneyColor(i)(0), String)
        Dim group As String = CType(nameGoupMoneyColor(i)(1), String)
        Dim money As String = CType(nameGoupMoneyColor(i)(2), String)

        If group = "A" Then
            mRow = DataGridViewUmsatzEntwicklung.Rows(DataGridViewUmsatzEntwicklung.Rows.Add())
        End If
        If group = "B" Then
            mRow = DataGridViewUmsatzSupport.Rows(DataGridViewUmsatzSupport.Rows.Add())
        End If
        If group = "C" Then
            mRow = DataGridViewUmsatzVertrieb.Rows(DataGridViewUmsatzVertrieb.Rows.Add())
        End If
        If group = "D" Then
            mRow = DataGridViewUmsatzAzubis.Rows(DataGridViewUmsatzAzubis.Rows.Add())
        End If
        If group = "E" Then
            mRow = DataGridViewUmsatzLeitung.Rows(DataGridViewUmsatzLeitung.Rows.Add())
        End If

        mRow.Cells(0).Value = name
        mRow.Cells(1).Value = money
        mRow.DefaultCellStyle.BackColor = Color.Red

    Next

对于不同的颜色:

        Dim mRow As New DataGridViewRow

        For i As Integer = 0 To nameGruppeUmsatzFarbe.Count - 1
            Dim name As String = CType(nameGoupMoneyColor(i)(0), String)
            Dim group As String = CType(nameGoupMoneyColor(i)(1), String)
            Dim money As String = CType(nameGoupMoneyColor(i)(2), String)

            If group = "A" Then
                mRow = DataGridViewUmsatzEntwicklung.Rows(DataGridViewUmsatzEntwicklung.Rows.Add())
            End If
            If group = "B" Then
                mRow = DataGridViewUmsatzSupport.Rows(DataGridViewUmsatzSupport.Rows.Add())
                mRow.DefaultCellStyle.BackColor = Color.Red
            End If
            If group = "C" Then
                mRow = DataGridViewUmsatzVertrieb.Rows(DataGridViewUmsatzVertrieb.Rows.Add())
                mRow.DefaultCellStyle.BackColor = Color.Purple
            End If
            If group = "D" Then
                mRow = DataGridViewUmsatzAzubis.Rows(DataGridViewUmsatzAzubis.Rows.Add())
                mRow.DefaultCellStyle.BackColor = Color.PowderBlue
            End If
            If group = "E" Then
                mRow = DataGridViewUmsatzLeitung.Rows(DataGridViewUmsatzLeitung.Rows.Add())
                mRow.DefaultCellStyle.BackColor = Color.SandyBrown
            End If

            mRow.Cells(0).Value = name
            mRow.Cells(1).Value = money


        Next