Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Using ASP.NET, C#, SQL Server.

I am trying to display data into multiple gridviews from same database (from views).
While displaying data into gridviews, the empty data values didn't get populated in gridviews, instead it is affecting the adjoining gridviews, causing uneven heights of the gridviews. I wish to represent these empty data values by '0', so that the gridviews appear aligned properly and shows '0's where the data is not present. I stored the DB values in dataset before retrieving into gridviews.
Please help. Thanking you in advance.
Posted
Comments
CHill60 21-Jan-15 9:44am    
Try using ISNULL() or COALESCE() in the SQL query that is retrieving the data

I would suggest converting the column that has null values to a template field. add a label control and bind the label with visibility set to false. add another label that is visible.

example:
XML
<asp:templatefield headertext="Quantity" xmlns:asp="#unknown">
<itemtemplate>
<asp:label id="quantlabel" runat="server" text=""></asp:label>
<asp:label id="Label6" runat="server" text="<%# Bind("TickDItemQuantity") %>" visible="false"></asp:label>
</itemtemplate>
<itemstyle horizontalalign="Center" />
</asp:templatefield>



on gridview rowdatabound you can access the bound labels value

hence:

VB
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim quantlabel As Label = TryCast(e.Row.FindControl("quantlabel"), Label)
            Dim Label6 As Label = TryCast(e.Row.FindControl("Label6"), Label)


            If Label6.Text Is Nothing Or Label6.Text = "" Then
                quantlabel.text = "0"
            Else
                quantlabel.Text = Label6.Text
            End If

End Sub

this way if the value is NULL from the database then your label value will reflect "0" or whatever you choose to insert into it else it will retain the original value of the bound label.

hope this will help you.
 
Share this answer
 
Use BoundField.NullDisplayText Property[^]
Quote:
Sometimes a field's value is stored as null in the data source. You can specify a custom caption to display for fields that have a null value by setting the NullDisplayText property. If this property is not set, null field values are displayed as empty strings (""). When a record is being updated or inserted in a data-bound control, if the user enters the value specified by this property (other than an empty string) for a field in a data-bound control, that value is automatically converted to null in the data source.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900