locked
Problem in accessing the label id inside switch case. RRS feed

  • Question

  • User1649939557 posted

    Hey everyone,

    I'm trying to add the values in a column and displaying the total in my GridView. My GridView is being created dynamically and data is populated using a switch case. I'm adding the values of column in switch case itself. I want to show that particular sum on top of column along with it's name like "Total Sales=12345" using label. 

    Is there anyway I can access the TemplateField label in my switch case?

    <asp:TemplateField>
        <HeaderTemplate>Jul
        <asp:Label runat="server" ID="JulyTotal" Text="Total" Visible="false"></asp:Label>
        </HeaderTemplate>
        <ItemTemplate>
             <asp:Label ID="JulyMonth" runat="server" Text='<%# Eval("Jul") %>' CssClass="labelstyle"></asp:Label> </ItemTemplate>
    </asp:TemplateField>

    Here's my GridView sample code.

    case "July": SaleSummaryTempTable.Rows[i][5] = amt.ToString();
    sum = amt + sum;
    break;

    And this is my switch case.

    Is there anyway i can show this "sum" along with month name as I mentioned above.

    Tuesday, October 2, 2018 5:09 PM

All replies

  • User1649939557 posted

    I want to show it in a label not in footer. Thanks for replying.

    Tuesday, October 2, 2018 5:39 PM
  • User-471420332 posted

    create footer template inside grid view like below then assign label to that total value you can use above example.

     <FooterTemplate>
        <asp:Label ID="Labeltotal" runat="server" Text=""></asp:Label>
       </FooterTemplate
    Tuesday, October 2, 2018 6:47 PM
  • User-1716253493 posted

    You can access the labels in rowdatabound

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType==DataControlRowType.Header)
            {
                Label JulyTotal = (Label)e.Row.FindControl("JulyTotal");
            }
            if(e.Row.RowType==DataControlRowType.DataRow)
            {
                Label JulyMonth = (Label)e.Row.FindControl("JulyMonth");
            }
        }

    Use switch case to manipulate the labels properties

    You can also use below code after data bound already

    Label JulyTotal = (Label)GridView1.HeaderRow.FindControl("JulyTotal");
    Label JulyMonth = (Label)GridView1.Rows[0].FindControl("JulyMonth");

    Wednesday, October 3, 2018 1:22 AM
  • User61956409 posted

    Hi AdityaPradhan,

    I want to show that particular sum on top of column along with it's name like "Total Sales=12345" using label. 

    You can refer to the following example to achieve your requirement.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnDataBound="GridView1_DataBound">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    Jan<asp:Label runat="server" ID="JanTotal" Text="Total" Visible="false"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="JanMonth" runat="server" Text='<%# Eval("Jan") %>' CssClass="labelstyle"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
                <asp:TemplateField>
                <HeaderTemplate>
                    Feb<asp:Label runat="server" ID="FebTotal" Text="Total" Visible="false"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="FebMonth" runat="server" Text='<%# Eval("Feb") %>' CssClass="labelstyle"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
                <asp:TemplateField>
                <HeaderTemplate>
                    Mar<asp:Label runat="server" ID="MarTotal" Text="Total" Visible="false"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="MarMonth" runat="server" Text='<%# Eval("Mar") %>' CssClass="labelstyle"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
                <asp:TemplateField>
                <HeaderTemplate>
                    Apr<asp:Label runat="server" ID="AprTotal" Text="Total" Visible="false"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="AprMonth" runat="server" Text='<%# Eval("Apr") %>' CssClass="labelstyle"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
                <asp:TemplateField>
                <HeaderTemplate>
                    May<asp:Label runat="server" ID="MayTotal" Text="Total" Visible="false"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="MayMonth" runat="server" Text='<%# Eval("May") %>' CssClass="labelstyle"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
                <asp:TemplateField>
                <HeaderTemplate>
                    Jun<asp:Label runat="server" ID="JunTotal" Text="Total" Visible="false"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="JunMonth" runat="server" Text='<%# Eval("Jun") %>' CssClass="labelstyle"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <HeaderTemplate>
                    Jul<asp:Label runat="server" ID="JulyTotal" Text="Total" Visible="false"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="JulyMonth" runat="server" Text='<%# Eval("Jul") %>' CssClass="labelstyle"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    private int jan_total, feb_total, mar_total, apr_total, may_total,jun_total, jul_total = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Jan");
            dt.Columns.Add("Feb");
            dt.Columns.Add("Mar");
            dt.Columns.Add("Apr");
            dt.Columns.Add("May");
            dt.Columns.Add("Jun");
            dt.Columns.Add("Jul");
    
            dt.Rows.Add(1, 1, 2, 1, 2, 1, 3);
            dt.Rows.Add(2, 2, 3, 2, 2, 2, 3);
            dt.Rows.Add(3, 3, 3, 3, 3, 3, 3);
    
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
    
        foreach (GridViewRow row in GridView1.Rows)
        {
    
            jan_total += Convert.ToInt32((row.Cells[0].FindControl("JanMonth") as Label).Text);
            feb_total += Convert.ToInt32((row.Cells[1].FindControl("FebMonth") as Label).Text);
            mar_total += Convert.ToInt32((row.Cells[2].FindControl("MarMonth") as Label).Text);
            apr_total += Convert.ToInt32((row.Cells[3].FindControl("AprMonth") as Label).Text);
            may_total += Convert.ToInt32((row.Cells[4].FindControl("MayMonth") as Label).Text);
            jun_total += Convert.ToInt32((row.Cells[5].FindControl("JunMonth") as Label).Text);
            jul_total += Convert.ToInt32((row.Cells[6].FindControl("JulyMonth") as Label).Text);
            //.....
        }
    
        (GridView1.HeaderRow.Cells[0].FindControl("JanTotal") as Label).Visible = true;
        (GridView1.HeaderRow.Cells[0].FindControl("JanTotal") as Label).Text = " total sales = "+jan_total.ToString();
    
        (GridView1.HeaderRow.Cells[1].FindControl("FebTotal") as Label).Visible = true;
        (GridView1.HeaderRow.Cells[1].FindControl("FebTotal") as Label).Text = " total sales = " + feb_total.ToString();
    
        (GridView1.HeaderRow.Cells[2].FindControl("MarTotal") as Label).Visible = true;
        (GridView1.HeaderRow.Cells[2].FindControl("MarTotal") as Label).Text = " total sales = " + mar_total.ToString();
    
        (GridView1.HeaderRow.Cells[3].FindControl("AprTotal") as Label).Visible = true;
        (GridView1.HeaderRow.Cells[3].FindControl("AprTotal") as Label).Text = " total sales = " + apr_total.ToString();
    
        (GridView1.HeaderRow.Cells[4].FindControl("MayTotal") as Label).Visible = true;
        (GridView1.HeaderRow.Cells[4].FindControl("MayTotal") as Label).Text = " total sales = " + may_total.ToString();
    
        (GridView1.HeaderRow.Cells[5].FindControl("JunTotal") as Label).Visible = true;
        (GridView1.HeaderRow.Cells[5].FindControl("JunTotal") as Label).Text = " total sales = " + jun_total.ToString();
    
        (GridView1.HeaderRow.Cells[6].FindControl("JulyTotal") as Label).Visible = true;
        (GridView1.HeaderRow.Cells[6].FindControl("JulyTotal") as Label).Text = " total sales = " + jul_total.ToString();
        
        //another code logic here
    }

    Test Result:

    With Regards,

    Fei Han

    Wednesday, October 3, 2018 5:42 AM