Changing color of cell in datagridview based on a condition.. CODE not working

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 117
Reputation: kavithabhaskar is an unknown quantity at this point 
Solved Threads: 0
kavithabhaskar kavithabhaskar is offline Offline
Junior Poster

Changing color of cell in datagridview based on a condition.. CODE not working

 
0
  #1
Aug 6th, 2008
FOLKS:

I fill my data grid view with data and i try to color a few cells based on a condition but nothing seems to work..no errors either..can u pls help me >?


  1. Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  2.  
  3. Dim con As OleDbConnection = New OleDb.OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C:\Documents and Settings\bashkark\Desktop\Final Database.mdb")
  4.  
  5. con.Open()
  6.  
  7. Dim cmd As New OleDbCommand
  8.  
  9. cmd = New OleDbCommand("SELECT * from table2", con)
  10.  
  11. Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd)
  12.  
  13. Try
  14.  
  15. Dim ds As DataSet = New DataSet()
  16.  
  17. da.Fill(ds, "table2")
  18.  
  19. DataGridView1.DataSource = ds.Tables("table2").DefaultView
  20.  
  21. For Each r As DataGridViewRow In DataGridView1.Rows
  22.  
  23. If r.IsNewRow = False AndAlso Not TypeOf (r.Cells("MFG").Value) Is DBNull Then
  24.  
  25. Select Case r.Cells("MFG").Value.ToString.ToUpper
  26.  
  27. Case "DELL", "HP"
  28.  
  29. r.Cells("MFG").Style.BackColor = Color.Green
  30.  
  31. End Select
  32.  
  33. End If
  34.  
  35. Next
  36.  
  37. Finally
  38.  
  39. con.Close()
  40.  
  41. cmd = Nothing
  42.  
  43. da.Dispose()
  44.  
  45. con.Dispose()
  46.  
  47. End Try
  48.  
  49. con.Close()
  50.  
  51. cmd = Nothing
  52.  
  53. con.Dispose()
  54.  
  55. End Sub
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 117
Reputation: kavithabhaskar is an unknown quantity at this point 
Solved Threads: 0
kavithabhaskar kavithabhaskar is offline Offline
Junior Poster

Re: Changing color of cell in datagridview based on a condition.. CODE not working

 
0
  #2
Aug 6th, 2008
hi:

how do i get an else condition here? does it have to be a case format again ?

say if it was not DELL or HP and it was compaq.. i want to make it appear red..

pls assist..



  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.  
  3. Dim con As OleDbConnection = New OleDb.OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C:\Documents and Settings\bashkark\Desktop\Final Database.mdb")
  4.  
  5. con.Open()
  6.  
  7. Dim cmd As New OleDbCommand
  8.  
  9. cmd = New OleDbCommand("SELECT * from table2", con)
  10.  
  11. Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd)
  12.  
  13. Try
  14.  
  15.  
  16.  
  17. Dim ds As DataSet = New DataSet()
  18.  
  19. da.Fill(ds, "table2")
  20.  
  21. DataGridView1.DataSource = ds.Tables("table2").DefaultView
  22.  
  23. Finally
  24.  
  25.  
  26.  
  27. con.Close()
  28.  
  29. cmd = Nothing
  30.  
  31.  
  32.  
  33. da.Dispose()
  34.  
  35. con.Dispose()
  36.  
  37. End Try
  38.  
  39.  
  40.  
  41. For Each r As DataGridViewRow In DataGridView1.Rows
  42.  
  43. If r.IsNewRow = False AndAlso Not TypeOf (r.Cells("MFG").Value) Is DBNull Then
  44.  
  45.  
  46.  
  47. Select Case r.Cells("MFG").Value.ToString.ToUpper
  48.  
  49. Case "DELL", "HP"
  50.  
  51.  
  52.  
  53. r.Cells("MFG").Style.ForeColor = Color.Green
  54.  
  55. End Select
  56.  
  57.  
  58.  
  59. End If
  60.  
  61.  
  62.  
  63. Next
  64.  
  65.  
  66.  
  67. con.Close()
  68.  
  69. cmd = Nothing
  70.  
  71.  
  72.  
  73. con.Dispose()
  74.  
  75. End Sub
  76.  
  77.  
  78.  
  79. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  80.  
  81. End
  82.  
  83.  
  84.  
  85. End Sub
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2
Reputation: peteratoce is an unknown quantity at this point 
Solved Threads: 0
peteratoce peteratoce is offline Offline
Newbie Poster

Re: Changing color of cell in datagridview based on a condition.. CODE not working

 
0
  #3
Aug 8th, 2008
Look here for an example solution to your problem:
http://www.vb-tips.com/dbpages.aspx?...5-cf57ce269df2
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC