Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to change the backcolor for buttons depends on information from database,
i have form has multiple buttons like this picture bellow.

I have a database table for shops (stores),There is a column in this table that contains the shop number and a column for its status : Yes or No (if rented or empty)

in the model Each shop has a button and this button named as the number of the store number in the database
How can I do for loop to Retrieve store data from the database
and changes the color of the button depends on the data from the database if it is empty or rented.

thank you.

What I have tried:

picture :
https://d.top4top.net/p_671g4mp01.png
Posted
Updated 2-Nov-17 8:15am
v3
Comments
Mrunal Sonawane 2-Nov-17 13:18pm    
Need to get the structure of the database... Please give some information about the database... Picture of the table... etc etc... So that I can recreate the table and try to write a code for it...

1 solution

Hey... I made my own database and imported that database to Visual Studio using the DataGridView control... Do the same and then use this...
VB
Public Sub ExecuteColoring()
        Try
            Dim numberRows As Integer
            numberRows = DataGridView1.Rows.Count - 2
            'We subtract 2 because first of all it gives one extra and also we have to take care that the count here starts with 0 and not 1

            Do Until numberRows = -1
                Dim btnCurrent As Button

                btnCurrent = Me.Controls.Item("Button" + (numberRows + 1).ToString)

                'Add one to the button number because our button number starts from 1 and not 0
                If DataGridView1.Rows(numberRows).Cells(1).Value.ToString.ToLower = "false" Then
                    'If it is false then the shop is not rented... So the color will be green
                    btnCurrent.BackColor = Color.Green
                Else                     'If it is true then the shop is rented and the color will be red
                    btnCurrent.BackColor = Color.Red
                End If
                numberRows -= 1
            Loop
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


Also... Add this 'ExecuteColoring()' to the button press event so that whenever you press the button this thing will execute and change the color...
Here is a preview... Preview - https://ibb.co/kaE60w
I think this is exactly what you wanted...
And if you're worrying about the database being visible... You can always hide it from the properties...
Hope I helped!
 
Share this answer
 
v2
Comments
Mrunal Sonawane 2-Nov-17 14:18pm    
Feel free to ask me something you don't understand... :D
Rabee Qabaha 16-Nov-17 7:58am    
Thank you man , i solved the problem, appreciate your effort.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900