现在我想用不同的颜色设计这个不同的类.这怎么可能?
在RowDataBound上编写所有类并给出颜色并不容易.
还有其他方法吗?
码:
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的学生班需要相同的颜色(对于单元格),B需要其他颜色.等.
解决方法 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>
代码背后:
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; } }}总结
以上是内存溢出为你收集整理的c# – 如何使用基于单元格值的颜色设置Asp.net GridView单元格的样式全部内容,希望文章能够帮你解决c# – 如何使用基于单元格值的颜色设置Asp.net GridView单元格的样式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)