How to Replace Null Value in Asp.Net GridView Template Field or Template Column in C# VB.Net

Null means nothing or meaningless. To make it meaningful we need to replace null value by an understandable word or a line. In C# and VB.Net we do it easily. But in GridView it’s a bit different. We can replace null value easily in GridView without calling any server side method or even replacing it in database query time. Here in this example I will show you “How we can replace NULL value in GridView TemplateField“.

Screenshot of an Output:
How to Replace Null Value in Asp.Net GridView Template Field or Template Column in C# VB.Net

Replace Null Value in Asp.Net GridView TemplateField:
C# Code:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
    DataKeyNames="ID" AutoGenerateSelectButton="True"  
    HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" 
    HeaderStyle-ForeColor="White" CellPadding="5">
<Columns>
    <asp:BoundField DataField="Name" HeaderText="Name">
    </asp:BoundField>
     <asp:TemplateField>
     
     <ItemTemplate>
     <asp:Label ID="Label1" runat="server" Text='<%# Eval("Address")== DBNull.Value ? "Not Found" : Eval("Address") %>'></asp:Label>
     </ItemTemplate>
     </asp:TemplateField>
    
    <asp:BoundField DataField="Contact" HeaderText="Contact no">
    </asp:BoundField>
</Columns>
</asp:GridView>
</asp:Content>

VB.Net Code:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
    DataKeyNames="ID" AutoGenerateSelectButton="True"  
    HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" 
    HeaderStyle-ForeColor="White" CellPadding="5">
<Columns>
    <asp:BoundField DataField="Name" HeaderText="Name">
    </asp:BoundField>
     <asp:TemplateField>
     
     <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# If(IsDbNull(Eval("Address")), "Not Found", Eval("Address")) %>'></asp:Label>
     </ItemTemplate>
     </asp:TemplateField>
    
    <asp:BoundField DataField="Contact" HeaderText="Contact no">
    </asp:BoundField>
</Columns>
</asp:GridView>
</asp:Content>

Add a Namespace:
1. Syystem.Data

Bind GridView with some NULL Data:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtSupplier = new DataTable("Supplier");
            dtSupplier.Columns.Add(new DataColumn("ID"));
            dtSupplier.Columns.Add(new DataColumn("Name"));
            dtSupplier.Columns.Add(new DataColumn("Address"));
            dtSupplier.Columns.Add(new DataColumn("Contact"));
            dtSupplier.Rows.Add("st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx");
            dtSupplier.Rows.Add("ir0039", "Shadesh builders", null, "01711yyyyyy");
            dtSupplier.Rows.Add("cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz");
            dtSupplier.Rows.Add("er0078", "Windblow", null , "01711qqqqqq");
            dtSupplier.Rows.Add("bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo");

            GridView1.DataSource = dtSupplier;
            GridView1.DataBind();
        }
    }

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dtSupplier As New DataTable("Supplier")
            dtSupplier.Columns.Add(New DataColumn("ID"))
            dtSupplier.Columns.Add(New DataColumn("Name"))
            dtSupplier.Columns.Add(New DataColumn("Address"))
            dtSupplier.Columns.Add(New DataColumn("Contact"))
            dtSupplier.Rows.Add("st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx")
            dtSupplier.Rows.Add("ir0039", "Shadesh builders", Nothing, "01711yyyyyy")
            dtSupplier.Rows.Add("cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz")
            dtSupplier.Rows.Add("er0078", "Windblow", Nothing, "01711qqqqqq")
            dtSupplier.Rows.Add("bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo")


            GridView1.DataSource = dtSupplier
            GridView1.DataBind()

        End If
    End Sub

Now run the project. Hope your null value will be replaced by your desired word or line.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, Gridview, VB.Net
One comment on “How to Replace Null Value in Asp.Net GridView Template Field or Template Column in C# VB.Net
  1. Navjot Singh says:

    Thanks to you all

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*