hide gridview coumn if empty or zero

RAVI 1,056 Reputation points
2022-09-20T11:22:05.287+00:00

hello this is the code im using in my page

<style type="text/css">
.grid-sltrow {
background: #ddd;
font-weight: bold;
}

    .SubTotalRowStyle {  
        border: solid 1px Black;  
        background-color: #D8D8D8;  
        font-weight: bold;  
    }  

    .GrandTotalRowStyle {  
        border: solid 1px Gray;  
        background-color: #000000;  
        color: #ffffff;  
        font-weight: bold;  
    }  

    .GroupHeaderStyle {  
        border: solid 1px Black;  
        background-color: #4682B4;  
        color: #ffffff;  
        font-weight: bold;  
    }  

    .serh-grid {  
        width: 85%;  
        border: 1px solid #6AB5FF;  
        background: #fff;  
        line-height: 14px;  
        font-size: 11px;  
        font-family: Verdana;  
    }  
</style>  

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"  
            OnRowDataBound="GridView1_RowDataBound"  
            OnRowCreated="GridView1_RowCreated"  
            EnableModelValidation="True">  
            <Columns>  
                <asp:BoundField DataField="ItemName" HeaderText="ItemName" SortExpression="ItemName" />  
                <asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" />  
                <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />  
            </Columns>  
        </asp:GridView>  

