7

datagridview の行の色を変更する必要がありますが、コードが機能しません。「Quantity という名前の列: が見つかりません。パラメーター名: columnName」というエラーが常に表示されます。

これが私のコードです:

Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
        If Me.DataGridView1.Rows(i).Cells("Quantity:").Value < 5 Then
            Me.DataGridView1.Rows(i).Cells("Quantity:").Style.ForeColor = Color.Red
        End If
    Next
End Sub

修正を手伝ってください。ありがとうございました。

4

7 に答える 7

13

これは役立つかもしれません

  1. 「RowPostPaint」イベントを使用する
  2. 列の名前は、列の「ヘッダー」ではありません。DataGridViewのプロパティに移動する必要があります=>次に列を選択します=>次に「Name」プロパティを探します

これをC#から変換しました('From:http ://www.dotnetpools.com/Article/ArticleDetiail/?articleId=74 )

    Private Sub dgv_EmployeeTraining_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) 
    Handles dgv_EmployeeTraining.RowPostPaint

    If e.RowIndex < Me.dgv_EmployeeTraining.RowCount - 1 Then
        Dim dgvRow As DataGridViewRow = Me.dgv_EmployeeTraining.Rows(e.RowIndex)

    '<== This is the header Name
        'If CInt(dgvRow.Cells("EmployeeStatus_Training_e26").Value) <> 2 Then  


    '<== But this is the name assigned to it in the properties of the control
        If CInt(dgvRow.Cells("DataGridViewTextBoxColumn15").Value.ToString) <> 2 Then   

            dgvRow.DefaultCellStyle.BackColor = Color.FromArgb(236, 236, 255)

        Else
            dgvRow.DefaultCellStyle.BackColor = Color.LightPink

        End If

    End If

End Sub
于 2013-02-12T19:22:44.227 に答える
3

エラーを修正しました。この行から「値」を削除しました:

If drv.Item("Quantity").Value < 5  Then

だからそれは次のようになります

If drv.Item("Quantity") < 5 Then

于 2012-11-18T06:04:21.060 に答える
1

これを試してください(注:現在Visual Studioを持っていないので、コードはアーカイブからコピーアンドペーストされます(テストしていません):

Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    Dim drv As DataRowView
    If e.RowIndex >= 0 Then
        If e.RowIndex <= ds.Tables("Products").Rows.Count - 1 Then
            drv = ds.Tables("Products").DefaultView.Item(e.RowIndex)
            Dim c As Color
            If drv.Item("Quantity").Value < 5  Then
                c = Color.LightBlue
            Else
                c = Color.Pink
            End If
            e.CellStyle.BackColor = c
        End If
    End If
End Sub
于 2012-11-14T13:02:42.723 に答える
0

CellFormating イベントe引数の使用:

If CInt(e.Value) < 5 Then e.CellStyle.ForeColor = Color.Red

于 2016-05-03T21:32:46.807 に答える
0
Dim dgv As DataGridView = Me.TblCalendarDataGridView

For i As Integer = 0 To dgv.Rows.Count - 1
    For ColNo As Integer = 4 To 7
        If Not dgv.Rows(i).Cells(ColNo).Value Is DBNull.Value Then

            dgv.Rows(i).Cells(ColNo).Style.BackColor =  vbcolor.blue
        End If
    Next
Next
于 2016-06-30T11:05:18.980 に答える
0
If drv.Item("Quantity").Value < 5  Then

これを使ってこれを好きに

If Cint(drv.Item("Quantity").Value) < 5  Then
于 2014-05-19T11:50:41.287 に答える