如何在asp.net c#中根据条件进行gridview行颜色 [英] How to gridview row color based on condition in asp.net c#

查看:271
本文介绍了如何在asp.net c#中根据条件进行gridview行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我想根据数据库列值更改gridview行的背景颜色。



在数据库中我有字段作为Result_Status

这里,我有两种类型的状态为PASS和FAIL



如果通过行背颜色应为绿色

如果失败,行背颜色应为红色。



我们需要在Gridview Row数据包中编写代码as



Hi friends,

I want to change the background color of gridview row based on database column value.

In database i have field as Result_Status
here, i have two types of Status as PASS and FAIL

If Pass the row back color should be Green
and if FAIL the row back color should be red.

We need to write code in Gridview Row Databound as

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {





}

}





请帮帮我。



谢谢。



}
}


Please can you help me.

Thanks.

推荐答案

你可以将css类应用到gridview行li ke follow



you can apply the css class to a gridview row like following

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if(Result_Status == "pass")
        {
             e.Row.CssClass = "green";
        }
        else
        {
             e.Row.CssClass = "red";
        }
    }
}


inside condition write

string statusVal=HttpUtility.HtmlDecode(e.Row.Cells[ColumnIndex].Text).Trim() ;

and columnIndex will be your columns index


then put if condition
if(statusVal=="PASS")
 e.Row.Cells[ColumnIndex].BackColor = ColorName;

ColorName is required color object


添加以下代码,请注意未经测试的一些修正将需要

add below code, and please note its not tested some correction will require
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    String statusVal = HttpUtility.HtmlDecode(e.Row.Cells[ColumnIndex].Text).Trim();
    if(statusVal =="Pass")
        {
            foreach (TableCell cell in e.Row.Cells) {
                cell.BackColor = Color.Yellow;
                }
        {
    if(statusVal =="Fail")
        {
            foreach (TableCell cell in e.Row.Cells) {
                cell.BackColor = Color.Red;
                }
        {

    }
}


这篇关于如何在asp.net c#中根据条件进行gridview行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