Link to home
Start Free TrialLog in
Avatar of searchsanjaysharma
searchsanjaysharma

asked on

how to make the datagridview row as red color if the cell value contains D

I am having the following code,
I want if in any row the cell value for a particular column in D then the row should be marked as red ie the backcolor


 Try
            con = New SqlConnection("Data Source=xxx.xxx.xxx.xxx;Initial Catalog=cusem2;User Id=sa;Password=tomato!@#$otamot")
            s1 = "select UID,Sname as 'Name',fname as 'Father',IMS from mstmassstudents where institute='" & CType(cmbinstitutes.SelectedItem, DataRowView)("institute").ToString() & "' and course='" & CType(cmbcourses.SelectedItem, DataRowView)("course").ToString() & "' and subjectcode='" & CType(cmbsubjectcodes.SelectedItem, DataRowView)("subjectcode").ToString() & "' order by 1"
            If (con.State = ConnectionState.Closed) Then
                con.Open()
            End If

            da1 = New SqlDataAdapter(s1, con)
            dt1 = New DataTable()
            da1.Fill(dt1)
            DataGridView1.DataSource = dt1
            DataGridView1.Visible = True
            DataGridView1.AllowUserToAddRows = False
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim obj(row.Cells.Count - 1) As Object
                If (row.Cells(3).Value = "D") Then
                    row.ReadOnly = True
                End If

            Next

        Catch ex As Exception
            MsgBox("Error occured : " & ex.ToString())
        Finally
            con.Close()

        End Try
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Hi..
Try
If row.Cells (3).Value.tostring.Contains ("D") then
     row.Cells (3).DefaultCellstyle.BackColor=Color.Red
End If

Open in new window

Avatar of searchsanjaysharma
searchsanjaysharma

ASKER

This doest work as DefaultCellstyle is not there.
I am sorry .  (FROM MOBILE)
Try style.BackColor

Yiannis
Not working
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial