如何在多个网格视图中显示空值 [英] How to show null values in multiple gridviews

查看:52
本文介绍了如何在多个网格视图中显示空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.NET,C#,SQL Server。



我试图将数据显示在同一数据库(来自视图)的多个网格视图中。

在将数据显示到网格视图中时,空数据值未在网格视图中填充,而是影响相邻的网格视图,从而导致网格视图的高度不均匀。我希望将这些空数据值表示为'0',以便网格视图显示正确对齐并显示数据不存在的'0'。在检索到网格视图之前,我将数据库值存储在数据集中。

请帮忙。提前感谢你。

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.

推荐答案

使用 BoundField.NullDisplayText Property [ ^ ]

Use BoundField.NullDisplayText Property[^]
Quote:

有时,字段的值在数据源中存储为null。您可以通过设置NullDisplayText属性指定要为具有空值的字段显示的自定义标题。如果未设置此属性,则空字段值将显示为空字符串()。在更新或插入数据绑定控件中的记录时,如果用户为数据绑定控件中的字段输入此属性指定的值(空字符串除外),则该值将自动转换为null数据源。

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.


我建​​议将具有空值的列转换为模板字段。添加标签控件并将标签与可见性设置为false绑定。添加另一个可见的标签。



例如:

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:
<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>







在gridview rowdatabound上你可以访问绑定的标签值



因此:






on gridview rowdatabound you can access the bound labels value

hence:

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



如果该值为NULL,则为b在数据库中,您的标签值将反映为0或您选择插入其中的任何内容,否则它将保留绑定标签的原始值。



希望这将是帮助你。


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.


这篇关于如何在多个网格视图中显示空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