【问题标题】:Change color of row based on value of a column in the row根据行中列的值更改行的颜色
【发布时间】:2011-01-19 17:12:33
【问题描述】:

我有一个 sqldatasource,它从我的服务器加载数据并将其放入数据网格中。

我有一个名为 clnumber 的列,其中包含数字 1、2、3

我想要的是每一行都有不同的颜色,具体取决于该数据行列中的数字

这是我使用的代码

 $(document).ready(function () {

    $("#<%=GridView1.UniqueID%> tr").each(function () {

        var number = $(this).children('td:eq(6)').text();

        if (number == 'OK') {
            $(this).children('td').css({ "background-color": "lightgreen" });

        }
    })
});

【问题讨论】:

  • jQuery 的 sn-p 的一个明显问题是 'Gridview1' 实际上并不指代任何东西。现在,'#Gridview1' 或 '.Gridview1' 可能真的有效。请参阅下面我的新帖子,其中包含针对此问题的 jQuery 解决方案。

标签: asp.net jquery vb.net gridview


【解决方案1】:

如果您为 gridview 提供了一个名为“myGridView”的 css 类,您可以执行以下操作:

$(document).ready(function () {

    $('.myGridView tr').each(function () {

        var number = $(this).children('td:eq(1)').text();

        if (number == '1') {
            $(this).children('td').css('background', 'red');
        }
    })
});

其中 'td:eq(1)' 指的是一行中的第二个单元格。这当然取决于行中的哪个单元格包含这个幻数。

我确定这不是 jQuery 最优雅的用法,但您可以根据需要对其进行重构

【讨论】:

  • 如果你已经有一个样式表,它可能会有所作为,虽然这不太可能,因为我在这里发布的内容将呈现内联 css(通过单元格标签的样式属性),这应该优先在你的样式表上。另请注意,我假设我的示例中的 gridview 有一个名为“myGridView”的 css 类。如果您尝试使用网格视图的 ID,我怀疑它不起作用,因为 ASP.NET 将在 HTML 输出中呈现不同的 ID。给网格视图一个 CSS 类名可以解决这个问题。
  • 它工作得很好,感谢唯一的问题是当我回发或更新面板时,网格所在的面板恢复正常:/
【解决方案2】:

根据数量如何?
第一行白色,第二行灰色?

if(rownumber%2 == 0) //white
else //grey

实际上反之亦然。

【讨论】:

  • 会自动为网格中的每一行执行此操作吗?
  • 把它放在 GridView_RowDataBound 事件中,否则如果你的 GridView 有分页,它只会应用于代码运行时存在的任何行。
  • 是的。任何数字除以二都是白色的,其他的都是灰色的。
  • 嗯,您能否调整该方法,例如,如果我想要任何带有字母 A、B、C 的行。很多事情我都需要这个
  • 你是什么意思?您将使用“A”、“B”和“C”而不是行号并为这些使用不同的颜色?在那种情况下,我只会使用静态字典 或其他东西。
【解决方案3】:

如果指示应该使用哪种颜色的数字实际上是在 HTML 输出中生成的,为什么不使用 javascript?

【讨论】:

  • 网上很多人都这么说,但我对javascript不熟悉,有人说jquery也是可能的
  • 是的,我会使用 jQuery。使用 SCRIPT 标记在页面的 HEAD 中引用 jQuery,然后简单地遍历每一行,检查每一行以及该行中包含该数字的特定单元格,并编写一个小函数,该函数将使用 CSS 设置该行的背景颜色。很抱歉,但这是一种比使用数据网格的事件处理程序来解决纯粹是布局/CSS 问题的方法要简单得多的方法。
  • 我尝试使用我将在上面发布的 jquery 代码,但它不起作用
【解决方案4】:

你可以使用类似这样的:

    /// <summary>
    /// Updates the row fore colour to show the line type
    /// </summary>
    /// <param name="sender">object</param>
    /// <param name="e">args</param>
    protected void gvLineValues_RowDataBound( object sender, GridViewRowEventArgs e )
    {
        try
        {
            //Format the row
            this.FormatRow( e );
        }
        catch ( Exception ex )
        {
            ErrorLogging.LogError( ex );
        }
    }

  /// <summary>
        /// Formats a gridview row
        /// </summary>
        /// <param name="e">Gridview event args</param>
        private void FormatRow( GridViewRowEventArgs e )
        {
            try
            {
                //Change the forecolor of the row
                if ( e.Row.RowType == DataControlRowType.DataRow )
                {
                    OrderLine oOrderLine = e.Row.DataItem as OrderLine;

                    if ( oOrderLine != null )
                    {
                        e.Row.ForeColor = oOrderLine.ForeColour;


                                //Check the line is over budget
                                if ( oOrderLine.OverBudget )
                                {
                                    e.Row.BackColor = ColourManager.OverBudgetItemBackColour;
                                    e.Row.ToolTip = String.Format( "Item over {0} and awaiting your approval", GlobalizationManager.Budget );
                                }
                                else
                                {
                                    e.Row.BackColor = ColourManager.WithinBudgetItemBackColour;
                                    e.Row.ToolTip = "Item awaiting your approval";
                                }
                            }
                        }

                        //Change the back colour of the row if its a deleted row
                        if ( oOrderLine.Deleted )
                        {
                            e.Row.Font.Strikeout = true;
                            e.Row.ToolTip = "This line has been deleted";
                        }
                    }
                }
            }
            catch ( Exception )
            {
                throw;
            }
        }

你可以做类似的事情

switch ( DataItem.ColorNumber )
{
case 1:

e.row.backcolor = Color.blue;

break;

case 2:

e.row.backcolor = Color.red;

break;

case 3:

e.row.backcolor = Color.white;

break;

}

【讨论】:

  • 感谢您的帮助,这可能是我使用的方法,除非我可以使用 javascript
猜你喜欢
  • 1970-01-01
  • 2015-09-02
  • 2013-05-07
  • 2022-01-12
  • 2019-03-09
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多