I have a grid view that I need to calculate some summary columns for multiple locations however some rows are in dollars (straight addition) and others are a percentage where I need an average. In the code below the field "goCAID" if 1 is a row summed.
If goCAID is not equal to one it's a percentage. My example showes what I'm trying to accompolish.
The "Sales" row is goCAID type "1" so I am adding all the location Goals for Goal Summary and adding all the location Totals for Total Summary.
The "Percent Of Region" row is goCAID type "2" so I need to average all the location goals for Goal Summary and average all the location Totals for Total Summary.
Having issues calculating some rows different plus having to handle every other column differently (Goals & Totals)
New York
New York
Charlotte
Charlotte
Atlanta
Atlanta
Goal
Total
Goal
Total
Goal
Total
Goal
Total
Summary
Summary
Sales
$1,000
$300
$800
$600
$500
$450
$2,300
$1,350
Percent of Region
50%
40%
60%
20%
75%
50%
62%
37%
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int sum = 0;
int average = 0;
int CountG = 0;
int CountT = 0;
int goCAID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "goCAID"));
for (int i = 4; i < (e.Row.Cells.Count - 2); i++)
{
if ((!e.Row.Cells[i].Text.Equals(" ")) && (!e.Row.Cells[i].Text.Equals("")))
{
if (goCAID.Equals(1)) // Sum
{
sum += int.Parse(e.Row.Cells[i].Text, NumberStyles.AllowThousands);
}
else //Average
{
/* Calculate Goals Average */
CountG = CountG + 1;
sum += int.Parse(e.Row.Cells[i].Text, NumberStyles.AllowThousands);
average = sum / CountG;
e.Row.Cells[e.Row.Cells.Count - 3].Text = average.ToString();
/* Calculate Totals Average */
CountT = CountT + 1;
sum += int.Parse(e.Row.Cells[i].Text, NumberStyles.AllowThousands);
average = sum / CountT;
e.Row.Cells[e.Row.Cells.Count - 3].Text = average.ToString();
}
break;
}
Why are you putting all the calculations on rowdatabound? Can you get the sam egrid structure you want using SP from database? It hink this way it will lower the performance. Please post you table structure we might able to help you. Or let us know the issues
you are facing with your current method.
You didn't separate the Goal and Total for calculating the summary. They are different. In the code you should add any cells (0,2,4) for Goal summary, and cells (1,3,5) for Total summary.
bplayer2
Member
107 Points
124 Posts
Need Help With C# Logic & Syntax
May 01, 2012 01:30 PM|LINK
I have a grid view that I need to calculate some summary columns for multiple locations however some rows are in dollars (straight addition) and others are a percentage where I need an average. In the code below the field "goCAID" if 1 is a row summed. If goCAID is not equal to one it's a percentage. My example showes what I'm trying to accompolish.
The "Sales" row is goCAID type "1" so I am adding all the location Goals for Goal Summary and adding all the location Totals for Total Summary.
The "Percent Of Region" row is goCAID type "2" so I need to average all the location goals for Goal Summary and average all the location Totals for Total Summary.
Having issues calculating some rows different plus having to handle every other column differently (Goals & Totals)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { int sum = 0; int average = 0; int CountG = 0; int CountT = 0; int goCAID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "goCAID")); for (int i = 4; i < (e.Row.Cells.Count - 2); i++) { if ((!e.Row.Cells[i].Text.Equals(" ")) && (!e.Row.Cells[i].Text.Equals(""))) { if (goCAID.Equals(1)) // Sum { sum += int.Parse(e.Row.Cells[i].Text, NumberStyles.AllowThousands); } else //Average { /* Calculate Goals Average */ CountG = CountG + 1; sum += int.Parse(e.Row.Cells[i].Text, NumberStyles.AllowThousands); average = sum / CountG; e.Row.Cells[e.Row.Cells.Count - 3].Text = average.ToString(); /* Calculate Totals Average */ CountT = CountT + 1; sum += int.Parse(e.Row.Cells[i].Text, NumberStyles.AllowThousands); average = sum / CountT; e.Row.Cells[e.Row.Cells.Count - 3].Text = average.ToString(); } break; }rio.jones
Member
246 Points
53 Posts
Re: Need Help With C# Logic & Syntax
May 01, 2012 02:00 PM|LINK
Hello,
Why are you putting all the calculations on rowdatabound? Can you get the sam egrid structure you want using SP from database? It hink this way it will lower the performance. Please post you table structure we might able to help you. Or let us know the issues you are facing with your current method.
bplayer2
Member
107 Points
124 Posts
Re: Need Help With C# Logic & Syntax
May 01, 2012 02:04 PM|LINK
The current issue I am facing is the code posted above is not getting the desired results as described above. All help appreciated!
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Need Help With C# Logic & Syntax
May 03, 2012 08:29 AM|LINK
Hi,
You didn't separate the Goal and Total for calculating the summary. They are different. In the code you should add any cells (0,2,4) for Goal summary, and cells (1,3,5) for Total summary.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework