Well i just tried to turn Rows cells that contain the word Yes into green on form load

when i put that code into a button it worked but on a form load i didn't work !

anyone can help me out ?

Thanks

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim j As Integer = 0
        For j = 0 To Me.DataGridView1.Rows.Count - 1

            If (Me.DataGridView1.Rows(j).Cells("StatusDataGridView").Value = "Yes") Then
                Me.DataGridView1.Rows(j).DefaultCellStyle.BackColor = Color.Green
            End If

        Next
     
       

    End Sub

Recommended Answers

All 4 Replies

jst first fill the data grid on form load nd then use this code there is no problem in the code ..
jst try to fill grid before use this

Ur playing with grid without binding any records to it.:)
u myt be getting error if im right. first bind records before u start playing with grid...

yeah ! well i paste it after that line of code in form load ! for the datagrid fill !
still doesn't work

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      

      
        Me.SitesTableAdapter.Fill(Me.Database1DataSet.sites)

    
     
         Dim i As Integer = 0
        For i = 0 To Me.DataGridView1.Rows.Count - 1


            If (DataGridView1.Rows(i).Cells("StatusDataGridView").Value = "Yes") Then
                DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Green
            End If

        Next

    End Sub

Well i just tried to turn Rows cells that contain the word Yes into green on form load

when i put that code into a button it worked but on a form load i didn't work !

anyone can help me out ?

Thanks

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim j As Integer = 0
        For j = 0 To Me.DataGridView1.Rows.Count - 1

            If (Me.DataGridView1.Rows(j).Cells("StatusDataGridView").Value = "Yes") Then
                Me.DataGridView1.Rows(j).DefaultCellStyle.BackColor = Color.Green
            End If

        Next
     
       

    End Sub

Not sure if this will help, but I have this bit of code that I use to highlight cells in a datagrid based on the cells value:

Private Sub DemoOneForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = My.Forms.MainForm
        Dim i As Integer
        For i = 0 To Me.customerDataGridView.Columns.Count - 1
            Me.customerDataGridView.Columns(i).Name = Me.customerDataGridView.Columns(i).DataPropertyName
        Next
        ' Don't allow new records to be added.
        Me.customerDataGridView.AllowUserToAddRows = False

    End Sub
Private Sub customerDataGridView_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles customerDataGridView.CellFormatting

        If Me.customerDataGridView.Columns(e.ColumnIndex).Name = "FD_R^2" Then
            If e.Value IsNot Nothing Then
                If CDbl(e.Value.ToString) > CDbl(My.Settings.FDr2LowValue) And CDbl(e.Value.ToString) < CDbl(My.Settings.FDr2HighValue) Then
                    e.CellStyle.BackColor = My.Settings.FDRcolor
                End If
            End If
        End If
End Sub

Then I have a button click event that Invalidates the datagrid when the value changes:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.customerDataGridView.Invalidate()
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.