Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview Formating zeros invisible 1

Status
Not open for further replies.

stephenk1973

Technical User
Jun 11, 2003
246
GB
How do i format gridview cells so that '0's are invisible?

Any suggestions please , this is stumping me.

Thanks

Stephen

 
If I understand your question correctly, simply use the Replace method (either in the original SQL statement or in the RowDataBound event) to replace a zero with nothing.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Stephen - probably several ways to approach this - are you talking about a numeric field which, if equal to 0, you need to show a NULL? Or are you talking about a string field, in which there are zeros?

If it is a numeric field there might be a way to format it out; but my first guess would be to use a function, for example:

..in your code behind...
Code:
Protected Function getNo(intNo As Object) As String
 If intNo = 0 Then
   Return ""  ' might need to modify this..
 End If
End Function  

..and then in the Grid use a Template Column..

<asp:TemplateColumn HeaderText="MyInt">
 <HeaderStyle horizontalalign="Center" verticalalign="Middle"></HeaderStyle>
 <ItemStyle horizontalalign="Center"></ItemStyle>
   <ItemTemplate>
     <asp:Label id="intMyNo" runat="Server" Text=<%# getNo(Container.DataItem("intNo"))%>/>
  </ItemTemplate>
</asp:TemplateColumn>
..or some variation of the above. You can also try using an "IIF(this, that, or this)" type of a statement in your SQL replacing the zero at that time. Just a couple of thoughts.
 

Went down Ca8sms route, thanks for your help, here the code i ended up with.......

I have a grid of 12 cells displaying qunatities than need to be formatted

Protected Sub Gridview1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then
Dim i As Integer
For i = 1 To 12

Dim Q As Integer

Q = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Q" & i))
If Q = 0 Then
' ' color the background of the row yellow
e.Row.Cells(9 + (i - 1)).Text = ""
End If

Next
End If


End Sub

Thanks

Stepehn
 
Stephen, ca8sms - on a second reading it occurred to me that one of my pages I also had a particular Grid row set to a colored background on condition of whether an integer was greater than some value, and that page utilized the above scheme (slightly different code) and placed within the ItemDataBound handle, so I agree - that is a very clean way of accomplishing this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top