Below is my gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EnableModelValidation="True" onrowdatabound="GridView1_RowDataBound" onrowcommand="GridView1_RowCommand" onrowdeleting="GridView1_RowDeleting">
<asp:TemplateField HeaderText="Amount" HeaderStyle-ForeColor="DimGray" >
<ItemTemplate>
<asp:Label ID="lblamount" runat="server" Text='Label' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" HeaderStyle-ForeColor="DimGray" >
<ItemTemplate>
<asp:Label ID="lblprice" runat="server" Text='Label' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
in the RowData_BoundEvent
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl2 = (Label)e.Row.FindControl("lblamount");
if(amount>0) {lbl2.Text = amount;} else lbl2.Text = string.Empty;
Label lbl3 = (Label)e.Row.FindControl("lblprice");
if(price>0) {lbl3.Text = price;} else lbl3.Text = string.Empty;
If there is no value entered either in Amount or Price , I am trying not to display the values in the gridview if the amount is not greater than zero. But still, the grid displays Amount as Label and Price as Label if the condition does not satisfy.
You should use the GridViewRow’s DataItem to get these values.