locked
Sum Gridview Column From Highest Value RRS feed

  • Question

  • User2033107836 posted

    Hello

    This is my Gridview look like

    I used this below code to get total

     if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string fileData = e.Row.Cells[6].Text.Replace("&quote;", "").Replace("</br>", "").Trim(); 
                string[] splittedFileData = fileData.Split(new string[] { "'" }, StringSplitOptions.RemoveEmptyEntries);
                if (null != splittedFileData && splittedFileData.Length > 0)
                {
                    double total = 0;
                  
                    foreach (string data in splittedFileData)
                    {
                        double tempResult = 0;
                        if (double.TryParse(data.Trim(), out tempResult))
                            total += tempResult;
                    }
                    //-- display total in a template field column with lblTotal as Label control
                    (e.Row.FindControl("lblTotal") as System.Web.UI.WebControls.Label).Text = total.ToString("##.###");
    
                    //-- display no of data in a template field column with lblNoOfData as Label control
                    (e.Row.FindControl("lblNoOfData") as System.Web.UI.WebControls.Label).Text = splittedFileData.Length.ToString();
    
                    double kk = total / splittedFileData.Length;
                    //-- display avarage in a template field column with lblAverageData as Label control
                    (e.Row.FindControl("lblAverageData") as System.Web.UI.WebControls.Label).Text = (kk / 9.81).ToString("##.###");
                }
            }

    But i want to start totalling from highest value that is in first row highest value is 13.6

    second row highest value is 17.3

    Thursday, April 6, 2017 11:07 AM

Answers

  • User-1509636757 posted

    Did you try out yourself? It is fairly easy; just use a counter where sum is being done.

    <asp:TemplateField HeaderText="Sample Col">
        <ItemTemplate>
            <asp:Label ID="lblNumbers" runat="server" Text='<%# Eval("Numbers") %>'></asp:Label>
            <br />
            <asp:Label ID="lblStart" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblCount" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblDataCount" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblTotal" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string fileData = ((Label)e.Row.FindControl("lblNumbers")).Text;
            string[] numlist = fileData.Split(new string[] { "<br/>" }, StringSplitOptions.RemoveEmptyEntries);
            decimal sum = 0;
            string sumString = string.Empty;
            int dataCount = 0;
            if (null != numlist)
            {
                int i = 0;
                decimal prevNumber = 0;
                bool dontSkip = false;
                        
                foreach (string item in numlist)
                {
                    if (i != 0)
                    {
                        if (Convert.ToDecimal(item) > prevNumber && !dontSkip)
                        {
                            dataCount = 1;
                            sum = Convert.ToDecimal(item);
                            sumString = item;
                        }
                        else
                        {
                            if (!dontSkip)
                                ((Label)e.Row.FindControl("lblStart")).Text = string.Format("Started From {0}", prevNumber);
                            dontSkip = true;
                            sum += Convert.ToDecimal(item);
                            dataCount++;
                            sumString += "+" + item;
                        }
                    }
                    i++;
                    prevNumber = Convert.ToDecimal(item);
                }
    
            }
            Label lblTotal = (Label)e.Row.FindControl("lblTotal");
            lblTotal.Text = string.Format("Total: {0}", sum.ToString());
            Label lblCount = (Label)e.Row.FindControl("lblCount");
            lblCount.Text = string.Format("Sum of: {0}", sumString);
            Label lblDataCount = (Label)e.Row.FindControl("lblDataCount");
            lblDataCount.Text = string.Format("Count: {0}", dataCount);
        }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 6, 2017 1:55 PM
  • User-1509636757 posted
    <asp:TemplateField HeaderText="Sample Col">
        <ItemTemplate>
            <asp:Label ID="lblNumbers" runat="server" Text='<%# Eval("Numbers") %>'></asp:Label>
            <br />
            <asp:Label ID="lblStart" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblCount" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblDataCount" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblTotal" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblAverage" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string fileData = ((Label)e.Row.FindControl("lblNumbers")).Text;
            string[] numlist = fileData.Split(new string[] { "<br/>" }, StringSplitOptions.RemoveEmptyEntries);
            decimal sum = 0;
            string sumString = string.Empty;
            int dataCount = 0;
            if (null != numlist)
            {
                int i = 0;
                decimal prevNumber = 0;
                bool dontSkip = false;
    
                foreach (string item in numlist)
                {
                    if (i != 0)
                    {
                        if (Convert.ToDecimal(item) > prevNumber && !dontSkip)
                        {
                            dataCount = 1;
                            sum = Convert.ToDecimal(item);
                            sumString = item;
                        }
                        else
                        {
                            if (!dontSkip)
                                ((Label)e.Row.FindControl("lblStart")).Text = string.Format("Started From {0}", prevNumber);
                            dontSkip = true;
                            sum += Convert.ToDecimal(item);
                            dataCount++;
                            sumString += "+" + item;
                        }
                    }
                    i++;
                    prevNumber = Convert.ToDecimal(item);
                }
            }
            Label lblTotal = (Label)e.Row.FindControl("lblTotal");
            lblTotal.Text = string.Format("Total: {0}", sum.ToString());
            Label lblCount = (Label)e.Row.FindControl("lblCount");
            lblCount.Text = string.Format("Sum of: {0}", sumString);
            Label lblDataCount = (Label)e.Row.FindControl("lblDataCount");
            lblDataCount.Text = string.Format("Count: {0}", dataCount);
            Label lblAverage = (Label)e.Row.FindControl("lblAverage");
            lblAverage.Text = string.Format("Average: {0}", decimal.Round(sum / dataCount, 2));
        }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 6, 2017 5:20 PM

