Set gridview backcolor to color from datatable?

前端 未结 1 481
我寻月下人不归
我寻月下人不归 2021-01-29 09:07

I have a datatable that looks like this:

Row1       Row2    Row3    Row4    Row5    Row6
Gold       Gold              
Pink       Pink                       


        
相关标签:
1条回答
  • 2021-01-29 09:43

    You can check the value of each cell in the RowDataBound event and color the cell based on it's value.

    protected void GridViewClicks_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //check if the current row is a datarow
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //loop all the cells in the row
            foreach (TableCell cell in e.Row.Cells)
            {
                //check if the color is hex or a string
                if (cell.Text.Contains("#"))
                {
                    cell.BackColor = ColorTranslator.FromHtml(cell.Text);
                }
                else
                {
                    cell.BackColor = Color.FromName(cell.Text);
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题