Jump to content

[GUIListViewEx] Set row color according to column value


trn80
 Share

Recommended Posts

Hi all, I am trying to create a GUIListViewEx where rows color are different based to the value of the listView column, but i can't understand how to achieve it.

The data to display comes from a database so I don't know how to create the colorsArray to pass in the _GUIListViewEx_LoadColour function.

If the c5 column values 1 the row must be green, else must be red, could you please help me?

Thank you in advance!

 

 

#include <SQLite.dll.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
#include <StaticConstants.au3>
#include <UpDownConstants.au3>
#include <EditConstants.au3>
#include <File.au3>
#include <Misc.au3>
#include <String.au3>
#include <Array.au3>
#include <GUIListViewEx.au3>

Global  $iGreen = "0x00FF00", _
        $iBlack = "0x000000", _
        $iRed = "0xFF0000", _
        $iWhite = "0xFFFFFF"

Global $sRet
; Create GUI

$hGUI = GUICreate("My GUI", 700, 230, -1, -1, BitOR($WS_SIZEBOX, $WS_SYSMENU))

    ; Create ListView For represent a db table with 6 column
    $idListview = GUICtrlCreateListView("C0|C1|C2|C3|C4|C5", 5, 5, 610, 180, BitOr($LVS_REPORT, $LVS_SHOWSELALWAYS, $WS_BORDER))
    _GUICtrlListView_SetExtendedListViewStyle($idListview, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_HEADERDRAGDROP))

    Global $dataPoolArray = retrieveData()  ; THE DATAPOOL SIZE IS UNKNOWN A THIS POINT, IT COMES FROM DATABASE
    _GUICtrlListView_AddArray($idListview, $dataPoolArray)

    ; Initiate ListView = sort on column click - editable headers - header colours - user colours
    $iLVIndex_1 = _GUIListViewEx_Init($idListview, $dataPoolArray, 0, 0, True, 1 + 8 + 32) ; + 16

    ; ****  IF the last column of the row is '1' the background color must be $iGreen, else must be $iRed  ****
    Local $dataPoolRowNum = UBound($dataPoolArray, $UBOUND_ROWS) ; Total number of rows. In this example it will be 10.
    Local $dataPoolColNum = UBound($dataPoolArray, $UBOUND_COLUMNS) ; Total number of columns. In this example it will be 20.
    ;~  ????  Global $aLVCol_1 = ?????
    ;~  ????  _GUIListViewEx_LoadColour($iLVIndex_1, ????? )

    GUISetState()
    While 1
            $iMsg = GUIGetMsg()
            Switch $iMsg
                Case $GUI_EVENT_CLOSE
                    ConsoleWrite("Ricevuto evento EXIT" & @CRLF)
                    Exit
            EndSwitch
    WEnd

    Func retrieveData()
        Local $array [2][6] = [['v00','v01','v02','v03','v04','1'],['v10','v11','v12','v13','v14','0']]
        return $array
    EndFunc

 

 

Link to comment
Share on other sites

  • Moderators

trn80,

Welcome to the AutoIt forums.

You can do it like this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#include <GUIListViewEx.au3>

Global $iGreen = "0x00FF00", $iRed = "0xFF0000"

Global $sRet
; Create GUI

$hGUI = GUICreate("My GUI", 700, 230, -1, -1, BitOR($WS_SIZEBOX, $WS_SYSMENU))

; Create ListView For represent a db table with 6 column
$idListview = GUICtrlCreateListView("C0|C1|C2|C3|C4|C5", 5, 5, 610, 180, BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS, $WS_BORDER))
_GUICtrlListView_SetExtendedListViewStyle($idListview, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_HEADERDRAGDROP))

Global $dataPoolArray = retrieveData()      ; THE DATAPOOL SIZE IS UNKNOWN A THIS POINT, IT COMES FROM DATABASE
_GUICtrlListView_AddArray($idListview, $dataPoolArray)

; Initiate ListView = sort on column click - editable headers - header colours - user colours
$iLVIndex_1 = _GUIListViewEx_Init($idListview, $dataPoolArray, 0, 0, True, 1 + 8 + 32)     ; + 16

; ****  IF the last column of the row is '1' the background color must be $iGreen, else must be $iRed  ****
Local $dataPoolRowNum = UBound($dataPoolArray, $UBOUND_ROWS)     ; Total number of rows. In this example it will be 10.
Local $dataPoolColNum = UBound($dataPoolArray, $UBOUND_COLUMNS)     ; Total number of columns. In this example it will be 20.

; Create colour array - 0-based to fit ListView content
Global $aLVCol_1[$dataPoolRowNum][$dataPoolColNum]
For $iRow = 0 To $dataPoolRowNum - 1
    If $dataPoolArray[$iRow][5] = 1 Then ; Check the required column and fill colour array accordingly
        For $iCol = 0 To $dataPoolColNum - 1
            $aLVCol_1[$iRow][$iCol] = ";" & $iGreen
        Next
    Else
        For $iCol = 0 To $dataPoolColNum - 1
            $aLVCol_1[$iRow][$iCol] = ";" & $iRed
        Next
    EndIf
Next

; Load colour array into ListView
_GUIListViewEx_LoadColour($iLVIndex_1, $aLVCol_1)

; If colours used then this function must be run BEFORE GUISetState
_GUIListViewEx_MsgRegister()

GUISetState()

While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            ConsoleWrite("Ricevuto evento EXIT" & @CRLF)
            Exit
    EndSwitch
WEnd

Func retrieveData()
    Local $array[2][6] = [['v00', 'v01', 'v02', 'v03', 'v04', '1'], ['v10', 'v11', 'v12', 'v13', 'v14', '0']]
    Return $array
EndFunc   ;==>retrieveData

I hope that is clear - please ask again if not. And in future if you have questions about any of my UDFs please post in the relevant thread as it flags it to me immediately.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...