locked
how To Get Column Total Based On My Code RRS feed

  • Question

  • User2033107836 posted

    Hello

    I need cell9 column Total how to do so and show in footer label of gridview

    protected void txt_cell_7_TextChanged(object sender, EventArgs e)
    {
        Sorting();
    }
    
    public void Sorting()
    {
        if (lbl_cell_6 != null && lbl_cell_8 != null)
        {
            string str = lbl_cell_6.Text;
            //string[] sp_str = { "\r\n" };
            string[] sp_str = { "<br />" };//here I used <br />
            string[] snq = str.Split(sp_str, StringSplitOptions.RemoveEmptyEntries);
    
            List<double> doublelist = new List<double>();
    
            foreach (string item in snq)
            {
                string sa = item.Replace("\"", "");
                doublelist.Add(double.Parse(sa.Split(',')[1]));
    
                if (sa.Equals(txt_cell_7.Text))
                {
                    break;
                }
            }
    
            int maxIndex = GetMaxValueIndex(doublelist);
    
            string[] resultList = GetResultList(doublelist, maxIndex);
    
            double sum = GetSum(resultList);
    
            lbl_cell_9.Text = sum.ToString();
            lbl_cell_10.Text = resultList[0]; //show the first value
            lbl_cell_8.Text = string.Join("</br>", resultList);
        }
    }
    
    //get the result list
    private string[] GetResultList(List<double> doublelist, int maxIndex)
    {
        string[] resultList = new string[doublelist.Count - maxIndex];
    
        for (int i = 0; i < resultList.Length; i++)
        {
            resultList[i] = doublelist[i + maxIndex].ToString();
        }
    
        return resultList;
    }
    
    //get Sum
    private double GetSum(string[] resultList)
    {
        double sum = 0;
        for (int i = 0; i < resultList.Length; i++)
        {
            sum += double.Parse(resultList[i]);
        }
        return sum;
    }
    
    //get max value index
    protected int GetMaxValueIndex(List<double> doublelist)
    {
        List<double> sortedList = new List<double>();
        for (int i = 0; i < doublelist.Count; i++)
        {
            sortedList.Add(doublelist[i]);
        }
    
        double temp = 0;
        int minIndexTemp = 0;
        for (int i = 0; i < sortedList.Count; i++)
        {
            minIndexTemp = i;
            for (int j = i; j < sortedList.Count; j++)
            {
                if (sortedList[minIndexTemp] > sortedList[j])
                {
                    minIndexTemp = j;
                }
            }
            temp = sortedList[minIndexTemp];
            sortedList[minIndexTemp] = sortedList[i];
            sortedList[i] = temp;
        }
    
        double maxValue = sortedList[sortedList.Count - 1];
    
        return doublelist.IndexOf(maxValue);
    }

    Tuesday, August 8, 2017 6:32 AM

Answers

  • User-1508012637 posted

    Hi asp.ambur,

    I need cell9 column Total how to do so and show in footer label of gridview

    According to your description, I’ve created a demo and the screenshot as below:

    Please refer to the following code:

    WebForm1.aspx

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server"
                    AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
                    GridLines="None" ShowFooter="True">
    
                    <Columns>
                        <asp:TemplateField HeaderText="Cell_6" FooterText="Total:">
                            <ItemTemplate><asp:Label ID="lbl_cell_6" runat="server" Text=""></asp:Label></ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Cell_7">
                            <ItemTemplate><asp:TextBox ID="txt_cell_7" AutoPostBack="true" runat="server" OnTextChanged="txt_cell_7_TextChanged"></asp:TextBox></ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Cell_8">
                            <ItemTemplate><asp:Label ID="lbl_cell_8" runat="server" Text="0"></asp:Label></ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Cell_9">
                            <ItemTemplate><asp:Label ID="lbl_cell_9" runat="server" Text="0"></asp:Label></ItemTemplate>
                            <FooterTemplate>
                                <asp:Label ID="lbl_cell_11" runat="server" Text="0"></asp:Label>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Cell10">
                            <ItemTemplate><asp:Label ID="lbl_cell_10" runat="server" Text="0"></asp:Label></ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
    
                    <%--Set styles to get better GridView appearance--%>
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                </asp:GridView>
            </div>
        </form>
    </body>
    

    And change the code in Sorting() method to:

    public void Sorting()
    {
        if (lbl_cell_6 != null && lbl_cell_8 != null)
        {
            string str = lbl_cell_6.Text;
            //string[] sp_str = { "\r\n" };
            string[] sp_str = { "<br />" };//here I used <br />
            string[] snq = str.Split(sp_str, StringSplitOptions.RemoveEmptyEntries);
    
            List<double> doublelist = new List<double>();
    
            foreach (string item in snq)
            {
                string sa = item.Replace("\"", "");
                doublelist.Add(double.Parse(sa.Split(',')[1]));
    
                if (sa.Equals(txt_cell_7.Text))
                {
                    break;
                }
            }
    
            int maxIndex = GetMaxValueIndex(doublelist);
    
            string[] resultList = GetResultList(doublelist, maxIndex);
    
            double sum = GetSum(resultList);
    
            lbl_cell_9.Text = sum.ToString();
            lbl_cell_11.Text = sum.ToString();
            lbl_cell_10.Text = resultList[0];
            lbl_cell_8.Text = string.Join("</br>", resultList);
        }
    }
    

    If you have any other questions, please feel free to contact me any time.

    Best Regards
    Velen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 9, 2017 9:49 AM

