简体   繁体   English

根据行中列的值更改行的颜色

[英]Change color of row based on value of a column in the row

I have an sqldatasource that loads, data from my server and puts it in a datagrid. 我有一个sqldatasource,它从服务器加载数据并将其放入datagrid中。

I have a Column named clnumber that has, the numbers 1,2,3 我有一个名为clnumber的列,其数字为1,2,3

What I want is that each row have a different color depending on which number is in that datarow column 我想要的是每一行都有不同的颜色,具体取决于该数据行列中的数字

THIS IS THE CODE I USED 这是我使用的代码

 $(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" });

        }
    })
});

Granted you've given your gridview a css class called 'myGridView' you could do the following: 授予您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');
        }
    })
});

where 'td:eq(1)' refers to the second cell in a row. 其中“ td:eq(1)”是指行中的第二个单元格。 This of course will depend on what cell in your row contains this magic number. 当然,这取决于行中包含该幻数的单元格。

I'm certain it's not the most elegant use of jQuery, but you could refactor it as you wish 我敢肯定这不是jQuery最优雅的用法,但是您可以根据需要重构它

Based on the number how? 根据数量如何?
First row white, second grey? 第一排白色,第二排灰色?

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

or the other way around actually. 或实际上相反。

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

You can use similar to this: 您可以这样使用:

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

you could do something like 你可以做类似的事情

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;

}

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

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