Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

C# question getting data from a GridviewRowDataBound

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
WebBuilder451 - 31 Aug 2007 15:58 GMT
This is what i'm using and it seems way too complicated!! for finding a data
item.
I hope some one has a better way?
private void fn_setRankingColor(GridViewRow gvr)
   {
       Label lbl = gvr.FindControl("lbl_RANKINGTODAY") as Label;
       if ((Int32)((DataRowView)gvr.DataItem).Row["RN"] ==
(Int32)((DataRowView)gvr.DataItem).Row["RNP"])
       {
       }
       else if ((Int32)((DataRowView)gvr.DataItem).Row["RN"] <
(Int32)((DataRowView)gvr.DataItem).Row["RNP"])
       {
           lbl.ForeColor = Color.DarkGreen;
           lbl.Font.Bold = true;
       }
       else
       {
           lbl.ForeColor = Color.DarkRed;
           lbl.Font.Bold = true;
       }
  }

Signature

(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes

Ged - 31 Aug 2007 16:26 GMT
I would extract the data values into local variables once, and then use the
local variables in the if...else statements.
Looks neater and will give you better performance (because it doesn't have
to retrieve the value for each statement and convert it to an int)

(Note: I've not checked this in VS2005, so I can't guarantee it will compile
and run)

private void fn_setRankingColor(GridViewRow gvr)
{
   Label lbl = gvr.FindControl("lbl_RANKINGTODAY") as Label;
   int rn = Convert.ToInt32(((DataRowView)gvr.DataItem).Row["RN"]));
   int rnp = Convert.ToInt32((((DataRowView)gvr.DataItem).Row["RNP"]));

   if( rn == rnp )
   {
       // Do something
   }
   else if(rn < rnp )
   {
       // Do something
   }
   else
   {
       // Do something
   }
}

HTH

Ged

> This is what i'm using and it seems way too complicated!! for finding a
> data
[quoted text clipped - 19 lines]
>        }
>   }
WebBuilder451 - 31 Aug 2007 16:44 GMT
ok, did that actually. Thank You. I have to admit i'd perfer not to but i'll
be using the values more than once in the report.

!
Signature

(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes

> I would extract the data values into local variables once, and then use the
> local variables in the if...else statements.
[quoted text clipped - 51 lines]
> >        }
> >   }
Ben Voigt [C++ MVP] - 31 Aug 2007 20:44 GMT
>I would extract the data values into local variables once, and then use the
>local variables in the if...else statements.
[quoted text clipped - 9 lines]
>    int rn = Convert.ToInt32(((DataRowView)gvr.DataItem).Row["RN"]));
>    int rnp = Convert.ToInt32((((DataRowView)gvr.DataItem).Row["RNP"]));

Even better:

DataRow row = ((DataRowView)gvr.DataItem).Row;
int rn = (int)row["RN"];
int rnp = (int)row["RNP"];
if (lbl.Font.Bold = (rn != rnp)) {
   lbl.Font.ForeColor = (rn < rnp)? Color.DarkGreen: Color.DarkRed;
}

assuming that the values are boxed integers (they were in the original code)
and don't need the overhead of the Convert.ToInt32 call.

But do you really need all of this?
Label lbl = gvr.FindControl("lbl_RANKINGTODAY") as Label;

Usually FindControl is only used when you don't know the exact control you
want, I suspect in this case you have the Label already stored in a member
variable.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.