Solved

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

Posted on 2013-05-09
6
605 Views
Last Modified: 2013-05-16
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
0
Comment
Question by:searchsanjaysharma
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 39154655
Hi..
Try
If row.Cells (3).Value.tostring.Contains ("D") then
     row.Cells (3).DefaultCellstyle.BackColor=Color.Red
End If

Open in new window

0
 

Author Comment

by:searchsanjaysharma
ID: 39154843
This doest work as DefaultCellstyle is not there.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 39154883
I am sorry .  (FROM MOBILE)
Try style.BackColor

Yiannis
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:searchsanjaysharma
ID: 39160421
Not working
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39168044
Change following

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

to

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
                    row.DefaultCellStyle.BackColor = Color.Red
                End If

            Next
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 39171987
tx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now