Code behind:

    public string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        if (!IsPostBack)  
        {  
            BindGridView();  
        }  
    }  

    protected void BindGridView()  
    {  
        DataTable dt = new DataTable();  
        using (SqlConnection con = new SqlConnection(constr))  
        {  
            string cmdtext = "select ID, CustomerName, ItemName, Price, Qty from TestTable2";  
            using (SqlCommand cmd = new SqlCommand(cmdtext, con))  
            {  
                con.Open();  
                SqlDataAdapter sda = new SqlDataAdapter(cmd);  
                sda.Fill(dt);  
                GridView1.DataSource = dt;  
                GridView1.DataBind();  
            }  
        }  
    }  

    // To keep track of the previous row Group Identifier      
    string strPreviousRowName = string.Empty;  
    // To keep track the Index of Group Total      
    int intSubTotalIndex = 1;  
    // To temporarily store Sub Total      
    double dblSubTotalUnitPrice = 0;  
    double dblSubTotalQuantity = 0;  
    // To temporarily store Grand Total      
    double dblGrandTotalUnitPrice = 0;  
    double dblGrandTotalQuantity = 0;  
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)  
    {  
        bool IsSubTotalRowNeedToAdd = false;  
        bool IsGrandTotalRowNeedtoAdd = false;  
        if ((strPreviousRowName != string.Empty) && (DataBinder.Eval(e.Row.DataItem, "CustomerName") != null))  
            if (strPreviousRowName != DataBinder.Eval(e.Row.DataItem, "CustomerName").ToString())  
                IsSubTotalRowNeedToAdd = true;  
        if ((strPreviousRowName != string.Empty) && (DataBinder.Eval(e.Row.DataItem, "CustomerName") == null))  
        {  
            IsSubTotalRowNeedToAdd = true;  
            IsGrandTotalRowNeedtoAdd = true;  
            intSubTotalIndex = 0;  
        }  

        if ((strPreviousRowName == string.Empty) && (DataBinder.Eval(e.Row.DataItem, "CustomerName") != null))  
        {  
            GridView grdViewOrders = (GridView)sender;  
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
            TableCell cell = new TableCell();  
            cell.Text = "Customer Name : " + DataBinder.Eval(e.Row.DataItem, "CustomerName").ToString();  
            cell.ColumnSpan = 3;  
            cell.CssClass = "GroupHeaderStyle";  
            row.Cells.Add(cell);  
            grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);  
            intSubTotalIndex++;  
        }  
          
        if (IsSubTotalRowNeedToAdd)  
        {  
            #region Adding Sub Total Row  
            GridView grdViewOrders = (GridView)sender;  
            // Creating a Row            
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
            //Adding Total Cell            
            TableCell cell = new TableCell();  
            cell.Text = "Sub Total";  
            cell.HorizontalAlign = HorizontalAlign.Left;  
            cell.ColumnSpan = 1;  
            cell.CssClass = "SubTotalRowStyle";  
            row.Cells.Add(cell);  
            //Adding Quantity Column              
            cell = new TableCell();  
            cell.Text = dblSubTotalQuantity.ToString();  
            cell.HorizontalAlign = HorizontalAlign.Right;  
            cell.CssClass = "SubTotalRowStyle";  
            row.Cells.Add(cell);  
            //Adding Unit Price Column            
            cell = new TableCell();  
            cell.Text = string.Format("{0:0.00}", dblSubTotalUnitPrice);  
            cell.HorizontalAlign = HorizontalAlign.Right;  
            cell.CssClass = "SubTotalRowStyle";  
            row.Cells.Add(cell);     
            //Adding the Row at the RowIndex position in the Grid        
            grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);  
            intSubTotalIndex++;  
            #endregion  
            #region Adding Next Group Header Details  
            if (DataBinder.Eval(e.Row.DataItem, "CustomerName") != null)  
            {  
                row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
                cell = new TableCell();  
                cell.Text = "Customer Name : " + DataBinder.Eval(e.Row.DataItem, "CustomerName").ToString();  
                cell.ColumnSpan = 3;  
                cell.CssClass = "GroupHeaderStyle";  
                row.Cells.Add(cell);  
                grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);  
                intSubTotalIndex++;  
            }  
            #endregion  
            #region Reseting the Sub Total Variables  
            dblSubTotalUnitPrice = 0;  
            dblSubTotalQuantity = 0;  
            #endregion  
        }  
        if (IsGrandTotalRowNeedtoAdd)  
        {  
            #region Grand Total Row  
            GridView grdViewOrders = (GridView)sender;  
            // Creating a Row        
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
            //Adding Total Cell             
            TableCell cell = new TableCell();  
            cell.Text = "Grand Total";  
            cell.HorizontalAlign = HorizontalAlign.Left;  
            cell.ColumnSpan = 1;  
            cell.CssClass = "GrandTotalRowStyle";  
            row.Cells.Add(cell);  
            //Adding Quantity Column             
            cell = new TableCell();  
            cell.Text = dblGrandTotalQuantity.ToString();  
            cell.HorizontalAlign = HorizontalAlign.Right;  
            cell.CssClass = "GrandTotalRowStyle";  
            row.Cells.Add(cell);  
            //Adding Unit Price Column            
            cell = new TableCell();  
            cell.Text = string.Format("{0:0.00}", dblGrandTotalUnitPrice);  
            cell.HorizontalAlign = HorizontalAlign.Right;  
            cell.CssClass = "GrandTotalRowStyle";  
            row.Cells.Add(cell);  
            //Adding the Row at the RowIndex position in the Grid       
            grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex, row);  
            #endregion  
        }  
    }  
      

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
    {  
        // This is for cumulating the values         
        if (e.Row.RowType == DataControlRowType.DataRow)  
        {  
            strPreviousRowName = DataBinder.Eval(e.Row.DataItem, "CustomerName").ToString();  
            double dblUnitPrice = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Price").ToString());  
            double dblQuantity = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Qty").ToString());  
            // Cumulating Sub Total              
            dblSubTotalUnitPrice += dblUnitPrice;  
            dblSubTotalQuantity += dblQuantity;  
            // Cumulating Grand Total             
            dblGrandTotalUnitPrice += dblUnitPrice;  
            dblGrandTotalQuantity += dblQuantity;  
        }  
    }  

for example if price column comes empty or zero then i want to hide that column how to do so

