Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i'm working on hotel system using C# and SQL

now i'm working on the main page
i'm just trying to make rooms takes labels shape uhmm , to explain more

if the user wanted to insert a new room he's gonna insert some information into a row by saving button

now we have the room inserted in database
we need to make status column for rooms so i made it but all i want to do is
if that cell = "1" the room is gonna be free and its color is gonna be Lime-Green
and if it's "2" the room is gonna be Busy and its color is gonna be Red
and if it's "3" it's under maintenance and its color is gonna be blue ,etc
so the problem is when the room's color should change it changes all the rooms
uhmm like if the first row = 1 all the rooms is gonna be Lime-Green

here's my code:-

C#
private void FE_Method1()
        {
            foreach (DataRow C2 in dB1_PureEyezDataSet.Room.Rows)
            {
                try
                {
                    Label B1 = new Label();
                    B1.BackColor = Color.LimeGreen;
                    B1.Location = new Point(25, 35);
                    B1.Width = 104;
                    B1.Height = 100;
                    B1.Dock = DockStyle.Fill;
                    B1.Anchor = AnchorStyles.None;
                    B1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                    TLP1.Controls.Add(B1);
                    B1.BorderStyle = BorderStyle.FixedSingle;
                    B1.Text = "Test";
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                            switch (row.Cells[9].Value.ToString())
                            {
                                case "1":
                                    break;
                                    B1.BackColor = Color.LimeGreen;
                                case "2":
                                    break;
                                    B1.BackColor = Color.Red;
                                case "3":
                                    B1.BackColor = Color.Blue;
                                    break;
                            }
                    }
                }
                catch (Exception)
                {
                }
            }


if there's anyway to solve that problem with the same idea please help me.

Thanks
Posted
Updated 24-Jun-11 16:06pm
v2

I haven't looked at your problem yet but there are some fundamental errors in your code.

For example:
C#
foreach (DataGridViewRow row in dataGridView1.Rows)
{
        switch (row.Cells[9].Value.ToString())
        {
            case "1":
                break;    // <======================================== HERE ================
                B1.BackColor = Color.LimeGreen;
            case "2":
                break;    // <======================================== HERE ================
                B1.BackColor = Color.Red;
            case "3":
                B1.BackColor = Color.Blue;
                break;
        }
}

You have the break statement BEFORE the statement to color the label, so the statement will never be executed.

Also you are putting all of your labels in exactly the same place, so you will only ever see the last one to be added, with this B1.Location = new Point(25, 35); statement.
 
Share this answer
 
v3
Comments
snake1 29-Mar-11 7:48am    
oh i'm sorry i added the comment as a solution sorry for that

firstly thanks for ur attention

here's my new code after editing some errors

Collapse

private void FE_Method1()
{
foreach (DataRow C2 in dB1_PureEyezDataSet.Room.Rows)
{
try
{
Label B1 = new Label();
B1.BackColor = Color.LimeGreen;
B1.Width = 104;
B1.Height = 100;
B1.Dock = DockStyle.Fill;
B1.Anchor = AnchorStyles.None;
B1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
TLP1.Controls.Add(B1);
B1.BorderStyle = BorderStyle.FixedSingle;
B1.Text = "Test";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
switch (row.Cells[9].Value.ToString())
{
case "1":
B1.BackColor = Color.LimeGreen;
break;
case "2":
B1.BackColor = Color.Red;
break;
case "3":
B1.BackColor = Color.Blue;
break;
}
}
}
catch (Exception)
{
}
}
}

TLP1 is a tablelayoutpanel

the problem is that i'm using B1 it's an object from Label class so when i try to change that object backcolor it changes all the labels

is there anyway to solve this problem ?
I've had a look at your problem and whatever is causing your errors, it is NOT "the problem is that i'm using B1 it's an object from Label class so when i try to change that object backcolor it changes all the labels". That simply doesn't happen because each B1 is a separate instance and setting one of it's properties doesn't affect anything else.

Anyway, I have created a little test project and used most of your code in it. It works fine. I'll show you the code in a moment but I'll explain a couple of changes I've made first.

1. I changed your TableLayoutPanel to a FlowLayoutPanel because it makes adding the labels easier. To use a TableLayoutPanel you would need to know how many 'cells' you'd need and create that number of columns/rows first. With a FlowLayoutPanel you just keep adding controls and it takes care of the rest.
2. I changed the height of the labels to 25, only because it saved space for my quick test project.
3. The DataSet. I knocked up a quick DataSet with one table "Room". That table only has two columns "RoomName" and RoomStatus".
4. I have not used the DataGrid part of your code because I am not sure what it does and because it seemed to me to be redundant for this situation. I could be wrong on that and if the code below doesn't help you, you will have to explain what you are using the DGV for in more detail.

So, here's the code that works for me. I hope that you can apply it to your situation.
C#
foreach (DataRow C2 in this.dB1_PureEyezDataSet.Tables["Room"].Rows)
{
    try
    {
        Label B1 = new Label();
        B1.BackColor = ((int)C2["RoomStatus"] == 1) ? Color.LimeGreen : ((int)C2["RoomStatus"] == 2) ? Color.Red : Color.Blue;
        B1.Width = 104;
        B1.Height = 25;
        B1.Dock = DockStyle.Fill;
        B1.Anchor = AnchorStyles.None;
        B1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        FLP1.Controls.Add(B1);
        B1.BorderStyle = BorderStyle.FixedSingle;
        B1.Text = C2["RoomName"].ToString();
        //foreach (DataGridViewRow row in dataGridView1.Rows)
        //{
        //        switch (row.Cells[9].Value.ToString())
        //        {
        //            case "1":
        //                B1.BackColor = Color.LimeGreen;
        //                break;
        //            case "2":
        //                B1.BackColor = Color.Red;
        //                break;
        //            case "3":
        //                B1.BackColor = Color.Blue;
        //                break;
        //        }
        //}
    }
    catch (Exception)
    {
    }
}
 
Share this answer
 
v2

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