How to Replace Null Value in Asp.Net GridView BoundField Column in C# VB.Net

If the datasource of a GridView control contains NULL value then the GridView Cell render blank text. To make the information more meaningful we need to replace the NULL value with a meaningful word or a line. GridView BoundField column provides us a property named NullDisplayText, by which we can display any text instead of NULL.

Sample Output:
How to Replace Null Value in Asp.Net GridView BoundField Column in C# VB.Net

Replace Null Value in Asp.Net GridView BoundField Column:

<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">
    <asp:BoundField DataField="Name" HeaderText="Name">
    <asp:BoundField NullDisplayText="Not Found" DataField="Address" HeaderText="Address">
    <asp:BoundField DataField="Contact" HeaderText="Contact no">

Bind GridView with some NULL value:
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;

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

        End If
    End Sub

Namespace to Add:
1. System.Data

Hope the tip will help you.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

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

AlphaOmega Captcha Classica  –  Enter Security Code