将gridview背景色设置为数据表中的颜色? [英] Set gridview backcolor to color from datatable?
本文介绍了将gridview背景色设置为数据表中的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 datatable
看起来像这样:
Row1 Row2 Row3 Row4 Row5 Row6黄金黄金粉色 粉色#FB7703 #FB7703红色 红色黄色的绿#0B93E1紫色的
这是当我将数据表绑定到网格时网格的样子:
如何将gridview中单元格的背景色设置为单元格中的颜色?
我知道我需要使用 RowDataBound
.
gridview 的标记:
<asp:GridView ID="GridViewClicks" runat="server"onrowdatabound="GridViewClicks_RowDataBound"></asp:GridView>
以及填充数据表的代码隐藏:
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 r){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.Row.RowType == DataControlRowType.DataRow){//循环该行中的所有单元格foreach(e.Row.Cells 中的 TableCell 单元格){//检查颜色是十六进制还是字符串if (cell.Text.Contains("#")){cell.BackColor = ColorTranslator.FromHtml(cell.Text);}别的{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屋!
查看全文