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 == " ")
{
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.