Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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";
            }

    }


C#
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);
                }
            }
        }
    }
Posted
Updated 16-Jan-17 23:21pm
v2
Comments
Afzaal Ahmad Zeeshan 16-Jan-17 7:20am    
Then before changing the color to Blue, check if the row has color red. If the color is red; return.
Richard Deeming 16-Jan-17 9:51am    
When is the row set to red? You've mentioned white and green, with blue for the selected row, but there's no mention of red...

This is most likely a specificity issue. Try moving the .blue CSS rule below the other colour rules in your CSS file.

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

OR
C#
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");
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900