Search code examples
vb.netbackcolor

vb.net datatable to excel with color


in vb.net i have datatable which the value from database here the format.

enter image description here

Is this possible when i want to make the style and color with the rules :

if nilai2 >= nilai1 then 
          hasil.backcolor = color.red
else if nilai2 < nilai1 then 
          hasil.backcolor = color.white
end if

Output : enter image description here

Thanks before


Solution

  • I have get the answer. Here my code :

    xlrange = sobjSheet.Range(columnName & "2:" & columnName & "" & dgvhasil.RowCount + 1)
    With xlrange
                .FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlLess, "0")
                .FormatConditions(1).font.ColorIndex = 3
            End With
    

    Thanks for your attentions