locked
hiding column label in Gridview RRS feed

  • Question

  • User351619809 posted

    Hello All,

    I have a gridview and I have a Template column in my gridview. Inside the Itemtemplate, I have two label controls. I am trying to hide one of the label control if a certain condition is met. The condition is that when second column which is a boundField. When that Item has a value of  "Expedit" then I want the label to be hidden. This is the label that I want to hide <asp:Label ID="lblDesc1" runat="server" Text='<%# Eval("RequestedBy") when item=Expedit. Below is what I tried to do and did not work:

     protected void grdShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e)
            {
               
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
    				Label lbl = (e.Row.FindControl("lblPrice") as Label);
                    TextBox tbq = (e.Row.FindControl("txtQuantity") as TextBox);
                    if (DataBinder.Eval(e.Row.DataItem, "Item").ToString() == "Expedit")
                    {
                        tbq.Enabled = false;
                        Label lblDesc1 = (e.Row.FindControl("lblDesc1") as Label);
    					lblDesc1.Visible=false;
                        lblDesc1.Text = "";
                    }
    
    			
    			}
    		}

    <div>above, I tried to search for the label lblDesc1, in my gridview and hide it when the condition is Expedit and it didnt work. I just want to hide lblDesc1 when item=Expedit. I dont want to hide lblDesc. below is my grid

    <asp:GridView ID="grdShoppingCart" runat="server" AutoGenerateColumns="false" class="ui-responsive table-stroke ss-table ui-search-result-table" DataKeyNames="CartID" AllowPaging="false" PageSize="5"  GridLines="None"  OnRowDataBound="grdShoppingCart_RowDataBound" OnRowDeleting="grdShoppingCart_RowDeleting">
    				<Columns>
    					
    					<asp:BoundField DataField="CartID" Visible="false" HeaderText="CartID" />
    					<asp:BoundField DataField="item" HeaderText="Item" HeaderStyle-Font-Bold="true" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="250px" ControlStyle-CssClass="ss-row"    />
    					<asp:TemplateField HeaderText="Description" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="300px" ControlStyle-CssClass="ss-row"    >
    						<ItemTemplate>
    							<asp:Label ID="lblDesc" runat="server" Text='<%# Eval("RequestedFor") %>' /><br />
    							&nbsp;Requested By: &nbsp;<asp:Label ID="lblDesc1" runat="server" Text='<%# Eval("RequestedBy") %>' />
    						</ItemTemplate>
    
    					</asp:TemplateField>
    					   <asp:TemplateField HeaderText="Quantity" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"  ControlStyle-CssClass="ss-row"  >
                            <ItemTemplate>
    									<asp:TextBox Width="45px" min="1" max="99" step="1" TextMode="Number" ID="txtQuantity"  Text='<%#Eval("Quantity") %>'    runat="server" AutoPostBack="true" OnTextChanged="txtQuantity_TextChanged"  ></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
    					<asp:TemplateField HeaderText="Price" HeaderStyle-HorizontalAlign="right" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="100px" ControlStyle-CssClass="ss-row" >
                            <ItemTemplate>
    							<asp:Label ID="lblPrice" runat="server"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
    			</Columns>
    
    			</asp:GridView>

    Is it possible to achieve that.
                
                Any help will be highly appreciated.

    Saturday, December 19, 2020 12:44 AM

Answers

  • User1535942433 posted

    Hi anjaliagarwal5@yahoo.com,

    The condition is that when second column which is a boundField. When that Item has a value of  "Expedit" then I want the label to be hidden.

    What is the item?Is is this :

    <asp:BoundField DataField="item" HeaderText="Item" HeaderStyle-Font-Bold="true" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="250px" ControlStyle-CssClass="ss-row"    />

    If you need when the item's value is "Expedit",the label lblDesc1 will be disabled,you could find boundfield in the rowdatabound event.Just like this:

    protected void grdShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label lbl = (e.Row.FindControl("lblPrice") as Label);
                    TextBox tbq = (e.Row.FindControl("txtQuantity") as TextBox);
                    if (e.Row.Cells[1].Text == "Expedit")
                    {
                        tbq.Enabled = false;
                        Label lblDesc1 = (e.Row.FindControl("lblDesc1") as Label);
                        lblDesc1.Visible = false;
                        lblDesc1.Text = "";
                    }
                }
            }

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 21, 2020 4:13 AM