lundi 30 avril 2018

if statement to color Datagridview rows binded to sql query

Alright, maybe i'm missing something simple but I can not get an if statement inside a for loop to color a datagridview row based on a cell's value. Maybe you guys can see something I can't. Here is my sql code

SELECT Part,
CASE WHEN missing = 1 or secondmissing = 1 then '1'
WHEN missing IS NULL AND secondMissing IS NULL then '0'
end as 'missing'
FROM TableA

This query returns a list of part numbers and if they have been marked as missing or not and appears to run fine.In my win forms application I have a For loop to check the datagridview loaded with the sql query above to color any rows yellow that have the missing marked as 1. However nothing I seem to try work for catching line items marked as 1. Here is my VB code.

For x as integer = 0 to DataGridView1.rows.count - 1
  With Datagridview1.rows(x)
    if .cells(1).value.tostring = "1" then *
       .DefaultCellStyle.Backcolor = Color.Yellow
   else
       .DefaultCellStyle.BackColor = Color.White
   End If
 End With
Next

Notice in the for loop I marked a line with an *. Here are all the things I have tried here.

if .cells("missing").value = True Then
if .cells(1).value.tostring = "1" Then
if CInt(.cells(1).value) = 1 Then
if CInt(.cells(1).value.tostring) = 1 Then

None of the above options seem to work. If I try and use the column name "missing" instead of the column integer of 1 it completley misses the code. Any help would be great.

EDIT: I guess I should note the sql column fields. Part is NvarChar(25) and both missing and secondmissing are bits.

Aucun commentaire:

Enregistrer un commentaire