将gridview背景颜色设置为从数据表中颜色化? [英] Set gridview backcolor to color from datatable?

查看:189
本文介绍了将gridview背景颜色设置为从数据表中颜色化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Row1  Row2 Row3 Row4 Row5 Row6 
金色黄金
粉红色粉红色
#FB7703#FB7703
红色红色
黄色
绿色
#0B93E1
Purple

当我将数据表绑定到网格时,这就是网格的样子:



如何将gridview中的单元格的背景颜色设置为单元格中的颜色?



我知道我需要使用 RowDataBound



Markup for gridview:

 < div> 
< asp:GridView ID =GridViewClicksrunat =server
onrowdatabound =GridViewClicks_RowDataBound>
< / asp:GridView>
< / div>

代码隐藏填充数据表:

  DataTable dataTable = GetColors(); 

DataTable gridTable = new DataTable();
gridTable.Columns.Add(Row1,typeof(string));
gridTable.Columns.Add(Row2,typeof(string));
gridTable.Columns.Add(Row3,typeof(string));
gridTable.Columns.Add(Row4,typeof(string));
gridTable.Columns.Add(Row5,typeof(string));
gridTable.Columns.Add(Row6,typeof(string));

for(int i = 0; i <8; i ++)
{
var r = gridTable.NewRow();
gridTable.Rows.Add(r);


foreach(dataTable.Rows中的DataRow)
{
int rowNum = Convert.ToInt16(r [1]) - 1;
int colNum = Convert.ToInt16(r [3]);
gridTable.Rows [rowNum] [colNum] = r [color]。ToString();
}

GridViewClicks.DataSource = gridTable;
GridViewClicks.DataBind();

谢谢。

解决方案

您可以检查 RowDataBound 事件中每个单元格的值,并根据它的值为单元格着色。

  protected void GridViewClicks_RowDataBound(object sender,GridViewRowEventArgs e)
{
//检查当前行是否为数据行
if(e .RowType == DataControlRowType.DataRow)
{
//循环行中的所有单元格
foreach(e.Row.Cells中的TableCell单元格)
{
//检查颜色是十六进制还是字符串
if(cell.Text.Contains(#))
{
cell.BackColor = ColorTranslator.FromHtml(cell.Text );
}
else
{
cell.BackColor = Color.FromName(cell.Text);
}
}
}
}


I have a datatable that looks like this:

Row1       Row2    Row3    Row4    Row5    Row6
Gold       Gold              
Pink       Pink              
#FB7703    #FB7703               
Red        Red               
Yellow                       
Green                        
#0B93E1                      
Purple                       

This is what the grid looks like when When I bind the datatable to the grid:

How can set background color of the cells in the gridview to the color in the cell?

I know I need to use RowDataBound.

Markup for gridview:

<div>
    <asp:GridView ID="GridViewClicks" runat="server" 
        onrowdatabound="GridViewClicks_RowDataBound">
    </asp:GridView>
</div>

And codebehind that fills datatable:

DataTable dataTable = GetColors();

DataTable gridTable = new DataTable();
gridTable.Columns.Add("Row1", typeof(string));
gridTable.Columns.Add("Row2", typeof(string));
gridTable.Columns.Add("Row3", typeof(string));
gridTable.Columns.Add("Row4", typeof(string));
gridTable.Columns.Add("Row5", typeof(string));
gridTable.Columns.Add("Row6", typeof(string));

for (int i = 0; i < 8; i++)
{
    var r = gridTable.NewRow();
    gridTable.Rows.Add(r);
}

foreach (DataRow r in dataTable.Rows)
{
    int rowNum = Convert.ToInt16(r[1]) - 1;
    int colNum = Convert.ToInt16(r[3]);
    gridTable.Rows[rowNum][colNum] = r["color"].ToString();
}

GridViewClicks.DataSource = gridTable;
GridViewClicks.DataBind();

Thanks.

解决方案

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);
            }
        }
    }
}

这篇关于将gridview背景颜色设置为从数据表中颜色化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