【问题标题】:How to style Asp.net GridView cells with colour based on cell value如何根据单元格值使用颜色设置 Asp.net GridView 单元格的样式
【发布时间】:2013-09-07 05:46:25
【问题描述】:

我有一个 Gridview ,它有一个名为 student_Class 的列。网格视图上有大约 80 个类。我已使用 GroupBy 查询对此类进行了分组。

现在我想用不同的颜色为这个不同的类设置样式。这怎么可能?
在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需要其他颜色,等等。

【问题讨论】:

  • 你给一个单元格赋予不同颜色的标准是什么??
  • 同班学生的颜色相同@Ankur Karl Anderson
  • 如果您已将网格与数据库中的值绑定,请根据您的要求运行从 0 到网格行数的循环。使用下面的行为单元格 grd_AssetDistri.Rows[0].Cells[0].BackColor = System.Drawing.Color.YOURCOLOR; 赋予颜色
  • Ankur,如果你循环遍历整个网格,那么当我有 10000 行绑定到我的网格时会发生什么,这是否会降低我的性能?所以这不是给行着色的正确方法。
  • @TejasVaishnav :首先,网格包含超过 1K 行的可能性很小。如果您正在使用,那么设计将被折腾。

标签: c# asp.net gridview


【解决方案1】:

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

【讨论】:

  • 这是一个不错的方法。但我的表包含 80 种类型的 student_class。所以80例?!!! @Samiey Mehdi
【解决方案2】:

如果您只想根据值设置样式,我必须建议您在客户端使用 Jquery 或 javaScript。
此外,它不会影响性能,因为它是在客户端而不是在RowDataBound

代码:使用客户端 -(我更推荐)
在这里你可以根据你的类值设置尽可能多的条件,无需编写额外的服务器端代码

$(document).ready(function () {
        $(".myGvClass").find("td").each(function () {
            if ($(this).text() == "Class B") {
                $(this).css("color", "Red");
            }
            
            if ($(this).text() == "Class A") {
                $(this).css("color", "Blue");
            }
          
            if ($(this).text() == "Class C") {
                $(this).css("color", "green");
            }
          //  ..... and so on
    });



 

HTML 标记:

 <asp:GridView ID="GridView1" runat="server" CssClass="myGvClass">
 </asp:GridView>

代码隐藏:

 GridView1.DataSource = YourDataTable;
 GridView1.DataBind();

屏幕截图:



代码:使用服务器端
在myGridview_DataBound 事件中循环遍历 Gridview 行,并检查条件单元格值并设置相应的颜色。

protected void myGridview_DataBound(object sender, EventArgs e)
{
    for (int i = 0; i <= myGridview.Rows.Count - 1; i++)
    {
        string myClassVal = myGridview.Rows[i].Cells[2].Text;
        if (myClassVal == "Class A")
        {
            myGridview.Rows[i].Cells[2].BackColor = Color.Green;
         }
        else if (myClassVal == "Class B")
        {
            myGridview.Rows[i].Cells[2].BackColor = Color.Red;
        }
        else 
        {
           myGridview.Rows[i].Cells[2].BackColor = Color.Orange;
        }
    }
}

HTML:

<asp:GridView ID="myGridview" runat="server" ondatabound="myGridview_DataBound">
</asp:GridView>

代码隐藏:

myGridview.DataSource = YourDataTable;
myGridview.DataBind(); 

截图:

【讨论】:

  • 但我的问题是我有超过 20 个课程。 @Satinder 辛格
  • @ARATHY:您可以在客户端执行,而不是在 RowDataBound 的服务器端写入 30 次
  • 怎么样? @Satinder 辛格
【解决方案3】:

给猫换皮的方法有很多,但如果你真的不想使用 RowDataBound,你可以使用一个 TemplateColumn,在它的 ItemTemplate 中包含一个样式化的控件。例如

<asp:GridView ...>
  ...
  <Columns>
     ...
     <asp:TemplateField ...>
         <ItemTemplate>
            <asp:Panel ... CssClass='<%# GetStudentCssClass(Eval("student_Class")) %>'>
            ...
            </asp:Panel>
         </ItemTemplate>
         ...

【讨论】:

  • 三个答案都是正确的并且符合OP的要求,但是OP仍然在等待一些神奇的解决方案,这样他就不必为80值写检查,或者设置80 css等,
猜你喜欢
  • 1970-01-01
  • 2013-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 2011-07-13
  • 1970-01-01
相关资源
最近更新 更多