Solved

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

Posted on 2013-05-09
6
611 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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