如何风格基于单元格的值Asp.net GridView的细胞色 [英] How to style Asp.net GridView cells with colour based on cell value
本文介绍了如何风格基于单元格的值Asp.net GridView的细胞色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个叫的GridView
,它有一列 student_Class
。大约有80类上的网格视图。我已经使用分组查询GROUPBY这个类。
I have a Gridview
, it have a column called student_Class
. There are around of 80 Class on grid view. I have grouped this class using GroupBy query.
现在我想风格,不同颜色的这种不同的类。这怎么可能?结果
这是不容易写在的RowDataBound
所有类和给予的颜色。
Now I want to Style this different class with different color. How is it possible?
It is not easy to write all classes on RowDataBound
and giving color.
有没有别的办法?
code:
groups = (ArrayList)Session["selectedclass"];
SELECT id,name,student_Class FROM student where
student_Class='"+groups[0].ToString().Trim()+"'
group by student_Class.
给出的数据为
id name student_class
1 aa A
2 bb A
3 cc A
4 dd B
5 ee B
6 as B
7 ss B
8 AZZ D
与值 A
Student类需要相同的颜色(单元)和 B
需要其他的颜色。等
The student class with value A
need same color(for cell) and B
need other color., etc.
推荐答案
ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1"
ondatabound="GridView1_DataBound" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="student_class" HeaderText="student_class"
SortExpression="student_class" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SiteConnectionString %>"
SelectCommand="SELECT * FROM [student]">
</asp:SqlDataSource>
背后code:
static string[,] ClassNames =
{
{"A","Red"},
{"B","Blue"},
{"C","Pink"},
{"D","Green"},
// and so on
};
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string className = e.Row.Cells[2].Text;
string color = "Black";
for (int i = 0; i <= ClassNames.GetUpperBound(0); i++)
{
if (ClassNames[i, 0] == className)
{
color = ClassNames[i, 1];
e.Row.Cells[2].ForeColor = Color.FromName(color);
e.Row.Cells[2].BorderColor = Color.Black;
break;
}
}
}
这篇关于如何风格基于单元格的值Asp.net GridView的细胞色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文