thanking you

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,505 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,746 Reputation points Microsoft Vendor
    2022-09-21T05:22:56.53+00:00

    Hi @RAVI ,

    for example if price column comes empty or zero then i want to hide that column how to do so

    You can try to determine whether to delete a row in RowDataBound.
    For example

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
            {  
                        
                if (e.Row.RowType == DataControlRowType.DataRow)  
                {  
                    string dblUnitPrice = e.Row.Cells[2].Text.ToString();  
                    if (dblUnitPrice == "&nbsp;")  
                        {  
                          e.Row.Visible = false;      
                        }               
                }  
            }  
    

    EDIT

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
            {  
                if (e.Row.RowType == DataControlRowType.DataRow)  
                {  
                    Label nov = e.Row.FindControl("L2") as Label;  
                    double dblQuantity = Convert.ToDouble(nov.Text);                                                 
                    if (dblQuantity == 0)  
                    {  
                        GridView1.Columns[1].Visible = false;  
                    }  
               }  
            }  
    

    EDIT2

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)  
            {  
                bool IsSubTotalRowNeedToAdd = false;  
                bool IsGrandTotalRowNeedtoAdd = false;  
                if ((strPreviousRowName != string.Empty) && (DataBin der.E v a l(e.Row.DataItem, "CustomerName") != null))  
                    if (strPreviousRowName != DataBin der.E v a l(e.Row.DataItem, "CustomerName").ToString())  
                        IsSubTotalRowNeedToAdd = true;  
                if ((strPreviousRowName != string.Empty) && (DataBin der.E v a l(e.Row.DataItem, "CustomerName") == null))  
                {  
                    IsSubTotalRowNeedToAdd = true;  
                    IsGrandTotalRowNeedtoAdd = true;  
                    intSubTotalIndex = 0;  
                }  
                if ((strPreviousRowName == string.Empty) && (DataBin der.E  v a l(e.Row.DataItem, "CustomerName") != null))  
                {  
                    GridView grdViewOrders = (GridView)sender;  
                    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
                    TableCell cell = new TableCell();  
                    cell.Text = "Customer Name : " + DataBinder.Ev al(e.Row.DataItem, "CustomerName").ToString();  
                    if(dblSubTotalQuantity == 0)  
                    {  
                        cell.ColumnSpan = 2;  
                    }  
                    else  
                    {  
                        cell.ColumnSpan = 3;  
                    }  
                     
                    cell.CssClass = "GroupHeaderStyle";  
                    row.Cells.Add(cell);  
                    grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);  
                    intSubTotalIndex++;  
                }  
           if (IsSubTotalRowNeedToAdd)  
                {  
                    #region Adding Sub Total Row  
                    GridView grdViewOrders = (GridView)sender;  
                    // Creating a Row            
                    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
                    //Adding Total Cell            
                    TableCell cell = new TableCell();  
                    cell.Text = "Sub Total";  
                    cell.HorizontalAlign = HorizontalAlign.Left;  
                    cell.ColumnSpan = 1;  
                    cell.CssClass = "SubTotalRowStyle";  
                    row.Cells.Add(cell);  
                    //Adding Quantity Column  
                    if(dblSubTotalQuantity != 0)  
                    {  
                        cell = new TableCell();  
                        cell.Text = dblSubTotalQuantity.ToString();  
                        cell.HorizontalAlign = HorizontalAlign.Right;  
                        cell.CssClass = "SubTotalRowStyle";  
                        row.Cells.Add(cell);  
                    }  
                    
                    //Adding Unit Price Column            
                    cell = new TableCell();  
                    cell.Text = string.For mat("{0:0.00}", dblSubTotalUnitPrice);  
                    cell.HorizontalAlign = HorizontalAlign.Right;  
                    cell.CssClass = "SubTotalRowStyle";  
                    row.Cells.Add(cell);  
                    //Adding the Row at the RowIndex position in the Grid        
                    grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);  
                    intSubTotalIndex++;  
                    #endregion  
                    #region Adding Next Group Header Details  
                    if (DataBin der.Ev al(e.Row.DataItem, "CustomerName") != null)  
                    {  
                        row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
                        cell = new TableCell();  
                        cell.Text = "Customer Name : " + DataBi nder.E v al(e.Row.DataItem, "CustomerName").ToString();  
                        cell.ColumnSpan = 3;  
                        cell.CssClass = "GroupHeaderStyle";  
                        row.Cells.Add(cell);  
                        grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);  
                        intSubTotalIndex++;  
                    }  
                    #endregion  
                    #region Reseting the Sub Total Variables  
                    dblSubTotalUnitPrice = string.Empty;  
                    dblSubTotalQuantity = 0;  
                    #endregion  
                }  
                if (IsGrandTotalRowNeedtoAdd)  
                {  
                    #region Grand Total Row  
                    GridView grdViewOrders = (GridView)sender;  
                    // Creating a Row        
                    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
                    //Adding Total Cell             
                    TableCell cell = new TableCell();  
                    cell.Text = "Grand Total";  
                    cell.HorizontalAlign = HorizontalAlign.Left;  
                    cell.ColumnSpan = 1;  
                    cell.CssClass = "GrandTotalRowStyle";  
                    row.Cells.Add(cell);  
                    //Adding Quantity Column  
                    if (dblGrandTotalQuantity != 0)  
                    {  
                        cell = new TableCell();  
                        cell.Text = dblGrandTotalQuantity.ToString();  
                        cell.HorizontalAlign = HorizontalAlign.Right;  
                        cell.CssClass = "GrandTotalRowStyle";  
                        row.Cells.Add(cell);  
                    }  
                    //Adding Unit Price Column            
                    cell = new TableCell();  
                    cell.Text = string.Fo rm at("{0:0.00}", dblGrandTotalUnitPrice);  
                    cell.HorizontalAlign = HorizontalAlign.Right;  
                    cell.CssClass = "GrandTotalRowStyle";  
                    row.Cells.Add(cell);  
                    //Adding the Row at the RowIndex position in the Grid       
                    grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex, row);  
                    #endregion  
                }}  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Sreeju Nair 12,361 Reputation points
    2022-09-20T11:51:56.947+00:00

    Do you mean, when the price column is 0 for one record, just hide that data just for that record? for this purpose, refer the TemplateField in Gridview.

    Refer: https://learn.microsoft.com/en-us/aspnet/web-forms/overview/data-access/custom-formatting/using-templatefields-in-the-gridview-control-cs

    0 comments No comments

  2. RAVI 1,056 Reputation points
    2022-09-21T06:28:48.687+00:00

    Hello

    Here is my sql data

    /****** Object: Table [dbo].[TestTable2] Script Date: 09/21/2022 12:20:55 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].TestTable2 ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    SET IDENTITY_INSERT [dbo].[TestTable2] ON
    INSERT [dbo].[TestTable2] ([ID], [CustomerName], [ItemName], [Price], [Qty]) VALUES (1, N'AA', N'AA', 11, 0)
    INSERT [dbo].[TestTable2] ([ID], [CustomerName], [ItemName], [Price], [Qty]) VALUES (2, N'AA', N'BB', 45, 0)
    INSERT [dbo].[TestTable2] ([ID], [CustomerName], [ItemName], [Price], [Qty]) VALUES (3, N'KK', N'CC', 55, 0)
    INSERT [dbo].[TestTable2] ([ID], [CustomerName], [ItemName], [Price], [Qty]) VALUES (4, N'KK', N'DD', 23, 0)
    INSERT [dbo].[TestTable2] ([ID], [CustomerName], [ItemName], [Price], [Qty]) VALUES (5, N'KK', N'EE', 32, 0)
    INSERT [dbo].[TestTable2] ([ID], [CustomerName], [ItemName], [Price], [Qty]) VALUES (6, N'MM', N'FF', 56, 0)
    INSERT [dbo].[TestTable2] ([ID], [CustomerName], [ItemName], [Price], [Qty]) VALUES (7, N'MM', N'GG', 65, 0)
    SET IDENTITY_INSERT [dbo].[TestTable2] OFF

    My aspx page

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="tt.aspx.cs" Inherits="tt" %>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    <style>

    .SubTotalRowStyle {  
         border: solid 1px Black;  
         background-color: #D8D8D8;  
         font-weight: bold;  
     }  
     .GrandTotalRowStyle {  
         border: solid 1px Gray;  
         background-color: #000000;  
         color: #ffffff;  
         font-weight: bold;  
     }  
     .GroupHeaderStyle {  
         border: solid 1px Black;  
         background-color: #4682B4;  
         color: #ffffff;  
         font-weight: bold;  
     }  
     .serh-grid {  
         width: 85%;  
         border: 1px solid #6AB5FF;  
         background: #fff;  
         line-height: 14px;  
         font-size: 11px;  
         font-family: Verdana;  
     }  
    

    </style>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
    OnRowDataBound="GridView1_RowDataBound"
    OnRowCreated="GridView1_RowCreated"
    EnableModelValidation="True">
    <Columns>
    <asp:BoundField DataField="ItemName" HeaderText="ItemName" SortExpression="ItemName" />

                  <asp:TemplateField HeaderText="Qty">  
               
                    <FooterTemplate>  
                        <asp:Label ID="FL2" runat="server"></asp:Label>  
                    </FooterTemplate>  
                    <ItemTemplate>  
                        <asp:Label ID="L2" runat="server"  
                         Text='<%# ((Eval("Qty")!= DBNull.Value ? Convert.ToDouble(Eval("Qty")) : 0)).ToString() %>'></asp:Label>  
                           
                    </ItemTemplate>  
                    <HeaderStyle ForeColor="Black" />  
                    <ItemStyle ForeColor="Teal" Width="50px"  HorizontalAlign="Right"  />  
                    <FooterStyle ForeColor="Teal" Font-Size="Medium"   HorizontalAlign="Right"  />  
                </asp:TemplateField>   
                   
                   
                 <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />  
             </Columns>  
         </asp:GridView>  
    </div>  
    </form>  
    

    </body>
    </html>

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Collections.Specialized;
    using System.Text;
    using System.Drawing;
    using System.IO;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Configuration;

    public partial class tt : System.Web.UI.Page
    {
    public string constr = ConfigurationManager.ConnectionStrings["CHEMIMSConnectionString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    BindGridView();
    }
    }

    protected void BindGridView()  
    {  
        DataTable dt = new DataTable();  
        using (SqlConnection con = new SqlConnection(constr))  
        {  
            string cmdtext = "select ID, CustomerName, ItemName, Price, Qty from TestTable2";  
            using (SqlCommand cmd = new SqlCommand(cmdtext, con))  
            {  
                con.Open();  
                SqlDataAdapter sda = new SqlDataAdapter(cmd);  
                sda.Fill(dt);  
                GridView1.DataSource = dt;  
                GridView1.DataBind();  
            }  
        }  
    }  
    // To keep track of the previous row Group Identifier      
    string strPreviousRowName = string.Empty;  
    // To keep track the Index of Group Total      
    int intSubTotalIndex = 1;  
    // To temporarily store Sub Total      
    double dblSubTotalUnitPrice = 0;  
    double dblSubTotalQuantity = 0;  
    // To temporarily store Grand Total      
    double dblGrandTotalUnitPrice = 0;  
    double dblGrandTotalQuantity = 0;  
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)  
    {  
        bool IsSubTotalRowNeedToAdd = false;  
        bool IsGrandTotalRowNeedtoAdd = false;  
        if ((strPreviousRowName != string.Empty) && (DataBinder.Eval(e.Row.DataItem, "CustomerName") != null))  
            if (strPreviousRowName != DataBinder.Eval(e.Row.DataItem, "CustomerName").ToString())  
                IsSubTotalRowNeedToAdd = true;  
        if ((strPreviousRowName != string.Empty) && (DataBinder.Eval(e.Row.DataItem, "CustomerName") == null))  
        {  
            IsSubTotalRowNeedToAdd = true;  
            IsGrandTotalRowNeedtoAdd = true;  
            intSubTotalIndex = 0;  
        }  
        if ((strPreviousRowName == string.Empty) && (DataBinder.Eval(e.Row.DataItem, "CustomerName") != null))  
        {  
            GridView grdViewOrders = (GridView)sender;  
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
            TableCell cell = new TableCell();  
            cell.Text = "Customer Name : " + DataBinder.Eval(e.Row.DataItem, "CustomerName").ToString();  
            cell.ColumnSpan = 3;  
            cell.CssClass = "GroupHeaderStyle";  
            row.Cells.Add(cell);  
            grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);  
            intSubTotalIndex++;  
        }  
    
        if (IsSubTotalRowNeedToAdd)  
        {  
            #region Adding Sub Total Row  
            GridView grdViewOrders = (GridView)sender;  
            // Creating a Row            
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
            //Adding Total Cell            
            TableCell cell = new TableCell();  
            cell.Text = "Sub Total";  
            cell.HorizontalAlign = HorizontalAlign.Left;  
            cell.ColumnSpan = 1;  
            cell.CssClass = "SubTotalRowStyle";  
            row.Cells.Add(cell);  
            //Adding Quantity Column              
            cell = new TableCell();  
            cell.Text = dblSubTotalQuantity.ToString();  
            cell.HorizontalAlign = HorizontalAlign.Right;  
            cell.CssClass = "SubTotalRowStyle";  
            row.Cells.Add(cell);  
            //Adding Unit Price Column            
            cell = new TableCell();  
            cell.Text = string.Format("{0:0.00}", dblSubTotalUnitPrice);  
            cell.HorizontalAlign = HorizontalAlign.Right;  
            cell.CssClass = "SubTotalRowStyle";  
            row.Cells.Add(cell);  
            //Adding the Row at the RowIndex position in the Grid        
            grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);  
            intSubTotalIndex++;  
            #endregion  
            #region Adding Next Group Header Details  
            if (DataBinder.Eval(e.Row.DataItem, "CustomerName") != null)  
            {  
                row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
                cell = new TableCell();  
                cell.Text = "Customer Name : " + DataBinder.Eval(e.Row.DataItem, "CustomerName").ToString();  
                cell.ColumnSpan = 3;  
                cell.CssClass = "GroupHeaderStyle";  
                row.Cells.Add(cell);  
                grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);  
                intSubTotalIndex++;  
            }  
            #endregion  
            #region Reseting the Sub Total Variables  
            dblSubTotalUnitPrice = 0;  
            dblSubTotalQuantity = 0;  
            #endregion  
        }  
        if (IsGrandTotalRowNeedtoAdd)  
        {  
            #region Grand Total Row  
            GridView grdViewOrders = (GridView)sender;  
            // Creating a Row        
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
            //Adding Total Cell             
            TableCell cell = new TableCell();  
            cell.Text = "Grand Total";  
            cell.HorizontalAlign = HorizontalAlign.Left;  
            cell.ColumnSpan = 1;  
            cell.CssClass = "GrandTotalRowStyle";  
            row.Cells.Add(cell);  
            //Adding Quantity Column             
            cell = new TableCell();  
            cell.Text = dblGrandTotalQuantity.ToString();  
            cell.HorizontalAlign = HorizontalAlign.Right;  
            cell.CssClass = "GrandTotalRowStyle";  
            row.Cells.Add(cell);  
            //Adding Unit Price Column            
            cell = new TableCell();  
            cell.Text = string.Format("{0:0.00}", dblGrandTotalUnitPrice);  
            cell.HorizontalAlign = HorizontalAlign.Right;  
            cell.CssClass = "GrandTotalRowStyle";  
            row.Cells.Add(cell);  
            //Adding the Row at the RowIndex position in the Grid       
            grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex, row);  
            #endregion  
        }  
    }  
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
    {  
        if (e.Row.RowType == DataControlRowType.DataRow)  
        {  
            Label nov = e.Row.FindControl("L2") as Label;  
    
            strPreviousRowName = DataBinder.Eval(e.Row.DataItem, "CustomerName").ToString();  
            double dblUnitPrice = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Price").ToString());  
            double dblQuantity = Convert.ToDouble(nov.Text);  
            // Cumulating Sub Total              
            dblSubTotalUnitPrice += dblUnitPrice;  
            dblSubTotalQuantity += dblQuantity;  
            // Cumulating Grand Total             
            dblGrandTotalUnitPrice += dblUnitPrice;  
            dblGrandTotalQuantity += dblQuantity;  
    

    //
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    string dblUnitPrice1 = e.Row.Cells[1].Text.ToString();
    if (dblUnitPrice1 == "0")
    {
    GridView1.Columns[2].Visible = false;
    }
    }

        }  
    }  
    

    }

    I want to hide Qty column because it has zero value how to do


  3. RAVI 1,056 Reputation points
    2022-09-21T09:32:32.813+00:00

    Hello

    How to hide this subtotal and grandtotal as well like below

    243452-mm.jpg


  4. RAVI 1,056 Reputation points
    2022-09-22T07:05:40.92+00:00

    hello
    still not working its not hiding column


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.