none
Gridview cell not being conditionally formatted RRS feed

  • Question

  • I am trying to set the font colour for a particular column if the cell text == "High"
    I thought the code I had (below) would do the trick but nothing is happening.
    The column in question is the 5th column along. I have checked the underlying data and there are no leading/trailing spaces in the data. The column cell font colour is always black

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.Cells[4].Text == "High")
                    {
                        e.Row.Cells[4].ForeColor = Color.Red;
                    }
    
                }
            }

    • Moved by CoolDadTx Wednesday, January 8, 2020 3:01 PM ASP.NET related
    Tuesday, January 7, 2020 10:37 PM

Answers

All replies

  • Hello,

    Use the following forum https://forums.asp.net/24.aspx/1?Web+Forms+Data+Controls and check out a similar post https://forums.asp.net/t/2004246.aspx?Conditional+formatting+of+cells+in+a+gridview


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by QPR_JAY Thursday, January 9, 2020 2:08 AM
    Tuesday, January 7, 2020 11:13 PM
  • You need to do this in the CellFormatting event handler (and not in the RowDataBound event handler):
    protected void GridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.ColumnIndex == 4 && e.Value = "High")
            e.CellStyle.ForeColor = Color.Red;
    }


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Tuesday, January 7, 2020 11:34 PM
  • This gives me the error: Operator && cannot be applied to operands of type 'bool' and 'object'
    Wednesday, January 8, 2020 1:52 AM
  • If I change the code to this I get no errors
    
     if (e.ColumnIndex == 4 && e.Value.ToString() == "High")
            e.CellStyle.ForeColor = Color.Red;
    }
    However nothing is red. If I change it to != "High" then it is the same, nothing is red. I added a breakpoint but the code is not even being hit. Do I need to add an event handler to the gridview tag itself in ordr to point it to the method?
    Wednesday, January 8, 2020 2:36 AM
  • The = "High" was a typo, it should have been == "High", sorry.

    And, yes, you need to add the event handler … sorry, I thought you would have known to do that.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, January 8, 2020 5:44 AM
  • Do you know which event handler to use?
    I tried/guessed OnCellFormatting but still nothing happens

    

    Wednesday, January 8, 2020 6:57 PM
  • It should be CellFormatting. I don't know how you are doing that with OnCellFormatting, which makes me wonder: which UI you are using? I am talking about WinForms, but event handlers are set up differently in WPF and ASP.NET (which, off the top of my head, I don't remember).

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, January 9, 2020 1:18 AM
  • Visual Studio 2019. ASP.NET Web App.
    Code behind method is GridView1_CellFormatting
    My asp:GridView tag needs an event handler to wire it up to the method.I have tried OnCellFormatting="GridView1_CellFormatting" but this does nothing and I'm not even sure it is a valid event handler
    Thursday, January 9, 2020 1:22 AM
  • Visual Studio 2019. ASP.NET Web App.
    Code behind method is GridView1_CellFormatting
    My asp:GridView tag needs an event handler to wire it up to the method.I have tried OnCellFormatting="GridView1_CellFormatting" but this does nothing and I'm not even sure it is a valid event handler
    This is why I pointed you to the links I provided in my first reply :-)

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, January 9, 2020 1:38 AM
  • I checked that and the first answer posted doesn't work and the accepted answer was an ugly mix of inline server tags in the markup which I hoped to avoid.
    However if that is the only way to set a font colour on the fly then so be it. The gridview doesn't appear to have a suitable event handler to handle this functionality that I can see.

    And the accepted solution in the link is in VB.NET

    • Edited by QPR_JAY Thursday, January 9, 2020 1:43 AM
    Thursday, January 9, 2020 1:43 AM
  • That said, I have got it to work by hacking around the example given and making it C# friendly.
    I converted the bound column to a template item, created a simple server side method that returned system.drawing.color and then added this to the item templates label:

    ForeColor='<%# setColour(Eval("Default_Priority").ToString()) %>'

    If the value of Default_Priority == "High" then the function returns system.drawing.color.red else black

    Thursday, January 9, 2020 1:59 AM