Solved

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

Posted on 2013-05-09
6
606 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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 …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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