I have two MS Access databases. I would like to change the forecolor of an item in a Gridview, if the item is listed in another table. I know how to change the color based on a value in the current table, but not sure how to do it if it's listed in another
table. Any thoughts would be appreciated.
Example below: I have two GridViews that pull from an Access DB, each with one column. Since ServerB and ServerC are listed in Table 2, I wish to have those two servers show up in red in Table1.
I made like this: Go to your designe page, click on your gridview ~> Properties ~> Click on the lightining bolt (Events page) and double click on RowDataBound, then code this !
n2netwrkng
Member
26 Points
61 Posts
Change forecolor in Gridview if value matches a value from another table
Dec 21, 2012 02:09 AM|LINK
I have two MS Access databases. I would like to change the forecolor of an item in a Gridview, if the item is listed in another table. I know how to change the color based on a value in the current table, but not sure how to do it if it's listed in another table. Any thoughts would be appreciated.
Example below: I have two GridViews that pull from an Access DB, each with one column. Since ServerB and ServerC are listed in Table 2, I wish to have those two servers show up in red in Table1.
Table 1 Table 2
ServerA ServerB
ServerB ServerC
ServerC
ServerD
NadeemZee
Participant
942 Points
179 Posts
Re: Change forecolor in Gridview if value matches a value from another table
Dec 21, 2012 03:47 AM|LINK
You can use row DataBound event to highlight rows conditionally.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { // check your condition here if (e.Row.RowType == DataControlRowType.DataRow) { string strValue = DataBinder.Eval(e.Row.DataItem, "SomeColumn").ToString(); //do something with the values and set color switch (strValue) { // change row color case "Something 1": e.Row.BackColor = System.Drawing.Color.Red; break; case "Something 2": e.Row.BackColor = System.Drawing.Color.Yellow; break; case "Something 3": e.Row.BackColor = System.Drawing.Color.Violet; break; case "Something 4": e.Row.BackColor = System.Drawing.Color.Teal; break; } } }Do FEAR (Face Everything And Rise)
Please mark as Answer if my post helps you..!
prabu.raveen...
Contributor
5032 Points
956 Posts
Re: Change forecolor in Gridview if value matches a value from another table
Dec 21, 2012 04:55 AM|LINK
Hi,
Execute the below query and bind the result into gridview1,
select *,ISNULL((Select 'Red' from Table2 where Table1.ServerName=Table2.ServerName),'') Color from Table1
And then Write the below code in RowDataBound event of gridview1 as below,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// check your condition here
if (e.Row.RowType == DataControlRowType.DataRow)
{
string strValue = DataBinder.Eval(e.Row.DataItem, "Color").ToString();
if(strValue=="Red") e.Row.Cells(namefieldindex).ForeColor = Drawing.Color.Red;
}
}
oned_gk
All-Star
36080 Points
7364 Posts
Re: Change forecolor in Gridview if value matches a value from another table
Dec 21, 2012 09:34 AM|LINK
GridView1.DataBind(); GridView2.DataBind(); foreach (GridViewRow gr1 in GridView1.Rows) { foreach (GridViewRow gr2 in GridView2.Rows) { if (gr1.Cells[0].Text == gr2.Cells[0].Text) { gr1.BackColor = System.Drawing.Color.Red; } } }Suwandi - Non Graduate Programmer
Ghaleon
Member
24 Points
96 Posts
Re: Change forecolor in Gridview if value matches a value from another table
Dec 21, 2012 10:53 AM|LINK
I made like this: Go to your designe page, click on your gridview ~> Properties ~> Click on the lightining bolt (Events page) and double click on RowDataBound, then code this !
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[1].Text == "SomeValue") { e.Row.Cells[1].BackColor = System.Drawing.Color.Red; } } }OBs: the number between the " [ ]" is the indice of your column, remeber that the first column is the "0" not 1 =)
First Column = e.Row.Cell[0]...
Second Column = e.Row.Cell[1] .. . =)