Click here to Skip to main content
16,019,768 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have try to change the specific button color,if the the database status contain yes. but it change all button color.

What I have tried:

C#
Button newPanelcategory = new Button();

                newPanelcategory.Name = dt.Rows[i]["table_id"].ToString();
                newPanelcategory.Text = dt.Rows[i]["table_id"].ToString();
                newPanelcategory.Location = System.Drawing.Point.Add(new Point(0, 4 + i * 55), new Size(50, 50));
                newPanelcategory.Height = 50;
                newPanelcategory.Width = 75;
                newPanelcategory.FlatStyle = FlatStyle.Flat;
                newPanelcategory.Font = new Font("Times New Roman", 10, FontStyle.Bold);
                this.Controls.Add(newPanelcategory);
                flowLayoutPanel1.Controls.Add(newPanelcategory);
               newPanelcategory.BackColor = Color.Red;
                newPanelcategory.ForeColor = Color.White;
                newPanelcategory.Click += new EventHandler(newPanelcategory_Click);
                flowLayoutPanel1.AutoScroll = true;
                 DataTable dtms = blod.getallorder();
                for (int ia = 0; ia < dtms.Rows.Count; ia++)
                {
                    if ((dtms.Rows[ia]["active"]).ToString() == "yes")
                    {

                        newPanelcategory.BackColor =Color.Blue;
                
                    }
                    else 
                    {
                        newPanelcategory.BackColor = Color.Red;
                    
                    }
                }

            }
Posted
Updated 19-Feb-16 0:15am
v3
Comments
Sergey Alexandrovich Kryukov 19-Feb-16 11:02am    
You always need to indicate (tag) your UI framework/library when asking about UI.
—SA

1 solution

I am assuming your intent is to have one button per row in the table.

If that is correct, your for loop should contain the entire logic of creating button and setting the color.

With current code, your button color will always be determined by last row of the table.
 
Share this answer
 
Comments
Member 12242717 19-Feb-16 6:20am    
i think button does not know the table_id that is active, how to do that
dan!sh 19-Feb-16 6:22am    
As I said before, your for loop should contain entire code to create button and set color. Read your for loop code again. Button colour is determined by last row of table.
Member 12242717 20-Feb-16 20:49pm    
can i have a solution in code.
dan!sh 21-Feb-16 23:01pm    
No. You need to write your own code.
Member 12242717 22-Feb-16 5:36am    
thanks you..

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