Solved

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

Posted on 2013-05-09
6
602 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
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.

708 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

17 Experts available now in Live!

Get 1:1 Help Now