RecipeDan
asked on
GridView BackColor
I have a gridview that looks like this. There a way I can extend the backcolor of the row so that goes to multiple rows. For example, show the color blue from 1.5 to 3.25 for Dan, the color red from 1.5 to 2.75 for Mike
Dan Mike John
1 X
1.5 X X
2
2.5
2.75 X
3
3.25 X
4
5 X
Dan Mike John
1 X
1.5 X X
2
2.5
2.75 X
3
3.25 X
4
5 X
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drview = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text == "1")
{
e.Row.Cells[1].Text = "X";
e.Row.Cells[1].BackColor = System.Drawing.Color.Blue;
}
if (e.Row.Cells[2].Text == "1")
{
e.Row.Cells[2].Text = "X";
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
if (e.Row.Cells[3].Text == "1")
{
e.Row.Cells[3].Text = "X";
e.Row.Cells[3].BackColor = System.Drawing.Color.Yellow;
}
}
}
ASKER
I am getting two errors with your code:
bad array declarator
min does not exist in the current context
I fixed the errors and loaded LoadColorsGrid from RowDataBound after everything was done. Should I have loaded it from somewhere else? I attached a screenshot of the results but it is not correct neither. I do not know why one column has two rows colored and the second column only has one and the third has none.
bad array declarator
min does not exist in the current context
I fixed the errors and loaded LoadColorsGrid from RowDataBound after everything was done. Should I have loaded it from somewhere else? I attached a screenshot of the results but it is not correct neither. I do not know why one column has two rows colored and the second column only has one and the third has none.
protected void LoadColorsGrid()
{
System.Drawing.Color[] colorList = new System.Drawing.Color[3];
colorList[0] = Color.Blue;
colorList[1] = Color.Red;
colorList[2] = Color.Yellow;
for(int i = 0; i < 3 ; i++)
{
bool started = false;
foreach(GridViewRow row in GridView1.Rows)
{
if(row.Cells[i].Text == "X")
{
if(!started) started = true; else break;
}
if(started) row.Cells[i].BackColor = colorList[i];
}
}
}
Screenshot.png
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works great. Thank you for your assistance. I spent the last two days trying to figure it out.
Open in new window