Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. change forecolor of a label in gridview [modified]

change forecolor of a label in gridview [modified]

Scheduled Pinned Locked Moved ASP.NET
sysadmin
20 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sandeep Mewara

    lets compromise on: For Gridview its RowDataBound and for DataGrid its ItemDataBound! :)

    A Offline
    A Offline
    Abhijit Jana
    wrote on last edited by
    #8

    I don't know if he has made some changes in his original post or not. It may be because Brij asked him to use ItemDataBound :laugh:

    Cheers ! Abhijit Codeproject MVP

    L 1 Reply Last reply
    0
    • A Abhijit Jana

      I don't know if he has made some changes in his original post or not. It may be because Brij asked him to use ItemDataBound :laugh:

      Cheers ! Abhijit Codeproject MVP

      L Offline
      L Offline
      laziale
      wrote on last edited by
      #9

      I just removed the and tags because of some formatting issue of the site :).

      1 Reply Last reply
      0
      • A Abhijit Jana

        Try with this : You need to do this in RowDataBound Event of GridView.

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

          Label lblFirstName= (Label)e.Item.FindControl("lblFirstName");
           if(lblFirstName.Text == "MyText")
                 lblFirstName.ForColor=mycolor;
        
        }
        

        }

        Cheers ! Abhijit Codeproject MVP

        L Offline
        L Offline
        laziale
        wrote on last edited by
        #10

        Thanks for all your help. I have one question, since I am trying to figure out the color of the label in the page_load event, is there anyway how I can assign the color depending from the text in that label? Thx, Laziale

        A 1 Reply Last reply
        0
        • L laziale

          Thanks for all your help. I have one question, since I am trying to figure out the color of the label in the page_load event, is there anyway how I can assign the color depending from the text in that label? Thx, Laziale

          A Offline
          A Offline
          Abhijit Jana
          wrote on last edited by
          #11

          laziale wrote:

          since I am trying to figure out the color of the label in the page_load event, is there anyway how I can assign the color depending from the text in that label?

          Why you are trying out in Page_Laod. If you want to check for each and every row of gridview, you need to check it in RowDataBound Event only.

          Cheers ! Abhijit Codeproject MVP

          L 1 Reply Last reply
          0
          • A Abhijit Jana

            laziale wrote:

            since I am trying to figure out the color of the label in the page_load event, is there anyway how I can assign the color depending from the text in that label?

            Why you are trying out in Page_Laod. If you want to check for each and every row of gridview, you need to check it in RowDataBound Event only.

            Cheers ! Abhijit Codeproject MVP

            L Offline
            L Offline
            laziale
            wrote on last edited by
            #12

            cause I want to display the proper color before the site is rendered on the screen. Is there a way how I can trigger that event in the Page_Load? Thanks, Laziale

            F B 2 Replies Last reply
            0
            • L laziale

              cause I want to display the proper color before the site is rendered on the screen. Is there a way how I can trigger that event in the Page_Load? Thanks, Laziale

              F Offline
              F Offline
              FyreWyrm
              wrote on last edited by
              #13

              Do you understand the page life cycle? Read about it here[^]. In your page load event you want to set the DataSource property of your grid and call DataBind. Hopefully in your init method you setup an item data bound event handler. When DataBind is called, for each row being added to the gridview your data bound event handler will be called. There you set your colors. After all of that is done, Render gets called for the page which generates all the HTML from your page and code which gets sent to your user's browser.

              Don't blame me. I voted for Chuck Norris.

              1 Reply Last reply
              0
              • L laziale

                Hello - I want to change the forecolor of a label in a gridview I have, depending on the text in that label. I can't access the label by the ID in the code-behind to get the properties. The label is part of templatefield: <div> <asp:Label ID="lblFirstName" runat="server" Text='<%#Server.HtmlEncode(Eval("FirstName").ToString())%>'></asp:Label> </div> Thx, Laziale

                modified on Monday, March 15, 2010 3:54 PM

                C Offline
                C Offline
                carlecomm
                wrote on last edited by
                #14

                hi, Here is an example: gridview_OnDataBound(){ Label label1 = e.Item.FindControl("lable1") as Label; Label label2 = e.Item.FindControl("lable2") as Label; if(lable1.Text == "1") { label2.ForeColor = "red"; }else if(lable1.Text == "2"){ label2.ForeColor = "blue"; } } Hope it helps:)

                L 1 Reply Last reply
                0
                • S Sandeep Mewara

                  lets compromise on: For Gridview its RowDataBound and for DataGrid its ItemDataBound! :)

                  B Offline
                  B Offline
                  Brij
                  wrote on last edited by
                  #15

                  There are many data controls in asp.net and and have the different event names .for the same cause Was just a typo :)

                  Cheers!! Brij

                  1 Reply Last reply
                  0
                  • L laziale

                    cause I want to display the proper color before the site is rendered on the screen. Is there a way how I can trigger that event in the Page_Load? Thanks, Laziale

                    B Offline
                    B Offline
                    Brij
                    wrote on last edited by
                    #16

                    For changing the color,just change the color in rowdatabound only.It'll suffice your purpose. After pageload,you must have assigning some datasource.then, your rowdatabound event will be called for each row.In this event,you can modify the color based on the text. After it page will render and your changes will refelct on the screen. So you dont need to do this at pageload.

                    Cheers!! Brij

                    1 Reply Last reply
                    0
                    • A Abhijit Jana

                      Try with this : You need to do this in RowDataBound Event of GridView.

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

                        Label lblFirstName= (Label)e.Item.FindControl("lblFirstName");
                         if(lblFirstName.Text == "MyText")
                               lblFirstName.ForColor=mycolor;
                      
                      }
                      

                      }

                      Cheers ! Abhijit Codeproject MVP

                      L Offline
                      L Offline
                      laziale
                      wrote on last edited by
                      #17

                      nothing is happening with that one, I have the event set but its not triggered at all. The page loads without triggering that event. But thanks for your help anyway. Laziale

                      A 1 Reply Last reply
                      0
                      • C carlecomm

                        hi, Here is an example: gridview_OnDataBound(){ Label label1 = e.Item.FindControl("lable1") as Label; Label label2 = e.Item.FindControl("lable2") as Label; if(lable1.Text == "1") { label2.ForeColor = "red"; }else if(lable1.Text == "2"){ label2.ForeColor = "blue"; } } Hope it helps:)

                        L Offline
                        L Offline
                        laziale
                        wrote on last edited by
                        #18

                        I can't get to the 'e' argument since the databound event is using EventArgs. :( Thanks for your help though

                        1 Reply Last reply
                        0
                        • L laziale

                          nothing is happening with that one, I have the event set but its not triggered at all. The page loads without triggering that event. But thanks for your help anyway. Laziale

                          A Offline
                          A Offline
                          Abhijit Jana
                          wrote on last edited by
                          #19

                          laziale wrote:

                          nothing is happening with that one, I have the event set but its not triggered at all.

                          Did you specified the event with gridview in aspx page ?

                          Cheers ! Abhijit Codeproject MVP

                          L 1 Reply Last reply
                          0
                          • A Abhijit Jana

                            laziale wrote:

                            nothing is happening with that one, I have the event set but its not triggered at all.

                            Did you specified the event with gridview in aspx page ?

                            Cheers ! Abhijit Codeproject MVP

                            L Offline
                            L Offline
                            laziale
                            wrote on last edited by
                            #20

                            ok, there were two events for the same thing, that why it wasn't triggered. Now its triggered, but for some reason when I am stepping in to the code the label property doesn't get the text. Its left empty. Here is the code:

                             void gvOrderProductVariants\_RowDataBound(object sender, GridViewRowEventArgs e)
                                {
                                    if (e.Row.RowType == DataControlRowType.DataRow)
                                    {
                                        Label lblFirstName= (Label)e.Row.FindControl("lblFirstName");
                                        if (lblFirstame.Text.Contains("John"))
                                            lblFirstName.ForeColor = System.Drawing.Color.Red;
                                    }
                            
                                 
                                }
                            

                            Please tell me if I am doing something wrong. Thx, Laziale

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups