3

次のような DataGridView があります。 ここに画像の説明を入力

私は次のように DataGridView をバインドしています:

Dim cmd As New SqlCommand("DashBordFetch", con.connect)  
cmd.CommandType = CommandType.StoredProcedure   
cmd.Parameters.Add("@locid", SqlDbType.Int).Value = locid
da.SelectCommand = cmd
da.Fill(ds)
DGVDashBoard.DataSource = ds.Tables(0)

Value が 1 のときに DataGridView の行の色を赤にしたいのですが、どうすればよいですか? 私はVB.NETで作業しています。

提供された回答の1つでコードを試しましたが、次のようになります。

4

6 に答える 6

2
Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then

      If(your condition)
         e.Row.BackColor = Drawing.Color.Red 
      End If

    End If

  End Sub
于 2013-07-26T14:20:00.390 に答える
1

Senthilkumar に感謝します Text が Closed の場合に行の背景に色を付けることができるものを探していました。これが、値の後に ToString を追加する必要があった私の最終的な解決策です。DataGridviewTextBoxColumn16 の名前は、datagridview プロパティに由来します。DataGridView の右上にある矢印をクリックし、列を編集して名前の値を見つけます。それが誰かを助けることを願っています。

For Each rw As DataGridViewRow In DataGridView1.Rows
        If rw.Cells("DataGridViewTextBoxColumn16").Value.ToString = "Closed" Then
            rw.DefaultCellStyle.BackColor = Color.Red
        Else
            rw.DefaultCellStyle.BackColor = Color.Green
        End If
    Next
于 2015-02-18T17:51:23.193 に答える
1

'datagridview の cellformatting イベントで特定の行の色を変更するには、このコードを使用します

        For Each drview As DataGridViewRow In dgrawPRoducts.Rows
            If drview.Cells.Count >= 14 Then
                If Not IsNothing(drview.Cells(14).Value) Then
                    If drview.Cells(14).Value.ToString.Trim = "N" Then
                        For i As Integer = 0 To drview.Cells.Count - 1
                            drview.Cells(i).Style.ForeColor = Color.Red
                           drview.Cells(i).Style.BackColor= Color.Gray
                        Next

                    End If
                End If
            End If

        Next
于 2015-03-18T05:52:50.887 に答える
0

ここに私のコードがあります:

For Each row As GridViewRow In GridView1.Rows
    row.BackColor = Drawing.Color.White
Next row
于 2015-04-28T08:22:56.347 に答える
0
    For Each rw As DataGridViewRow In DataGridView1.Rows 
    If rw.Cells("slno").Value =1 Then 
    rw.DefaultCellStyle.BackColor = Color.Red
    Else 
    rw.DefaultCellStyle.BackColor = Color.Green
    End If 
    Next
于 2013-07-26T14:15:00.980 に答える