10 replies

Last post Jul 14, 2017 10:32 AM by asp.ambur

• asp.ambur

Member

399 Points

1101 Posts

how to calculate before last value

Hello

This is my data

2.4
2.6
2.8
3.2
3.4
4.5
8.5
9.6
9.9
10.5
10.8
11.5
4.9
7.6
2.6
1.5
3.6
2.5
6.4
5.5
3.8
1.2
1.2
1.2
1.2

This my code

```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)
e.Row.Cells[9].Text = string.Format("{0}", prevNumber);
dontSkip = true;
sum += Convert.ToDecimal(item);
dataCount++;
sumString += "+" + item;
}
}
i++;
prevNumber = Convert.ToDecimal(item);
}
}```

The above code is calculating from highest to till end sum and no of records..

But i want to sum from highest to till before repeat values and no of records like thos below

11.5
4.9
7.6
2.6
1.5
3.6
2.5
6.4
5.5
3.8

before 1.2 coz from 1.2 to its keep o coming same data so left that values

sum = 49.9

No of records = 10

please give me the code based on my code..

Thank You

• Cathy Zou

Star

8660 Points

2882 Posts

Re: how to calculate before last value

Jul 11, 2017 02:25 AM|Cathy Zou|LINK

Hi asp.ambur,

From your description, you would sum from hightest to till before the repeat values.

If so, you could refer to the following code:

```protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindGridView();
}
private void BindGridView()
{
DataTable dt = new DataTable();
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;
int dataCount = 0;
if (null != numlist)
{
int i = 0;
decimal prevNumber = 0;
bool dontSkip = false;
for (int j=0; j < numlist.Length; j++)
{
if (i != 0)
{
if (Convert.ToDecimal(numlist[j]) > prevNumber && !dontSkip)
{
dataCount = 1;
sum = Convert.ToDecimal(numlist[j]);
sumString = numlist[j];
}
else
{
if (!dontSkip)
((Label)e.Row.FindControl("lblStart")).Text = string.Format("Started From {0}", prevNumber);
dontSkip = true;
if (j == numlist.Length - 1)
{
if (!numlist[j].Equals(numlist[j - 1]))
{
sum += Convert.ToDecimal(numlist[j]);
dataCount++;
sumString += "+" + numlist[j];
}
}
else {
if (!numlist[j].Equals(numlist[j -1]) && !numlist[j].Equals(numlist[j + 1]))
{
sum += Convert.ToDecimal(numlist[j]);
dataCount++;
sumString += "+" + numlist[j];
}
}
}
}
i++;
prevNumber = Convert.ToDecimal(numlist[j]);
}
}
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);
}
}```

Output screenshot as below:

Best regards

Cathy

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• asp.ambur

Member

399 Points

1101 Posts

Re: how to calculate before last value

Hello Cathy Zou

Can you show end value in another label

you have shown started from perfect

i need end to  show in another label

• Cathy Zou

Star

8660 Points

2882 Posts

Re: how to calculate before last value

Jul 12, 2017 01:30 AM|Cathy Zou|LINK

Hi asp.ambur，

From your description, you would show ended value in another label, and then you could refer to it:

```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;
decimal endNumber = 0;
bool dontSkip = false;

for (int j=0; j < numlist.Length; j++)
{
if (i != 0)
{
if (Convert.ToDecimal(numlist[j]) > prevNumber && !dontSkip)
{
dataCount = 1;
sum = Convert.ToDecimal(numlist[j]);
sumString = numlist[j];
}
else
{
if (!dontSkip)
((Label)e.Row.FindControl("lblStart")).Text = string.Format("Started From {0}", prevNumber);
dontSkip = true;
if (j == numlist.Length - 1)
{
if (!numlist[j].Equals(numlist[j - 1]))
{
sum += Convert.ToDecimal(numlist[j]);
dataCount++;
sumString += "+" + numlist[j];

}
}
else
{
if (!numlist[j].Equals(numlist[j - 1]) && !numlist[j].Equals(numlist[j + 1]))
{
sum += Convert.ToDecimal(numlist[j]);
dataCount++;
sumString += "+" + numlist[j];
endNumber = Convert.ToDecimal(numlist[j]);
}
}
}
}
i++;
prevNumber = Convert.ToDecimal(numlist[j]);
((Label)e.Row.FindControl("lblEnd")).Text = string.Format("Ended with {0}", endNumber);
}
}
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);
}
}```