All replies

  • User-1509636757 posted

    What difference it will make if you start totalling from highest value rather than totalling in sequential way?

    Thursday, April 6, 2017 12:47 PM
  • User2033107836 posted

    Actually it one report that client need.. so i need to start totalling from first highest values

    Thursday, April 6, 2017 3:30 PM
  • User-1509636757 posted

    I am not sure if I understood the question. If suppose I take this as example:

    5,8,2,1,6,4

    If I total this series start from highest 8+6+5+4+2+1=26 or I total them as it is 5+8+2+1+6+4=26.. answer is still 26. So, question is, what will it make the difference in application to start totalling from first highest value?

    Thursday, April 6, 2017 5:08 PM
  • User2033107836 posted

    For example column look like this

    7

    2

    13

    9

    4

    2

    15

    1

    here 13 is highest value in ascending i want to add  13+9+4+2+15+1 = 44

    Thursday, April 6, 2017 5:20 PM
  • User-271186128 posted

    Hi asp.ambur,

    For example column look like this

    7

    2

    13

    9

    4

    2

    15

    1

    here 13 is highest value in ascending i want to add  13+9+4+2+15+1 = 44

    Based on your description, I suggest you could refer to the following code:

            <asp:GridView ID="GridView2"
                 OnRowDataBound="GridView2_RowDataBound"
                 AutoGenerateColumns="false" runat="server">
                <Columns>
                    <asp:TemplateField HeaderText="Data">
                        <ItemTemplate>
                            <asp:Label ID="lblData" runat="server" Text='<%# Eval("Data") %>'></asp:Label>
                            <br />
                            Total: <asp:Label ID="lblTotal" runat="server" Text=""></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

    Code behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Data", typeof(string)) });
                    dt.Rows.Add("7<br/>2<br/>13<br/>9<br/>4<br/>2<br/>15<br/>1");
                    GridView2.DataSource = dt;
                    GridView2.DataBind();
                }
            }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { string fileData = ((Label)e.Row.FindControl("lblData")).Text; //find the data and convert it to int list. You could also convert it to double type. List<int> numlist = fileData.Split(new string[] { "<br/>" }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToList();
    //List<double> numlist = fileData.Split(new string[] { "<br/>" }, StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToList();

    //sort the list numlist = numlist.OrderByDescending(c => c).ToList(); Label lblTotal = (Label)e.Row.FindControl("lblTotal");
    //using the Sum() method to get the total number. lblTotal.Text = string.Format("{0}={1}", string.Join("+", numlist), numlist.Sum().ToString()); } }

    The output as below:

    Best regards,
    Dillion

    Tuesday, April 11, 2017 8:16 AM
  • User2033107836 posted

    7

    2

    Should not come

    Tuesday, April 11, 2017 8:52 AM
  • User-271186128 posted

    Hi asp.ambur,

    7

    2

    Should not come

    Based on which condition you will remove this items? In the RowDataBound event, you could remove item from the list using the remove method.

    http://www.c-sharpcorner.com/UploadFile/mahesh/how-to-remove-an-item-from-a-C-Sharp-list/

    Best regards,
    Dillion

    Wednesday, April 12, 2017 9:14 AM
  • User2033107836 posted

    Let Me Tell You Clear

    I have gridview in which i have one column "Data"

    The first row has value like this below

    2.3

    4

    5

    6

    5.4

    2

    1.6

    1

    Second row

    4.6

    5.4

    10.5

    11.5

    14

    10

    9

    5

    1

    This is my code

    if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string fileData = e.Row.Cells[6].Text.Replace("&quote;", "").Replace("</br>", "").Trim(); 
                string[] splittedFileData = fileData.Split(new string[] { "'" }, StringSplitOptions.RemoveEmptyEntries);
                if (null != splittedFileData && splittedFileData.Length > 0)
                {
                    double total = 0;
                  
                    foreach (string data in splittedFileData)
                    {
                        double tempResult = 0;
                        if (double.TryParse(data.Trim(), out tempResult))
                            total += tempResult;
                    }
                    //-- display total in a template field column with lblTotal as Label control
                    (e.Row.FindControl("lblTotal") as System.Web.UI.WebControls.Label).Text = total.ToString("##.###");
    
                    //-- display no of data in a template field column with lblNoOfData as Label control
                    (e.Row.FindControl("lblNoOfData") as System.Web.UI.WebControls.Label).Text = splittedFileData.Length.ToString();
    
                    double kk = total / splittedFileData.Length;
                    //-- display avarage in a template field column with lblAverageData as Label control
                    (e.Row.FindControl("lblAverageData") as System.Web.UI.WebControls.Label).Text = (kk / 9.81).ToString("##.###");
                }
            }

    **** I want the sum to be start from highest value that is

    First row total : 16

    second row total : 39

    Tuesday, June 6, 2017 10:51 AM
  • User-1509636757 posted

    Can you clarify what is the logic of having sum 16 and 39? do you mean to take sum of only first 5 highest numbers?

    Tuesday, June 6, 2017 11:05 AM
  • User2033107836 posted

    see first row value

    2.3      4               5       6          5.4       2   1.6     1

    here 2.3 next 4 and next 5 and next 6 then after 6 it goes descending  5.4    2     1.6     1

    here 6 is the highest value when we see from start ascending order so i start to calculate sum from 6 to last data...

    6 + 5.4 + 2 + 1.6 + 1 = 16

    Tuesday, June 6, 2017 11:23 AM
  • User2033107836 posted

    Let us assume Third row value

    1     3    6     9    7        5.5         4         3.5        2        1.5          0.5

    here when we see in ascending 9 is highest after that descending  starts 

    Total will be  9 + 7 + 5.5 + 4 + 3.5 + 2 + 1.5 + 0.5 =  33

    Tuesday, June 6, 2017 11:49 AM
  • User-1509636757 posted

    I have modified Dillon's code to get the expected output further as:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="GridView1"
                 OnRowDataBound="GridView1_RowDataBound"
                 AutoGenerateColumns="false" runat="server">
                <Columns>
                    <asp:TemplateField HeaderText="Sample Col">
                        <ItemTemplate>
                            <asp:Label ID="lblNumbers" runat="server" Text='<%# Eval("Numbers") %>'></asp:Label>
                            <br />
                            <asp:Label ID="lblTotal" runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </form>
    </body>
    </html>
    public partial class WebForm202 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindGridView();
        }
    
        private void BindGridView()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Numbers"));
            dt.Rows.Add("2.3<br/>4<br/>5<br/>6<br/>5.4<br/>2<br/>1.6<br/>1");
            dt.Rows.Add("4.6<br/>5.4<br/>10.5<br/>11.5<br/>14<br/>10<br/>9<br/>5<br/>1");
            dt.Rows.Add("1<br/>3<br/>6<br/>9<br/>7<br/>5.5<br/>4<br/>3.5<br/>2<br/>1.5<br/>0.5");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string fileData = ((Label)e.Row.FindControl("lblNumbers")).Text;
                List<decimal> numlist = fileData.Split(new string[] { "<br/>" }, StringSplitOptions.RemoveEmptyEntries).Select(decimal.Parse).ToList();
                decimal sum = 0;
                if (null != numlist)
                {
                    int i = 0;
                    decimal prevNumber = 0;
                    bool dontSkip = false;
                    foreach (decimal item in numlist)
                    {
                        if (i != 0)
                        {
                            if (item > prevNumber && !dontSkip)
                            {
                                sum = item;
                            }
                            else
                            {
                                dontSkip = true;
                                sum += item;
                            }
                        }
                        i++;
                        prevNumber = item;
                    }
                }
                Label lblTotal = (Label)e.Row.FindControl("lblTotal");
                lblTotal.Text = string.Format("Total: {0}", sum.ToString());
            }
        }
    }



    Tuesday, June 6, 2017 11:59 AM
  • User2033107836 posted

    error im using asp.net 2.0

    Compiler Error Message: CS0117: 'System.Array' does not contain a definition for 'Select'

    Source Error:

    Line 268:
    Line 269:        string zfileData = (e.Row.FindControl("lblTotal") as System.Web.UI.WebControls.Label).Text;
    Line 270:        List<decimal> numlist = zfileData.Split(new string[] { "<br/>" }, StringSplitOptions.RemoveEmptyEntries).Select(decimal.Parse).ToList();
    Line 271:        decimal sum = 0;
    Line 272:        if (null != numlist)
    Tuesday, June 6, 2017 12:16 PM
  • User-1509636757 posted

    error im using asp.net 2.0

    Try not to use LinQ methods like Select() in above code. Here is modification required:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string fileData = ((Label)e.Row.FindControl("lblNumbers")).Text;
            List<string> numlist = fileData.Split(new string[] { "<br/>" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            decimal sum = 0;
            if (null != numlist)
            {
                int i = 0;
                decimal prevNumber = 0;
                bool dontSkip = false;
                foreach (string item in numlist)
                {
                    if (i != 0)
                    {
                        if (Convert.ToDecimal(item) > prevNumber && !dontSkip)
                        {
                            sum = Convert.ToDecimal(item);
                        }
                        else
                        {
                            dontSkip = true;
                            sum += Convert.ToDecimal(item);
                        }
                    }
                    i++;
                    prevNumber = Convert.ToDecimal(item);
                }
            }
            Label lblTotal = (Label)e.Row.FindControl("lblTotal");
            lblTotal.Text = string.Format("Total: {0}", sum.ToString());
        }
    }

    Tuesday, June 6, 2017 12:41 PM
  • User2033107836 posted

    error

    Compiler Error Message: CS0117: 'System.Array' does not contain a definition for 'ToList'

    Source Error:

    Line 268:
    Line 269:        string zfileData = e.Row.Cells[6].Text;
    Line 270:        List<string> numlist = zfileData.Split(new string[] { "<br/>" }, StringSplitOptions.RemoveEmptyEntries).ToList();
    Line 271:        decimal sum = 0;
    Line 272:        if (null != numlist)
    Tuesday, June 6, 2017 12:45 PM
  • User-1509636757 posted

    I did not noticed that :( since it is also a differed method in LinQ you cannot use it in 2.0. use Array instead:

    string[] numlist = fileData.Split(new string[] { "<br/>" }, StringSplitOptions.RemoveEmptyEntries);



    Tuesday, June 6, 2017 1:23 PM
  • User2033107836 posted

    I really thanks to you Mr.Kaushal

    Can i see the first highest value in another label so that user can know its the start of sum

    Tuesday, June 6, 2017 1:27 PM
  • User2033107836 posted

    I need two things the code works fine

    1) Highest value start number to show in another label

    2) from highest till end no of data count in another label

    Tuesday, June 6, 2017 1:30 PM
  • User-1509636757 posted

    1) Highest value start number to show in another label

    2) from highest till end no of data count in another label

    You can still use same code to get the first highest and sum numbers.

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="GridView1"
                 OnRowDataBound="GridView1_RowDataBound"
                 AutoGenerateColumns="false" runat="server">
                <Columns>
                    <asp:TemplateField HeaderText="Sample Col">
                        <ItemTemplate>
                            <asp:Label ID="lblNumbers" runat="server" Text='<%# Eval("Numbers") %>'></asp:Label>
                            <br />
                            <asp:Label ID="lblStart" runat="server"></asp:Label>
                            <br />
                            <asp:Label ID="lblCount" runat="server"></asp:Label>
                            <br />
                            <asp:Label ID="lblTotal" runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </form>
    </body>
    </html>
        public partial class WebForm202 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                    BindGridView();
            }
    
            private void BindGridView()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("Numbers"));
                dt.Rows.Add("2.3<br/>4<br/>5<br/>6<br/>5.4<br/>2<br/>1.6<br/>1");
                dt.Rows.Add("4.6<br/>5.4<br/>10.5<br/>11.5<br/>14<br/>10<br/>9<br/>5<br/>1");
                dt.Rows.Add("1<br/>3<br/>6<br/>9<br/>7<br/>5.5<br/>4<br/>3.5<br/>2<br/>1.5<br/>0.5");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string fileData = ((Label)e.Row.FindControl("lblNumbers")).Text;
                    string[] numlist = fileData.Split(new string[] { "<br/>" }, StringSplitOptions.RemoveEmptyEntries);
                    decimal sum = 0;
                    string sumString = string.Empty;
                    if (null != numlist)
                    {
                        int i = 0;
                        decimal prevNumber = 0;
                        bool dontSkip = false;
                        
                        foreach (string item in numlist)
                        {
                            if (i != 0)
                            {
                                if (Convert.ToDecimal(item) > prevNumber && !dontSkip)
                                {
                                    sum = Convert.ToDecimal(item);
                                    sumString = item;
                                }
                                else
                                {
                                    if (!dontSkip)
                                        ((Label)e.Row.FindControl("lblStart")).Text = string.Format("Started From {0}", prevNumber);
                                    dontSkip = true;
                                    sum += Convert.ToDecimal(item);
                                    sumString += "+" + item;
                                }
                            }
                            i++;
                            prevNumber = Convert.ToDecimal(item);
                        }
                    }
                    Label lblTotal = (Label)e.Row.FindControl("lblTotal");
                    lblTotal.Text = string.Format("Total: {0}", sum.ToString());
                    Label lblCount = (Label)e.Row.FindControl("lblCount");
                    lblCount.Text = string.Format("Sum of: {0}", sumString);
                }
            }
        }



    Tuesday, June 6, 2017 1:41 PM
  • User2033107836 posted

    Thanks Lot Its Works

    Last Requirements Is 

    how to show from highest to till end no of data count in another label

    First Row No Of Data Count  : 5

    Second Row No Of Data Count : 5

    Third Row No Of Data Count : 8

    Tuesday, June 6, 2017 1:48 PM
  • User-1509636757 posted

    Did you try out yourself? It is fairly easy; just use a counter where sum is being done.

    <asp:TemplateField HeaderText="Sample Col">
        <ItemTemplate>
            <asp:Label ID="lblNumbers" runat="server" Text='<%# Eval("Numbers") %>'></asp:Label>
            <br />
            <asp:Label ID="lblStart" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblCount" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblDataCount" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblTotal" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string fileData = ((Label)e.Row.FindControl("lblNumbers")).Text;
            string[] numlist = fileData.Split(new string[] { "<br/>" }, StringSplitOptions.RemoveEmptyEntries);
            decimal sum = 0;
            string sumString = string.Empty;
            int dataCount = 0;
            if (null != numlist)
            {
                int i = 0;
                decimal prevNumber = 0;
                bool dontSkip = false;
                        
                foreach (string item in numlist)
                {
                    if (i != 0)
                    {
                        if (Convert.ToDecimal(item) > prevNumber && !dontSkip)
                        {
                            dataCount = 1;
                            sum = Convert.ToDecimal(item);
                            sumString = item;
                        }
                        else
                        {
                            if (!dontSkip)
                                ((Label)e.Row.FindControl("lblStart")).Text = string.Format("Started From {0}", prevNumber);
                            dontSkip = true;
                            sum += Convert.ToDecimal(item);
                            dataCount++;
                            sumString += "+" + item;
                        }
                    }
                    i++;
                    prevNumber = Convert.ToDecimal(item);
                }
    
            }
            Label lblTotal = (Label)e.Row.FindControl("lblTotal");
            lblTotal.Text = string.Format("Total: {0}", sum.ToString());
            Label lblCount = (Label)e.Row.FindControl("lblCount");
            lblCount.Text = string.Format("Sum of: {0}", sumString);
            Label lblDataCount = (Label)e.Row.FindControl("lblDataCount");
            lblDataCount.Text = string.Format("Count: {0}", dataCount);
        }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 6, 2017 1:55 PM
  • User2033107836 posted

    Thanks its works fine
    Last very last

    I need lblTotal / lblDataCount to show average value
    how would be the code

    Tuesday, June 6, 2017 2:22 PM
  • User2033107836 posted

    First Row 16 /  5 = 3.2

    Second Row 14 / 5 = 2.8

    Third Row 33/ 8 = 4.12

    i need last value like this

    Tuesday, June 6, 2017 2:35 PM
  • User-1509636757 posted
    <asp:TemplateField HeaderText="Sample Col">
        <ItemTemplate>
            <asp:Label ID="lblNumbers" runat="server" Text='<%# Eval("Numbers") %>'></asp:Label>
            <br />
            <asp:Label ID="lblStart" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblCount" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblDataCount" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblTotal" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblAverage" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string fileData = ((Label)e.Row.FindControl("lblNumbers")).Text;
            string[] numlist = fileData.Split(new string[] { "<br/>" }, StringSplitOptions.RemoveEmptyEntries);
            decimal sum = 0;
            string sumString = string.Empty;
            int dataCount = 0;
            if (null != numlist)
            {
                int i = 0;
                decimal prevNumber = 0;
                bool dontSkip = false;
    
                foreach (string item in numlist)
                {
                    if (i != 0)
                    {
                        if (Convert.ToDecimal(item) > prevNumber && !dontSkip)
                        {
                            dataCount = 1;
                            sum = Convert.ToDecimal(item);
                            sumString = item;
                        }
                        else
                        {
                            if (!dontSkip)
                                ((Label)e.Row.FindControl("lblStart")).Text = string.Format("Started From {0}", prevNumber);
                            dontSkip = true;
                            sum += Convert.ToDecimal(item);
                            dataCount++;
                            sumString += "+" + item;
                        }
                    }
                    i++;
                    prevNumber = Convert.ToDecimal(item);
                }
            }
            Label lblTotal = (Label)e.Row.FindControl("lblTotal");
            lblTotal.Text = string.Format("Total: {0}", sum.ToString());
            Label lblCount = (Label)e.Row.FindControl("lblCount");
            lblCount.Text = string.Format("Sum of: {0}", sumString);
            Label lblDataCount = (Label)e.Row.FindControl("lblDataCount");
            lblDataCount.Text = string.Format("Count: {0}", dataCount);
            Label lblAverage = (Label)e.Row.FindControl("lblAverage");
            lblAverage.Text = string.Format("Average: {0}", decimal.Round(sum / dataCount, 2));
        }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 6, 2017 5:20 PM
  • User2033107836 posted

    Thank You So Much @Kushal

    Code Works Fine

    Tuesday, June 6, 2017 10:49 PM