OnRowDataBound question

G

Guest

Hello,
I have run into an issue when using the OnRowDataBound event to apply
conditional formatting to a row. I need to check a specific column to see if
the value from the database is null. Without conditional formatting the page
just shows empty text in the table when the database column is null. I
figured the text value would be equivalent to "" but that doesn't work. Here
is a code snippet:

Protected Sub Grid_Databound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(10).Text = "" Then e.Row.Cells(10).Text = "blank"
End If
End Sub

This does not work - the text on the page always displays empty, it never
shows "blank". Am I missing something obvious? Is there a way to do this
using code similar to what I have above, or do I need to use a template
column along with a label control?

Thanks,
John
 
E

Eliyahu Goldin

John,

Why don't you set a breakpoint and check the value of the Text property?

Likely, an empty cell is rendered with a non-breaking space character. You
can either check on it, or try to trim it.
 
G

Guest

Thanks for the suggestion. I had actually tried something similar, and was
confused because I was seeing a whitespace as the value. But your suggestions
gave me an idea - I checked for a text value of in my code and it worked:

If e.Row.Cells(10).Text = " " Then e.Row.Cells(10).Text = "blank"

So I guess that the text value is of the row is the value that gets passed
into the HTML code between the <td> tags, and isn't representative of the
actual null value in the dataset cell.

Am I on the right track?
Thanks,
John

Eliyahu Goldin said:
John,

Why don't you set a breakpoint and check the value of the Text property?

Likely, an empty cell is rendered with a non-breaking space character. You
can either check on it, or try to trim it.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Aconquija said:
Hello,
I have run into an issue when using the OnRowDataBound event to apply
conditional formatting to a row. I need to check a specific column to see
if
the value from the database is null. Without conditional formatting the
page
just shows empty text in the table when the database column is null. I
figured the text value would be equivalent to "" but that doesn't work.
Here
is a code snippet:

Protected Sub Grid_Databound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(10).Text = "" Then e.Row.Cells(10).Text =
"blank"
End If
End Sub

This does not work - the text on the page always displays empty, it never
shows "blank". Am I missing something obvious? Is there a way to do this
using code similar to what I have above, or do I need to use a template
column along with a label control?

Thanks,
John
 
Joined
May 27, 2010
Messages
1
Reaction score
0
you can use the HTML Name as follows:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

If e.Row.Cells(10).Text = "&nbsp;" Then
e.Row.Cells(10).Text = "blank"
End If

End If

End Sub
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top