Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 7th, 2007, 10:49 AM
Authorized User
 
Join Date: Jul 2006
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;
            }
        }
    }
 
Old June 7th, 2007, 10:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
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
================================================== =========
 
Old June 8th, 2007, 03:30 AM
Authorized User
 
Join Date: Jul 2006
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).

 
Old June 8th, 2007, 07:17 AM
Friend of Wrox
 
Join Date: Aug 2004
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.
 
Old June 8th, 2007, 08:49 AM
Authorized User
 
Join Date: Jul 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old June 11th, 2007, 03:54 AM
Friend of Wrox
 
Join Date: Aug 2004
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.
 
Old June 14th, 2007, 03:20 AM
Authorized User
 
Join Date: Jul 2006
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






Similar Threads
Thread Thread Starter Forum Replies Last Post
Editing all GridView rows Abbas ASP.NET 2.0 Professional 4 March 3rd, 2012 02: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 03:52 PM
Insert row between specific rows Paula222 Access VBA 2 February 10th, 2006 03:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.