Display blank (empty) in place of Zero in ASP.Net GridView using C# and VB.Net

makumbi
 
on Jul 28, 2022 06:08 AM
675 Views

How can i avoid zero in GridView data.

noNameClassStreamA1A2A3A4A5A6A7A8A9A10

I want if the value does not contain any data to remain empty.

Please help.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
KasimA
 
on Jul 28, 2022 08:40 AM
on Jul 28, 2022 08:53 AM

Hi makumbi,

Inside the RowDataBound event you need to find TableCell using foreach loop and check condition TableCell has value "0" then set it to string.Empty.

Please refer below sample.

HTML

<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="No">
            <ItemTemplate>
                <asp:CheckBox ID="cbCheck" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Class" HeaderText="Class" />
        <asp:BoundField DataField="Stream" HeaderText="Stream" />
        <asp:BoundField DataField="A1" HeaderText="A1" />
        <asp:BoundField DataField="A2" HeaderText="A2" />
        <asp:BoundField DataField="A3" HeaderText="A3" />
        <asp:BoundField DataField="A4" HeaderText="A4" />
        <asp:BoundField DataField="A5" HeaderText="A5" />
        <asp:BoundField DataField="A6" HeaderText="A6" />
        <asp:BoundField DataField="A7" HeaderText="A7" />
        <asp:BoundField DataField="A8" HeaderText="A8" />
        <asp:BoundField DataField="A9" HeaderText="A9" />
        <asp:BoundField DataField="A10" HeaderText="A10" />
        <asp:BoundField DataField="A11" HeaderText="" />
    </Columns>
</asp:GridView>

Namespace

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[14]  {
                                            new DataColumn("Name",typeof(string)),
                                            new DataColumn("Class",typeof(string)),
                                            new DataColumn("Stream",typeof(string)),
                                            new DataColumn("A1",typeof(string)),
                                            new DataColumn("A2", typeof(int)),
                                            new DataColumn("A3",typeof(int)),
                                            new DataColumn("A4",typeof(int)),
                                            new DataColumn("A5",typeof(int)),
                                            new DataColumn("A6",typeof(int)),
                                            new DataColumn("A7", typeof(int)),
                                            new DataColumn("A8",typeof(int)),
                                            new DataColumn("A9",typeof(int)),
                                            new DataColumn("A10",typeof(int)),
                                            new DataColumn("A11",typeof(int)) });
        dt.Rows.Add("22-03119", "ABER ABIGAIL HOWARD", "P3", "RED", "33", "33", "22", "44", "22", "33", "44", "22", "0", "10");
        dt.Rows.Add("20-01954", "AGABA AZRIEL", "P3", "RED", "89", "33", "7", "0", "0", "0", "0", "0", "0", "10");
        dt.Rows.Add("20-01950", "AGABA HARRIS", "P3", "RED", "88", "0", "66", "0", "0", "0", "0", "0", "0", "10");
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
}

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        foreach (TableCell cell in e.Row.Cells)
        {
            if (cell.Text=="0")
            {
                cell.Text = string.Empty;
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(13) {New DataColumn("Name", GetType(String)), New DataColumn("Class", GetType(String)), 
            New DataColumn("Stream", GetType(String)), New DataColumn("A1", GetType(String)), New DataColumn("A2", GetType(Integer)), 
            New DataColumn("A3", GetType(Integer)), New DataColumn("A4", GetType(Integer)), New DataColumn("A5", GetType(Integer)), 
            New DataColumn("A6", GetType(Integer)), New DataColumn("A7", GetType(Integer)), New DataColumn("A8", GetType(Integer)), 
            New DataColumn("A9", GetType(Integer)), New DataColumn("A10", GetType(Integer)), New DataColumn("A11", GetType(Integer))})
        dt.Rows.Add("22-03119", "ABER ABIGAIL HOWARD", "P3", "RED", "33", "33", "22", "44", "22", "33", "44", "22", "0", "10")
        dt.Rows.Add("20-01954", "AGABA AZRIEL", "P3", "RED", "89", "33", "7", "0", "0", "0", "0", "0", "0", "10")
        dt.Rows.Add("20-01950", "AGABA HARRIS", "P3", "RED", "88", "0", "66", "0", "0", "0", "0", "0", "0", "10")
        gvDetails.DataSource = dt
        gvDetails.DataBind()
    End If
End Sub

Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then

        For Each cell As TableCell In e.Row.Cells

            If cell.Text = "0" Then
                cell.Text = String.Empty
            End If
        Next
    End If
End Sub

Screenshot