C# question getting data from a GridviewRowDataBound

G

Guest

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;
}
}

--
(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
 
G

Guest

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
 
G

Guest

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.

!
--
(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 said:
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


WebBuilder451 said:
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;
}
}

--
(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
 
B

Ben Voigt [C++ MVP]

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"]));

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top