如何更改所选行的颜色,即使它有条件? [英] How to change the color of a selected row, even if it has a condition?

查看:88
本文介绍了如何更改所选行的颜色,即使它有条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,如果condition = true / accept,那么该行将为绿色。否则是怀特。如果我选择正常(白色,无条件)行并单击,颜色将变为蓝色。



但如果我点击红色行仍然是红色。无论怎样,我想要它,如果我选择它,它总是将颜色变为蓝色。



我尝试了什么: < br $>


I have a gridview and if the condition = true/accept, then the row will be green. Otherwise is White. If I select a normal (white, without condition) row and click on, the Color changes to blue.

But if I click on a red row it still be red. I want that, no matter what, that it always changes the Color to blue if i select it.

What I have tried:

<pre lang="c#">protected void RowDataBound(object sender, GridViewRowEventArgs e)
    {
            e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(sender as Control, "Select$" + e.Row.RowIndex);
           
              if (Data.Rows[e.Row.RowIndex]["accept"].ToString() == "3")
            {
                e.Row.CssClass += " green";
                
            }

             if (e.Row.RowState == DataControlRowState.Selected)
            {
                e.Row.CssClass += " blue";
            }

    }







protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex > -1)
        {
             foreach (GridViewRow row in ((GridView)sender).Rows)
            {
                if (row.RowIndex == GridView1.SelectedIndex)
                {
                    row.CssClass += " blue";
                }
                else
                {
                    row.CssClass = row.CssClass.Replace(" blue", string.Empty);
                }
            }
        }
    }

推荐答案

+ e.Row。 RowIndex);

if(Data.Rows [e.Row.RowIndex] [accept]。ToString()==3)
{
e.Row .CssClass + =green;

}

if(e.Row.RowState == DataControlRowState.Selected)
{
e.Row .CssClass + =blue;
}

}
" + e.Row.RowIndex); if (Data.Rows[e.Row.RowIndex]["accept"].ToString() == "3") { e.Row.CssClass += " green"; } if (e.Row.RowState == DataControlRowState.Selected) { e.Row.CssClass += " blue"; } }







protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex > -1)
        {
             foreach (GridViewRow row in ((GridView)sender).Rows)
            {
                if (row.RowIndex == GridView1.SelectedIndex)
                {
                    row.CssClass += " blue";
                }
                else
                {
                    row.CssClass = row.CssClass.Replace(" blue", string.Empty);
                }
            }
        }
    }


您可以使用以下内容:

You can use following:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (GridView1.SelectedIndex > -1)
        GridView1.SelectedRow.CssClass = "blue";
}


您可以使用以下内容:

You can use following:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (GridView1.SelectedIndex > -1)
        GridView1.SelectedRow.CssClass = "blue";
}






OR

protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowIndex == GridView1.SelectedIndex)
        {
            row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
        }
        else
        {
            row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
        }
    }
}


这篇关于如何更改所选行的颜色,即使它有条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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