Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 7th, 2007, 10:49 AM
Authorized User
 
Join Date: Jul 2006
Location: , , United Kingdom.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Highlight specific rows in a gridview

Hi;
I am trying to highlight rows in a gridview when the data (date/time) Column1 is older than the data (date/time) in Column2. these collomns can be empty (null) therefore it get this errow message :"Object cannot be cast from DBNull to other types"

I would be grateful if anyone help me with that. this is my code

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row;
        if (row.DataItem == null)
        {
            return;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            DateTime date1 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "Column1"));
            DateTime date2 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "Column2"));

            if (DateTime.Compare(date2, date1) < 0)
            {
                e.Row.BackColor = System.Drawing.Color.Plum;
            }
        }
    }
Reply With Quote
  #2 (permalink)  
Old June 7th, 2007, 10:56 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,190
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

where do you get the error????.. unless I missing something this looks ok...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
Reply With Quote
  #3 (permalink)  
Old June 8th, 2007, 03:30 AM
Authorized User
 
Join Date: Jul 2006
Location: , , United Kingdom.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Gonzalo for your reply,
I get the error on these lines:
 DateTime date1 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "Column1"));
            DateTime date2 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "Column2"));

and i think because Column1 and Column2 contain empty cells(i.e. in the data source some time these columns should have empty cells and this cause the problem).

Reply With Quote
  #4 (permalink)  
Old June 8th, 2007, 07:17 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Well, you will get an error if you try to convert a blank value to datetime, optimize your code so that you convert to datetime only if the column has some value, avoid conversion in case of blank & null values.

Regards
Mike

Don't expect too much, too soon.
Reply With Quote
  #5 (permalink)  
Old June 8th, 2007, 08:49 AM
Authorized User
 
Join Date: Jul 2006
Location: , , United Kingdom.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Mike how can i do that, could you please give me some advice

Reply With Quote
  #6 (permalink)  
Old June 11th, 2007, 03:54 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Well you can do something like below:

if ((DataBinder.Eval(e.Row.DataItem, "Column1") != DBNull.Value) && (DataBinder.Eval(e.Row.DataItem, "Column1") != string.Empty))
                {
                    //DateTime date1 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "Column1"));
                }

Regards
Mike

Don't expect too much, too soon.
Reply With Quote
  #7 (permalink)  
Old June 14th, 2007, 03:20 AM
Authorized User
 
Join Date: Jul 2006
Location: , , United Kingdom.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Mike I did it in a different way but I think the way you have captured the blank values is much better therefore I have used your code.

Thanks very much.

Regards
rao

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Editing all GridView rows Abbas ASP.NET 2.0 Professional 4 March 3rd, 2012 01:19 PM
Getting a GridView Row to highlight gcm_uk ASP.NET 2.0 Basics 0 May 1st, 2007 05:55 AM
Deleting rows not equal to specific values Oprete Excel VBA 1 March 25th, 2007 11:14 PM
Editing all GridView rows Abbas C# 2005 0 November 29th, 2006 02:52 PM
Insert row between specific rows Paula222 Access VBA 2 February 10th, 2006 02:56 PM



All times are GMT -4. The time now is 02:37 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.