Output screenshot as below:

Best regards

Cathy

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• asp.ambur

Member

399 Points

1101 Posts

Re: how to calculate before last value

hello @Cathy Zou

if my data is like this below

11.5
4.9
7.6
2.6
1.5
3.6
2.5
6.4
7.4

7.4

5.5
3.8

If two values comes same thenits skiping calculation why 7.4, 7.4

• asp.ambur

Member

399 Points

1101 Posts

Re: how to calculate before last value

One more In center its possible to come repeat values

For example 7.4 7.4 has to add coz its is coming in between 11.5 to 3.8 it has to add in calculation..

Thanks

• Cathy Zou

Star

8660 Points

2882 Posts

Re: how to calculate before last value

Jul 14, 2017 01:39 AM|Cathy Zou|LINK

Hi asp.ambur,

From your description, you would sum from highest to till before repeat values.

However, at the same time you would calculate the repeat num between highest and till.

I am not sure I get the idea about what you want, please tell me more detail.

You could refer to the following code firstly:

```if (null != numlist)
{
int i = 0;
decimal prevNumber = 0;
decimal endNumber = 0;
bool dontSkip = false;
for (int j = 0; j < numlist.Length; j++)
{
if (i != 0)
{
if (Convert.ToDecimal(numlist[j]) > prevNumber && !dontSkip)
{
dataCount = 1;
sum = Convert.ToDecimal(numlist[j]);
sumString = numlist[j];
}
else
{
if (!dontSkip)
((Label)e.Row.FindControl("lblStart")).Text = string.Format("Started From {0}", prevNumber);
dontSkip = true;
if (j == numlist.Length - 1)
{
if (!numlist[j].Equals(numlist[j - 1]))
{
sum += Convert.ToDecimal(numlist[j]);
dataCount++;
sumString += "+" + numlist[j];
}
}
else
{
if (!numlist[j].Equals(numlist[j - 1]) || !numlist[j].Equals(numlist[j + 1]))
{
sum += Convert.ToDecimal(numlist[j]);
dataCount++;
sumString += "+" + numlist[j];
endNumber = Convert.ToDecimal(numlist[j]);
((Label)e.Row.FindControl("lblEnd")).Text = string.Format("Ended with {0}", endNumber);
}
else if (numlist[j].Equals(numlist[j - 1]) && numlist[j].Equals(numlist[j + 1]))
{
sum -= Convert.ToDecimal(numlist[j]);
dataCount--;
sumString += "-" + numlist[j];
endNumber = Convert.ToDecimal(numlist[j - 2]);
((Label)e.Row.FindControl("lblEnd")).Text = string.Format("Ended with {0}", endNumber);
break;
}
}

}
}
i++;
prevNumber = Convert.ToDecimal(numlist[j]);

}
}```

Output screenshot as below:

Best regards

Cathy

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• asp.ambur

Member

399 Points

1101 Posts

Re: how to calculate before last value

End value is 3.8 is perfect after that I don't want to add coz 1.2 1.2 1.2 1.2 is the very lowest and its repeated so I want to calculate Till lowest before repeat that is upto 3.8 after that I don't want to calculate coz it's the last lowest value reapat
• asp.ambur

Member

399 Points

1101 Posts

Re: how to calculate before last value

From higest value 11.5 to till lowest end value 3.8 in between any value comes repeated it has to calculate coz after 3.8. The data is reapting 3 or 4 times and its last value from our higest to lowest so we are skipping 1.2 1.2 1.2 1.2
• Cathy Zou

Star

8660 Points

2882 Posts

Re: how to calculate before last value

Jul 14, 2017 09:48 AM|Cathy Zou|LINK

Hi asp.ambur,

Best regards

Cathy

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• asp.ambur

Member

399 Points

1101 Posts

Re: how to calculate before last value

hello cathy

 ```Line 246: nums[i] = decimal.Parse(numlist[i]); Line 247: } Line 248: decimal maxValue = nums.Max();```