Wednesday, March 28, 2012

string.format gridview cell

I am trying to use the following code to format specific cells in my gridview and failing. I can successfully format a column by setting htmlencode off and string.format for the field, but I need to control the formatting at the cellular level :-). I can get font, color etc, but not string.format. I can add to the string i.e. in the code below everything works, except the <code> "

Case"Sales"

For i = 1To e.Row.Cells.Count - 1

IfNot RTrim(e.Row.Cells(i).Text.ToString) =""Then

e.Row.Cells(i).Text =String.Format("{0:c}", e.Row.Cells(i).Text)

EndIf

Next

"</code>

Also if not rtrim(e.row.cells(i).text.tostring = "" does not do anything for me, what is the correct syntax so that empty cells do not recieving this formatting?

<code>

Sub gridview2_rowdatabound(ByVal senderAsObject,ByVal eAs GridViewRowEventArgs)Handles GridView2.RowDataBound

Dim iAsInteger

If e.Row.RowType = DataControlRowType.DataRowThen

e.Row.Cells(4).Font.Bold =True

e.Row.Cells(8).Font.Bold =True

e.Row.Cells(12).Font.Bold =True

e.Row.Cells(16).Font.Bold =True

e.Row.Cells(17).Font.Bold =True

e.Row.Cells(4).BackColor = Drawing.Color.LightGray

e.Row.Cells(8).BackColor = Drawing.Color.LightGray

e.Row.Cells(12).BackColor = Drawing.Color.LightGray

e.Row.Cells(16).BackColor = Drawing.Color.LightGray

e.Row.Cells(17).BackColor = Drawing.Color.LightGray

SelectCase RTrim(e.Row.Cells(0).Text)

Case"Sales"

For i = 1To e.Row.Cells.Count - 1

IfNot RTrim(e.Row.Cells(i).Text.ToString) =""Then

e.Row.Cells(i).Text =String.Format("{0:c}", e.Row.Cells(i).Text)

EndIf

Next

Case"Gross Profit"

For i = 1To e.Row.Cells.Count - 1

IfNot e.Row.Cells(i).TextIsNothingThen

e.Row.Cells(i).Text ="$" & e.Row.Cells(i).Text

EndIf

Next

EndSelect

EndIf

EndSub

</code>

Thanks

if you change your trouble code to this:

String str =String.Format("{0:c}", e.Row.Cells(i).Text);
e.Row.Cells(i).Text =String.Format("{0:c}", e.Row.Cells(i).Text)

and debugg it,

what result to you get for str?
Does it format it?
Does your code even go into the if statement above the trouble line?

foregive my ignorance, is there a way to see the value of a variable in vs.net during a debugging session besides assigning it to a label in my web app?

I do know that the code does go into the if statement as changing the code to me e.row.cells(i).text = "$" & e.row.cells(i).text gives me the expected result in the gridview cell.

Also the app compiles without error.

Thanks in advance for your assistance.


Ok, I placed a break immediately after the str1 = string.format...

and in my vs.net 2005 it shows the number with no formatting.

"Good news is I now see how to trouble shoot my code without adding extra labels etc on my application to display variables just so that I know what is being assigned to the variable" Bad news, I still don't see why string.format is not working.


Oops. Disregard this post, plz.


Try this:

IfNot RTrim(e.Row.Cells(i).Text.ToString) =""Then

e.Row.Cells(i).Text =String.Format("{0:c}", Double.Parse(e.Row.Cells(i).Text))

EndIf


that works perfectly. Thank you

0 comments:

Post a Comment