decided to do it with event receiver and would like to share the code if any one has similar requirement. event receiver make it easy to get the sum of the column and get the percentage for each row. see the code for ItemAdded event but you would want to add the same code for ItemUpdated and ItemDeleted event so that it will reflect the update accordingly.
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
string listName = "your list name goes here";
using (SPWeb web = properties.OpenWeb())
{
if (web.Lists[listName] != null)
{
try
{
SPList splist = web.Lists[listName];
int sum = 0;
foreach (SPListItem item in splist.Items)
{
sum = sum + Convert.ToInt32(item["Values"]);
}
//LoggingService
foreach (SPListItem item in splist.Items)
{
string percentage = string.Empty;
int percent = Convert.ToInt32(item["Values"]);/// Convert.ToInt32(sum);
double questient = (double)percent / sum;
percentage = questient.ToString("P1", CultureInfo.InvariantCulture);
item["Percent & widget"] = percentage;
this.EventFiringEnabled = false;
item.Update();
this.EventFiringEnabled = true;
}
}
catch (Exception ex)
{
// LoggingService
}
} // end if
} // end using
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.