TonyReba
asked on
Change Cell Color dynamically in a Gridview Loop
I am trying to build a for loop that will go to each row in my gridview and check wheter a row has any of the following criterias, but I am not quite sure how to build the loop since right now I specify the column , please help
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (Int32 i = 0; i < GridView3.Columns.Count; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView row = e.Row.DataItem as DataRowView;
if (row["NT1_Status"].ToString() == "Assigned")
{
e.Row.Cells[0].BackColor = System.Drawing.Color.LightGreen;
}
else if (row["NT1_Status"].ToString() == "BR")
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Azure;
}
else if (row["NT1_Status"].ToString() == "OpenNS")
{
e.Row.Cells[0].BackColor = System.Drawing.Color.LightSalmon;
}
}
}
}
ASKER
Well really is not for specific column eg. Nt1. Is any cell in the grid with those statuses.
I was having a similar problem and I used the following logic in the page load event instead of the row binding to get around it.
DataTable dt = (DataTable)gridView.DataSo urce;
int rowIndex = 0;
foreach (GridViewRow gvr in gridView.Rows)
{
gvr.DataItem = dt.Rows[rowIndex++];
DataRow dr = (DataRow)gvr.DataItem;
object[] oa = dr.ItemArray;
if (oa[7].ToString().Length == 0)
{
gvr.Cells[7].BackColor = System.Drawing.Color.Red;
}
}
DataTable dt = (DataTable)gridView.DataSo
int rowIndex = 0;
foreach (GridViewRow gvr in gridView.Rows)
{
gvr.DataItem = dt.Rows[rowIndex++];
DataRow dr = (DataRow)gvr.DataItem;
object[] oa = dr.ItemArray;
if (oa[7].ToString().Length == 0)
{
gvr.Cells[7].BackColor = System.Drawing.Color.Red;
}
}
ASKER
I dont reallly see the logc there,
the function should look for all cells in the grid, but instead of saying
string statusValue = (string)DataBinder.Eval(e. Row.DataIt em, "NT1_Status");
where "NT1_Status" is the status field , i want to look for all cells and when the Status field of each column is for example
if (statusValue == "Assigned")
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Light Green;
}
but NT1_Staus is just one of many databound fields which are called e.g. NT2_Status.,,,
sorrry my english is very bas hope i get the idead.
the function should look for all cells in the grid, but instead of saying
string statusValue = (string)DataBinder.Eval(e.
where "NT1_Status" is the status field , i want to look for all cells and when the Status field of each column is for example
if (statusValue == "Assigned")
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Light
}
but NT1_Staus is just one of many databound fields which are called e.g. NT2_Status.,,,
sorrry my english is very bas hope i get the idead.
GridView3_RowDataBound occurs every time a row is bound to data in a GridView control.
Check:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
If you want to look at all the the other values, just repeat the same construct:
string status2Value = (string)DataBinder.Eval(e. Row.DataIt em, "NT2_Status");
//do similar code here
etc.
Note: For the column loop, you can even use the column name, instead of hard -coded names ("NT2_Status")
Question: Are you planning to change every cell or just cell[0] based on the contents.
Check:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
If you want to look at all the the other values, just repeat the same construct:
string status2Value = (string)DataBinder.Eval(e.
//do similar code here
etc.
Note: For the column loop, you can even use the column name, instead of hard -coded names ("NT2_Status")
Question: Are you planning to change every cell or just cell[0] based on the contents.
ASKER
I am not very familiar with GridView, but
I have a filed on the database for each column status , so the grid looks like
NT1 NT2 NT3
43 Assigned 65 BR
34 11 Assigned
22 BR
11
So on this case 43 and 11 cells should change color
if (statusValue == "Assigned")
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Light Green;
}
I have a filed on the database for each column status , so the grid looks like
NT1 NT2 NT3
43 Assigned 65 BR
34 11 Assigned
22 BR
11
So on this case 43 and 11 cells should change color
if (statusValue == "Assigned")
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Light
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I rewrite your code, notice the way i fetch the value:
string statusValue = (string)DataBinder.Eval(e.
You need to fech any required value that way and cast it to your needs.
Cheers,
Miguel
Open in new window