Dear all,
I have a database with 5 columns where I have written 0, 1 or 2.
Depending of the result in these 5 columns I have to change the color of 5 labels (green - 0, yellow - 1 or red - 2) in asp.net c#.
how can I accomplish this?
Regards,
Trifon Dinev.

Kirtan PatelRank #292 · 8.6K reputationPosted May 12, 2009, 6:23 AM EDT
Tell me the Table name
name and Values Within it
Trifon Dinev24 reputationPosted May 12, 2009, 6:36 AM EDT
table
alarms(id, smoke, hightemp, door, generator, generatorLowFuel);
only id is int, the others are char;
example data:
1, 1,0,2,0,0
2, 0,0,0,0,0
3,0,2,0,0,0
4,0,1,0,1,0
5,2,0,1,2,0
6,0,1,0,1,1
and now depending from the result my 5 labels must change their color green if I have 0, yellow if I have 1 or red if I have 2.
Thanks and regards,
Trifon Dinev.
Niradhip ChakrabortyRank #429 · 5.8K reputationPosted May 12, 2009, 7:57 AM EDT
You can read the value from database and assign it to some variable and based on the value in variable you ca assign the color:
say int intValue will hold the value from database.
if(intValue ==0)
{
Label1.ForeColor = System.Drawing.Color.SkyBlue;
}
else if(intValue ==1)
{
Label1.ForeColor = System.Drawing.Color.LightGreen;
}
Kirtan PatelRank #292 · 8.6K reputationPosted May 12, 2009, 8:40 AM EDT
And One Color Array
and Two Buttons Next And Previous For navigation in Database
Store the Currentrow Value in Session and Navigate Trough Database
Here is ScreenShot
public partial class _Default : System.Web.UI.Page
{
DataTable dt;
Color[] ColorArray= new Color[4];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["CurrentRow"] = 0;
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
int CurrentRecord = Convert.ToInt32(Session["CurrentRow"].ToString());
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand comm = new SqlCommand("Select * from Alarms", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(ds, "VAlarm");
dt = new DataTable();
dt = ds.Tables[0];
int RowIntable = dt.Rows.Count;
if (CurrentRecord < RowIntable)
{
int smoke = Convert.ToInt32(dt.Rows[CurrentRecord][1].ToString());
int hightemp = Convert.ToInt32(dt.Rows[CurrentRecord][2].ToString());
int door = Convert.ToInt32(dt.Rows[CurrentRecord][3].ToString());
int generator = Convert.ToInt32(dt.Rows[CurrentRecord][4].ToString());
int generatorlowfuel = Convert.ToInt32(dt.Rows[CurrentRecord][5].ToString());
ColorArray[0] = Color.Green;
ColorArray[1] = Color.Yellow;
ColorArray[2] = Color.Red;
ColorArray[3] = Color.Orange;
Label1.BackColor = ColorArray[smoke];
Label2.BackColor = ColorArray[hightemp];
Label3.BackColor = ColorArray[door];
Label4.BackColor = ColorArray[generator];
Label5.BackColor = ColorArray[generatorlowfuel];
Session["CurrentRow"] = CurrentRecord + 1;
}
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
int CurrentRecord = Convert.ToInt32(Session["CurrentRow"].ToString());
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand comm = new SqlCommand("Select * from Alarms", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(ds, "VAlarm");
dt = new DataTable();
dt = ds.Tables[0];
int RowIntable = dt.Rows.Count;
if (CurrentRecord > 0 && CurrentRecord < RowIntable)
{
int smoke = Convert.ToInt32(dt.Rows[CurrentRecord][1].ToString());
int hightemp = Convert.ToInt32(dt.Rows[CurrentRecord][2].ToString());
int door = Convert.ToInt32(dt.Rows[CurrentRecord][3].ToString());
int generator = Convert.ToInt32(dt.Rows[CurrentRecord][4].ToString());
int generatorlowfuel = Convert.ToInt32(dt.Rows[CurrentRecord][5].ToString());
ColorArray[0] = Color.Green;
ColorArray[1] = Color.Yellow;
ColorArray[2] = Color.Red;
ColorArray[3] = Color.Orange;
Label1.BackColor = ColorArray[smoke];
Label2.BackColor = ColorArray[hightemp];
Label3.BackColor = ColorArray[door];
Label4.BackColor = ColorArray[generator];
Label5.BackColor = ColorArray[generatorlowfuel];
Session["CurrentRow"] = CurrentRecord - 1;
}
}
}
If you cant Understand Code Above Download My Project Files and Look at Code
http://www.mediafire.com/?kxjdnowillz
Trifon Dinev24 reputationPosted May 14, 2009, 2:30 AM EDT
Thank you very much.
But what if I want to change the color of the button:
Ex: color is green in normal condition, if something happens and I have green and yellow label, the button to change in yellow, but if I have green, yellow and red, button color to change in RED - High priority?
The problem arise too if I have two tables and the button has to know the conditions from the two tables :(
Thanks again.
Trifon.