All replies

  • User-359936451 posted

    You need to do all of your work in the RowDataBound Event, here is my VB code. Perhaps you can adapt it.

        Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    
            If e.Row.RowType = DataControlRowType.DataRow Then
                If (DataBinder.Eval(e.Row.DataItem, "ColHeading#2")) = "True" Then
                    totalRecordCnt = totalRecordCnt + 1
                End If
                If String.IsNullOrEmpty((DataBinder.Eval(e.Row.DataItem, "ColHeading#1").ToString)) And (DataBinder.Eval(e.Row.DataItem, "ColHeading#2")) = "True" Then
                    transCompleted = transCompleted + 1
                End If
                If (DataBinder.Eval(e.Row.DataItem, "ColHeading#3")) = "True" And (DataBinder.Eval(e.Row.DataItem, "ColHeading#2")) = "True" Then
                    transPending = transPending + 1
                End If
    
            ElseIf e.Row.RowType = DataControlRowType.Footer Then
                e.Row.Cells(0).Text = "Total " & totalRecordCnt & ", Remaining:"
                ' for the Footer, display the running totals
                e.Row.Cells(1).Text = transCompleted.ToString("d") & " = Remaining: " & Convert.ToDecimal((transCompleted / totalRecordCnt)).ToString("p")
                e.Row.Cells(4).Text = transPending.ToString("d") & " = Remaining: " & Convert.ToDecimal((transPending / totalRecordCnt)).ToString("p")
    
                e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Center
                e.Row.Cells(4).HorizontalAlign = HorizontalAlign.Center
                e.Row.Font.Bold = True
            End If
        End Sub

    Tuesday, August 8, 2017 3:24 PM
  • User2033107836 posted

    I need C# code 

    Wednesday, August 9, 2017 6:24 AM
  • User-1508012637 posted

    Hi asp.ambur,

    I need cell9 column Total how to do so and show in footer label of gridview

    According to your description, I’ve created a demo and the screenshot as below:

    Please refer to the following code:

    WebForm1.aspx

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server"
                    AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
                    GridLines="None" ShowFooter="True">
    
                    <Columns>
                        <asp:TemplateField HeaderText="Cell_6" FooterText="Total:">
                            <ItemTemplate><asp:Label ID="lbl_cell_6" runat="server" Text=""></asp:Label></ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Cell_7">
                            <ItemTemplate><asp:TextBox ID="txt_cell_7" AutoPostBack="true" runat="server" OnTextChanged="txt_cell_7_TextChanged"></asp:TextBox></ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Cell_8">
                            <ItemTemplate><asp:Label ID="lbl_cell_8" runat="server" Text="0"></asp:Label></ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Cell_9">
                            <ItemTemplate><asp:Label ID="lbl_cell_9" runat="server" Text="0"></asp:Label></ItemTemplate>
                            <FooterTemplate>
                                <asp:Label ID="lbl_cell_11" runat="server" Text="0"></asp:Label>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Cell10">
                            <ItemTemplate><asp:Label ID="lbl_cell_10" runat="server" Text="0"></asp:Label></ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
    
                    <%--Set styles to get better GridView appearance--%>
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                </asp:GridView>
            </div>
        </form>
    </body>
    

    And change the code in Sorting() method to:

    public void Sorting()
    {
        if (lbl_cell_6 != null && lbl_cell_8 != null)
        {
            string str = lbl_cell_6.Text;
            //string[] sp_str = { "\r\n" };
            string[] sp_str = { "<br />" };//here I used <br />
            string[] snq = str.Split(sp_str, StringSplitOptions.RemoveEmptyEntries);
    
            List<double> doublelist = new List<double>();
    
            foreach (string item in snq)
            {
                string sa = item.Replace("\"", "");
                doublelist.Add(double.Parse(sa.Split(',')[1]));
    
                if (sa.Equals(txt_cell_7.Text))
                {
                    break;
                }
            }
    
            int maxIndex = GetMaxValueIndex(doublelist);
    
            string[] resultList = GetResultList(doublelist, maxIndex);
    
            double sum = GetSum(resultList);
    
            lbl_cell_9.Text = sum.ToString();
            lbl_cell_11.Text = sum.ToString();
            lbl_cell_10.Text = resultList[0];
            lbl_cell_8.Text = string.Join("</br>", resultList);
        }
    }
    

    If you have any other questions, please feel free to contact me any time.

    Best Regards
    Velen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 9, 2017 9:49 AM