question

RAVI-6930 avatar image
0 Votes"
RAVI-6930 asked LanHuang-MSFT commented

hide gridview coumn if empty or zero

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

dotnet-aspnet-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered LanHuang-MSFT edited

Hi @RAVI-6930,

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

sreejukg avatar image
0 Votes"
sreejukg answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RAVI-6930 avatar image
0 Votes"
RAVI-6930 answered LanHuang-MSFT commented

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](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CustomerName] [varchar](50) NULL,
[ItemName] [varchar](50) NULL,
[Price] [float] NULL,
[Qty] [float] NULL,
CONSTRAINT [PK_TestTable2] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) 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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RAVI-6930,
First you get the value of L2(Qty), you can directly judge dblQuantity. ie: if (dblQuantity == 0).
Columns start at 0 and Qty is in the second column, so it's GridView1.Columns[1].
The specific code I have edited into the answer, you can check it out.
Best regards,
Lan Huang

0 Votes 0 ·
RAVI-6930 avatar image
0 Votes"
RAVI-6930 answered LanHuang-MSFT edited

Hello

How to hide this subtotal and grandtotal as well like below

243452-mm.jpg



mm.jpg (40.8 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RAVI-6930,
Because Sub Total and Grand Total are new lines added later. You just need to judge whether you need to add.
IsSubTotalRowNeedToAdd:

  //Adding Quantity Column
                 if(dblSubTotalQuantity != 0)
             {
                 cell = new TableCell();
                 cell.Text = dblSubTotalQuantity.ToString();
                 cell.HorizontalAlign = HorizontalAlign.Right;
                 cell.CssClass = "SubTotalRowStyle";
                 row.Cells.Add(cell);
             }

IsGrandTotalRowNeedtoAdd:

 //Adding Quantity Column
                 if (dblGrandTotalQuantity != 0)
                 {
                     cell = new TableCell();
                     cell.Text = dblGrandTotalQuantity.ToString();
                     cell.HorizontalAlign = HorizontalAlign.Right;
                     cell.CssClass = "GrandTotalRowStyle";
                     row.Cells.Add(cell);
                 }

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 Votes 0 ·
RAVI-6930 avatar image
0 Votes"
RAVI-6930 answered LanHuang-MSFT edited

hello
still not working its not hiding column

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RAVI-6930,
If you have any questions, please click to 243799-image.png .
Are your current effects completely unchanged?
I suggest you to check that your code is positioned correctly.
I've provided the full code for GridView1_RowCreated for your reference, I've edited it into my answer.
Best regards,
Lan Huang



0 Votes 0 ·
image.png (1.1 KiB)
image.png (1.1 KiB)
RAVI-6930 avatar image
0 Votes"
RAVI-6930 answered LanHuang-MSFT commented

Hello

Can you please copy and paste my full code and check now and show me one small output its not working my side

thanks

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RAVI-6930,
The method provided before is to use all your code to test, I only modified three places, I have marked in the screenshot, you can check.
244037-image.png
243940-image.png
244044-image.png
244071-image.png
Best regards,
Lan Huang


0 Votes 0 ·
image.png (66.3 KiB)
image.png (73.5 KiB)
image.png (61.3 KiB)
image.png (16.8 KiB)