答案 0 :(得分:0)
决定与事件接收器一起使用,并且如果任何人有相似的要求,希望共享代码。事件接收器使获取列总和和获得每一行的百分比变得容易。请参阅ItemAdded事件的代码,但您想为ItemUpdated和ItemDeleted事件添加相同的代码,以使其反映相应的更新。
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
}