简体   繁体   English

C#中动态表格的备用行颜色

[英]alternate row color for dynamic table in c#

I want to give alternate color to Rows which are going to create dynamically. 我想给将动态创建的赋予其他颜色。 Following is the table structure in aspx.cs page, I am using 以下是aspx.cs页面中的表结构,我正在使用

         StringBuilder html = new StringBuilder();

         foreach (DataRow row in dsDashboardDetails.Tables[0].Rows)
            {

                html.Append("<tr style='color: Black'>");

                foreach (DataColumn column in dsDashboardDetails.Tables[0].Columns)
                {
                    html.Append("<td>");
                    html.Append(row[column.ColumnName]);
                    html.Append("</td>");
                }
                html.Append("</tr>");

            }
            //Table end.
            html.Append("</table>");

why not use css: 为什么不使用CSS:

<style type="text/css">
    tr:nth-child(odd) { background-color:#eee; }
</style>

Have you tried setting a counter and incrementing that whilst looping? 您是否尝试过设置计数器并在循环时将该计数器递增?

Then everytime you output a row and the counter is odd, for example, you can conditionally output a different style for the row. 然后,每次输出一行并且计数器为奇数时,例如,您可以有条件地为该行输出另一种样式。

It may be better to separate the styles as CSS and alternate class names whilst outputting so you don't have to modify your code when/if you want to change the styling of your site. 最好在输出时将样式分隔为CSS和备用类名,这样,当/如果您要更改网站的样式,则不必修改代码。

This is a safe way that works for old browsers. 这是适用于旧浏览器的安全方法。

StringBuilder html = new StringBuilder();
var rowCount = 0;
foreach (DataRow row in dsDashboardDetails.Tables[0].Rows)
{
    var className = rowCount++ % 2 == 0 ? "even" : "odd"; //check if it is an odd or even rowCount
    html.Append("<tr style='color: Black' class='"+className+"'>");

    foreach (DataColumn column in dsDashboardDetails.Tables[0].Columns)
    {
        html.Append("<td>");
        html.Append(row[column.ColumnName]);
        html.Append("</td>");
    }
    html.Append("</tr>");
}

//Table end.
html.Append("</table>");

Add this CSS for the Rows 为行添加此CSS

tr.odd{
    background-color: #999;
}
tr.even{
    background-color: #ccc;
}

It's really bad practice to mix layout (html) with backend code. 将布局(html)与后端代码混合在一起确实是一个坏习惯。 And I would suggest you'd solve your problem with a DataRepeater (or Razor depending on your project) in combination with CSS; 我建议您结合使用DataRepeater(或Razor,具体取决于您的项目)来解决您的问题; -- add a <table> first in the StringBuilder btw; -首先在StringBuilder btw中添加<table>

CSS: CSS:

table tbody tr:nth-child(odd) {
    background-color: #eee;
}

but if you want to stick to this concept you could use a "toggle" boolean, like so: 但是如果您要坚持这个概念,可以使用“ toggle”布尔值,如下所示:

bool background=false;
foreach (DataColumn column in dsDashboardDetails.Tables[0].Columns)
                {
                    html.Append("<td>");
if (background) { // add color }
background = !background;
                    html.Append(row[column.ColumnName]);
                    html.Append("</td>");
                }

You can use css , but if you're looking for pure C# , use a counter: 您可以使用css ,但是如果您正在寻找纯C# ,请使用一个计数器:

StringBuilder html = new StringBuilder();
var rowIndex = 0;
foreach (DataRow row in dsDashboardDetails.Tables[0].Rows) {
    html.AppendFormat("<tr style='color: {0}'>", 
        rowIndex % 2 == 0 ? "Black" : "Red"); // Red or anything you want
    foreach (DataColumn column in dsDashboardDetails.Tables[0].Columns) {
        html.Append("<td>");
        html.Append(row[column.ColumnName]);
        html.Append("</td>");
    }
    html.Append("</tr>");
}
//Table end.
html.Append("</table>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM