Wednesday, July 19, 2017 Gridview : Replace NULL or Empty values with other/message text

In this article I am going to explain how to replace NULL or Empty values with other/message text in Gridview.

When we are displaying records in Gridview, some of fields have null value.  I want to show -–NA—on the instead of empty column. We have 2 options to achieve this. If we are using Boundfield in that case we can use NullDisplayText property.
2nd option to write the code on RowDatabound event of Gridview.

HTML Markup of Webform:

<html xmlns="">
<head runat="server">

    <form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onrowdatabound="GridView1_RowDataBound" CellPadding="4" ForeColor="#333333" GridLines="None">
          <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <asp:BoundField DataField="name" HeaderText="Name" NullDisplayText="--NA--">
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle VerticalAlign="Middle" />
        <asp:BoundField DataField="genre" HeaderText="Genre" NullDisplayText="--NA--">
            <HeaderStyle HorizontalAlign="Left" />
            <asp:TemplateField HeaderText="Genre (NA)">
                    <asp:Label ID="lblgenre" runat="server" Text='<%#Eval("genre") %>'></asp:Label>
                <HeaderStyle HorizontalAlign="Left" />
               <asp:BoundField DataField="Budget" HeaderText="Budget" NullDisplayText="--NA--">
            <HeaderStyle HorizontalAlign="Left" />
          <EditRowStyle BackColor="#999999" />
          <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
          <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
          <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
          <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
          <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
          <SortedAscendingCellStyle BackColor="#E9E7E2" />
          <SortedAscendingHeaderStyle BackColor="#506C8C" />
          <SortedDescendingCellStyle BackColor="#FFFDF8" />
          <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

Add namespace

C# Code:
using System.Configuration;
using System.Data.SqlClient;
using System.Data; Code:
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data

Bind Gridview
Create a method to bind gridview and call it on page load event.

C# Code:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
    private void BindGrid()
        SqlDataAdapter adp = new SqlDataAdapter("SpGetMovie", con);
        adp.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataTable dt = new DataTable();
        if (dt.Rows.Count > 0)
            GridView1.DataSource = dt;
    } Code:
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If
    End Sub
    Private Sub BindGrid()
        Dim adp As New SqlDataAdapter("SpGetMovie", con)
        adp.SelectCommand.CommandType = CommandType.StoredProcedure
        Dim dt As New DataTable()
        If dt.Rows.Count > 0 Then
            GridView1.DataSource = dt
        End If
    End Sub

Replace null/empty field with other/message text
On RowdataBound write the below given code:

C# Code:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            Label lblNAgenre = (Label)e.Row.Cells[0].FindControl("lblgenre");
            if (lblNAgenre.Text == "")
                lblNAgenre.Text = "--NA--";
    } Code:

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 lblNAgenre As Label = DirectCast(e.Row.Cells(0).FindControl("lblgenre"), Label)
            If lblNAgenre.Text = "" Then
                lblNAgenre.Text = "--NA--"
            End If
        End If
    End Sub

Result: Gridview : Replace NULL or Empty values with other/message text

No comments:

Post a Comment