-3

hear i am post the my grid view.

                <Columns>
                    <asp:TemplateField HeaderText="Item">
                            <ItemTemplate>
                                <p> <asp:Label ID="lblproductname"  display="Dynamic" runat="server" Text='<%# Bind("productname") %>'></asp:Label></p>
                                <p><asp:Label ID="lblProductWeight" display="Dynamic" runat="server" Text='<%# Bind("groupvalue") %>'></asp:Label></p> 
                                 <p> <asp:Label ID="lblProductType" display="Dynamic" runat="server" Text='<%# Bind("groupname") %>'></asp:Label></p> 
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Left" />
                            <ItemStyle HorizontalAlign="Left" />
                        </asp:TemplateField>
                    <asp:BoundField DataField="Quantity" HeaderText="Qty">
                        <HeaderStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Price" HeaderText="Price">
                        <HeaderStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="SubTotal" HeaderText="Sub Total">
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:BoundField>
                </Columns>
                <FooterStyle CssClass="datatable" />
            </asp:GridView>

so how to make the visible false ItemTemplate of lables programaticaly in xyz method

user2564537
  • 1,041
  • 1
  • 12
  • 16
  • I'm not sure if this is correct so don't want to edit question but I believe the question is: "How can I change the three labels visibility in `ItemTemplate`?" – Sayse Aug 30 '13 at 10:32

3 Answers3

3

In the Rowcommand in the grid you can Hide or shows item templates for example:

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      //Your Condition
      Label lblproductname = (Label)e.Row.FindControl("lblproductname")
      If(a > B)
         lblproductname.Visible = true;
      //Others Lables
      ....

    }

  }

I hope that help.

Szymon Kuzniak
  • 848
  • 1
  • 6
  • 16
Wilfredo P
  • 4,070
  • 28
  • 46
0
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
         e.Row.Cells[columnIndex].Visible = false;
}

The only workaround I can suggest is to give a HeaderText on ASPX Page and then find using that.

protected void xyz()
{
    ((DataControlField)youGridView.Columns
            .Cast<DataControlField>()
            .Where(fld => fld.HeaderText == "your header text")
            .SingleOrDefault()).Visible = false;
}
Joan Caron
  • 1,969
  • 18
  • 21
0

I believe that your question is answered here:

How to find control in TemplateField of GridView?

Copied from the answer:

foreach(GridViewRow row in GridView1.Rows) {
    if(row.RowType == DataControlRowType.DataRow) {
        Label myLabel = row.FindControl("myLabelID") as Label;
        myLabel.Visible = false;
    }
}

From RowDataBoundEvent:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        Label myLabel = e.Row.FindControl("myLabelID") as Label;
        myLabel.Visible = false;
    }
}
Community
  • 1
  • 1
rdans
  • 2,179
  • 22
  • 32