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. How to highlight today's date row in datagrid when page loaded?

How to highlight today's date row in datagrid when page loaded?

Scheduled Pinned Locked Moved ASP.NET
tutorialquestion
13 Posts 3 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.
  • D Offline
    D Offline
    Dhruvil
    wrote on last edited by
    #1

    Hi, I have a datagrid, in which i want to highlight today's date row when that page is loaded. any body have any idea? thanks, NIKI

    S G 2 Replies Last reply
    0
    • D Dhruvil

      Hi, I have a datagrid, in which i want to highlight today's date row when that page is loaded. any body have any idea? thanks, NIKI

      S Offline
      S Offline
      Sushant Duggal
      wrote on last edited by
      #2

      HI, At the time of databoud(in event ItemDataBound), Check the date field with current date, if matches, change the backcolor of the row. Its really easy Thanks, Sushant Duggal.

      D 1 Reply Last reply
      0
      • S Sushant Duggal

        HI, At the time of databoud(in event ItemDataBound), Check the date field with current date, if matches, change the backcolor of the row. Its really easy Thanks, Sushant Duggal.

        D Offline
        D Offline
        Dhruvil
        wrote on last edited by
        #3

        I tried that. but i think my syntax is not good. If you can guide me.And also if the current date is not in the list, it should highlight most nearest future date in the datagrid. Thanks, NIKI

        S 1 Reply Last reply
        0
        • D Dhruvil

          I tried that. but i think my syntax is not good. If you can guide me.And also if the current date is not in the list, it should highlight most nearest future date in the datagrid. Thanks, NIKI

          S Offline
          S Offline
          Sushant Duggal
          wrote on last edited by
          #4

          HI niki, Send me the code here, i'll try to find the error. If you have not written the code, then i'll give u the code. Thanks Sushant Duggal.

          D 1 Reply Last reply
          0
          • S Sushant Duggal

            HI niki, Send me the code here, i'll try to find the error. If you have not written the code, then i'll give u the code. Thanks Sushant Duggal.

            D Offline
            D Offline
            Dhruvil
            wrote on last edited by
            #5

            Hi Sushant, The code for comparing the today's date is not here, but other thing which i implemented is here. public void dg_ItemDataBound(object sender, DataGridItemEventArgs e) { if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem) { e.Item.Attributes.Add("onmouseover","this.style.cursor='hand'; this.style.backgroundColor='pink'; "); e.Item.Attributes.Add("onclick","javascript:ShowDetail('"+DataBinder.Eval(e.Item.DataItem,"EVENT_DT")+"');"); ShowDetail is my js function, but it nothing to do with this issue. the full issue is like : I need to highlight today's date if it's available in the datagrid, or nearest future date from today. If you can send some code, that's really helpful for me. I really appreciate your help. Thank you very much Sushant. NIKI

            S 1 Reply Last reply
            0
            • D Dhruvil

              Hi Sushant, The code for comparing the today's date is not here, but other thing which i implemented is here. public void dg_ItemDataBound(object sender, DataGridItemEventArgs e) { if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem) { e.Item.Attributes.Add("onmouseover","this.style.cursor='hand'; this.style.backgroundColor='pink'; "); e.Item.Attributes.Add("onclick","javascript:ShowDetail('"+DataBinder.Eval(e.Item.DataItem,"EVENT_DT")+"');"); ShowDetail is my js function, but it nothing to do with this issue. the full issue is like : I need to highlight today's date if it's available in the datagrid, or nearest future date from today. If you can send some code, that's really helpful for me. I really appreciate your help. Thank you very much Sushant. NIKI

              S Offline
              S Offline
              Sushant Duggal
              wrote on last edited by
              #6

              HI NIKI, This is what i think u need... Please try this code : private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DateTime dt = Convert.ToDateTime(e.Item.Cells[5].Text);//PUT THE SELL NUMBER WHERE IS THE DATE FIELD if(DateTime.Compare(DateTime.Today, dt) == 0) // this is the current date e.Item.BackColor = Color.Aqua; else if(DateTime.Compare(DateTime.Today, dt) == -1) // this is the future coming dates e.Item.BackColor = Color.Yellow; } } I hope this helps you:) THanks Sushant Duggal.

              D 1 Reply Last reply
              0
              • S Sushant Duggal

                HI NIKI, This is what i think u need... Please try this code : private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DateTime dt = Convert.ToDateTime(e.Item.Cells[5].Text);//PUT THE SELL NUMBER WHERE IS THE DATE FIELD if(DateTime.Compare(DateTime.Today, dt) == 0) // this is the current date e.Item.BackColor = Color.Aqua; else if(DateTime.Compare(DateTime.Today, dt) == -1) // this is the future coming dates e.Item.BackColor = Color.Yellow; } } I hope this helps you:) THanks Sushant Duggal.

                D Offline
                D Offline
                Dhruvil
                wrote on last edited by
                #7

                Hi Sushant, Thank you very much for help. along with your code, i am adding this two lines. if i dont have today's date or future dates in my datagrid it will highlight the latest past date in datagrid. else if(DateTime.Compare(DateTime.Today, dt) == +1) e.Item.BackColor = Color.red; But, this makes backcolor red for all past dates rows instead of just a single latest past date.(I have at present, no future dates or today's date) I want to highlight only latest past date in datagrid. Any help??? that's really urgent. and, adition to that, it should pass the value for that row as default to the client side javascript function when page load (this should be same for future date, today's date or latest past date).i am trying with 'onload' function, but it's not working. Presently i m passing values on 'onclick' event, as below: e.Item.Attributes.Add("onclick","ShowDetail('"+ DataBinder.Eval(e.Item.DataItem,"TOPIC_DATE")+"');"); any thing you can suggest, it will be very much helpful for me. Thanks you very much again. NIKI

                S 1 Reply Last reply
                0
                • D Dhruvil

                  Hi Sushant, Thank you very much for help. along with your code, i am adding this two lines. if i dont have today's date or future dates in my datagrid it will highlight the latest past date in datagrid. else if(DateTime.Compare(DateTime.Today, dt) == +1) e.Item.BackColor = Color.red; But, this makes backcolor red for all past dates rows instead of just a single latest past date.(I have at present, no future dates or today's date) I want to highlight only latest past date in datagrid. Any help??? that's really urgent. and, adition to that, it should pass the value for that row as default to the client side javascript function when page load (this should be same for future date, today's date or latest past date).i am trying with 'onload' function, but it's not working. Presently i m passing values on 'onclick' event, as below: e.Item.Attributes.Add("onclick","ShowDetail('"+ DataBinder.Eval(e.Item.DataItem,"TOPIC_DATE")+"');"); any thing you can suggest, it will be very much helpful for me. Thanks you very much again. NIKI

                  S Offline
                  S Offline
                  Sushant Duggal
                  wrote on last edited by
                  #8

                  HI Niki, if you want to do that, then you need to add all the rows information of the datagrid in an data table. do one thing, create a datatable(define the datatable globally) , take two columns , one for date and one for row index. do this in ItemDatabound event. then where you are binding the grid, after datagrid.DataBind(), compare and change the color. I hope you got my idea , and this idea works:) Thanks Sushant Duggal.

                  D 1 Reply Last reply
                  0
                  • S Sushant Duggal

                    HI Niki, if you want to do that, then you need to add all the rows information of the datagrid in an data table. do one thing, create a datatable(define the datatable globally) , take two columns , one for date and one for row index. do this in ItemDatabound event. then where you are binding the grid, after datagrid.DataBind(), compare and change the color. I hope you got my idea , and this idea works:) Thanks Sushant Duggal.

                    D Offline
                    D Offline
                    Dhruvil
                    wrote on last edited by
                    #9

                    Hi Sushant, This should not work?? if(DateTime.Compare(DateTime.Today, dt) == +1) { e.Item.Attributes.Add("onload","this.style.backgroundColor='red';"); I don't know, i m not sure. Thanks,

                    S 1 Reply Last reply
                    0
                    • D Dhruvil

                      Hi Sushant, This should not work?? if(DateTime.Compare(DateTime.Today, dt) == +1) { e.Item.Attributes.Add("onload","this.style.backgroundColor='red';"); I don't know, i m not sure. Thanks,

                      S Offline
                      S Offline
                      Sushant Duggal
                      wrote on last edited by
                      #10

                      hi niki, where r u checking that it is the latest one?????? This will color all the rows. Sushant Duggal.

                      D 1 Reply Last reply
                      0
                      • S Sushant Duggal

                        hi niki, where r u checking that it is the latest one?????? This will color all the rows. Sushant Duggal.

                        D Offline
                        D Offline
                        Dhruvil
                        wrote on last edited by
                        #11

                        Hi Sushant, I have all the past values at present. so, i want to highlight only the latest past date row. but instead, it's highlighting all past values. I am checking in the item_databound property. Thanks, NIKI

                        S 1 Reply Last reply
                        0
                        • D Dhruvil

                          Hi, I have a datagrid, in which i want to highlight today's date row when that page is loaded. any body have any idea? thanks, NIKI

                          G Offline
                          G Offline
                          Guffa
                          wrote on last edited by
                          #12

                          No. I don't use datagrids. --- b { font-weight: normal; }

                          1 Reply Last reply
                          0
                          • D Dhruvil

                            Hi Sushant, I have all the past values at present. so, i want to highlight only the latest past date row. but instead, it's highlighting all past values. I am checking in the item_databound property. Thanks, NIKI

                            S Offline
                            S Offline
                            Sushant Duggal
                            wrote on last edited by
                            #13

                            HI Niki, what I wrote in the last thread was to change backcolor of one row only. I got a new and easy Idea. DO one thing, on the page load event, get the max date from the database, then in ItemDataBound compare that date with all the rows, which ever matches, change the backcolor of that row. I think this is really easy:) Thanks Sushant Duggal.

                            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