In this article I am going to explain how to replace NULL or
Empty values with other/message text in Asp.net Gridview.
.
Description:
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.
Implementations:
HTML
Markup of Webform:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" NullDisplayText="--NA--">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="genre" HeaderText="Genre" NullDisplayText="--NA--">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField HeaderText="Genre (NA)">
<ItemTemplate>
<asp:Label ID="lblgenre" runat="server" Text='<%#Eval("genre") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:BoundField DataField="Budget" HeaderText="Budget" NullDisplayText="--NA--">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<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" />
</asp:GridView>
</form>
</body>
</html>
Add
namespace
C# Code:
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
VB.net 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)
{
BindGrid();
}
}
private void BindGrid()
{
SqlDataAdapter
adp = new SqlDataAdapter("SpGetMovie", con);
adp.SelectCommand.CommandType = CommandType.StoredProcedure;
DataTable
dt = new DataTable();
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.net 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
BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim adp As New SqlDataAdapter("SpGetMovie", con)
adp.SelectCommand.CommandType = CommandType.StoredProcedure
Dim dt As New DataTable()
adp.Fill(dt)
If dt.Rows.Count > 0 Then
GridView1.DataSource = dt
GridView1.DataBind()
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--";
}
}
}
VB.net 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:
No comments:
Post a Comment