Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to Sum values String Values of percents{1/8, 2/8, 3/8, 4/8, 5/8, 6/8, 7/8, 8/8} which are strings. And I want to sum percent's in days. Only in this same day. I write a code, but it isn't work:

        foreach (SPListItem olistaDniPrzeznaczonychNaProjekt in listaDniPrzeznaczonychNaProjekt)
        {

            e.Cell.Style.Add("background-color", "#F5BCA9");
            e.Cell.CssClass = "dni_swiateczne";
            e.Cell.Controls.Add(new LiteralControl(("<p style='color:blue; font-size:8px;'>" + olistaDniPrzeznaczonychNaProjekt["Kontrakt"].ToString() + "</p><p style='color:#5D198E;'>" + olistaDniPrzeznaczonychNaProjekt["Procenty"].ToString().Substring(3) + "</p>")));
            if(e.Day.Date.Date.ToUniversalTime() == e.Day.Date.Date.ToUniversalTime()){
                sumaProcentow += olistaDniPrzeznaczonychNaProjekt["Procenty"].ToString().Substring(3);
                e.Cell.Controls.Add(new LiteralControl(("Suma = " + sumaProcentow)));
            }

Always add values from early dates. And it is "Suma = 4/84/84/84/85/83/83/83/83/83/8", And I only want to sum in one day. enter image description here

share|improve this question
    
I want only sum percent's in this the same day. –  Grzegorz Z Oct 23 '12 at 10:23
    
If we look for examle 30 day, so we have got 3/8 and 3/8, andthat is 6/8 Sum, so I want to write me "Suma = 6/8" –  Grzegorz Z Oct 23 '12 at 10:26
add comment

3 Answers

up vote 1 down vote accepted

Grzegorz,

You can use SPQuery to get all the items for that day... and then loop through the items and sum them up!

SPQuery query = new SPQuery();
query = "<Where><Eq><FieldRef Name='EventDate' /><Value Type='DateTime'>" + e.Day.Date.ToShortDateString() + "</Value></Eq></Where>";
SPListItemCollection itemColl = calendarList.GetItems(query);
int NumeratorSum = 0, DenominatorSum = 0;
foreach(SPListItem item in itemColl)
{
    NumeratorSum += int.Parse(item["Procenty"].ToString().Split('/')[0]);
    DenominatorSum += int.Parse(item["Procenty"].ToString().Split('/')[1]);
}

e.Cell.Controls.Add(new LiteralControl(("Suma = " + NumeratorSum.ToString() + "/" + DenominatorSum.ToString())));
share|improve this answer
    
It can't uderstand this line. ` "<Where><Eq><FieldRef Name='EventDate' /><Value Type='DateTime'>" + e.Day.Date.ToShortDateString() + "</Value></Eq></Where>";` A lot of errors come –  Grzegorz Z Oct 23 '12 at 11:22
    
There schould be query.Query? –  Grzegorz Z Oct 23 '12 at 11:37
    
Now it's working great thanks :) :) :) –  Grzegorz Z Oct 23 '12 at 11:39
1  
yes query.Query - just a typo mistake :).. I am glad it worked! –  Arsalan Adam Khatri Oct 23 '12 at 11:55
    
I'm glad too :) :) –  Grzegorz Z Oct 23 '12 at 12:47
add comment

Whole Code luks like:

      foreach (SPListItem olistaDniPrzeznaczonychNaProjekt in listaDniPrzeznaczonychNaProjekt)
            {

                e.Cell.Style.Add("background-color", "#F5BCA9");
                e.Cell.CssClass = "dni_swiateczne";
                e.Cell.Controls.Add(new LiteralControl(("<p style='color:blue; font-size:8px;'>" + olistaDniPrzeznaczonychNaProjekt["Kontrakt"].ToString() + "</p><p style='color:#5D198E;'>" + olistaDniPrzeznaczonychNaProjekt["Procenty"].ToString().Substring(3) + "</p>")));

                var query = new SPQuery();
                query.Query = "<Where><Eq><FieldRef Name='Dzien' /><Value Type='DateTime'>" + e.Day.Date.ToShortDateString() + "</Value></Eq></Where>";
                SPListItemCollection itemColl = witryna.Lists["Szczegoly"].GetItems(query);
                int sumaProcentow = 0;
                foreach (SPListItem item in itemColl)
                {
                    sumaProcentow += int.Parse(olistaDniPrzeznaczonychNaProjekt["Procenty"].ToString().Split('/')[1]);
                }

                e.Cell.Controls.Add(new LiteralControl(("Suma = " + sumaProcentow.ToString())));

            }

Thanks Arsalan Adam Khatri

share|improve this answer
    
Only I have got problem with Split('/'), because I want to show all number, not only 8. All number this is "4/8" –  Grzegorz Z Oct 23 '12 at 12:22
add comment

I done this like this, whole Code:

 // Gdy nie ma zaznaczonej osoby to wtedy wyświetlamy wszystkie daty urlopów 
            foreach (SPListItem olistaDniPrzeznaczonychNaProjekt in listaDniPrzeznaczonychNaProjekt)
            {

                e.Cell.Style.Add("background-color", "#F5BCA9");
                e.Cell.CssClass = "dni_swiateczne";
                e.Cell.Controls.Add(new LiteralControl(("<p style='color:blue; font-size:8px;'>" + olistaDniPrzeznaczonychNaProjekt["Kontrakt"].ToString() + "</p><p style='color:#5D198E;'>" + olistaDniPrzeznaczonychNaProjekt["Procenty"].ToString().Substring(3) + "</p>")));

                var query = new SPQuery();
                query.Query = "<Where><Eq><FieldRef Name='Dzien' /><Value Type='DateTime'>" + e.Day.Date.ToShortDateString() + "</Value></Eq></Where>";
                SPListItemCollection itemColl = witryna.Lists["Szczegoly"].GetItems(query);
                int sumaProcentow = 0;
                foreach (SPListItem item in itemColl)
                {
                    int index;

                    index = olistaDniPrzeznaczonychNaProjekt["Procenty"].ToString().IndexOf("#");
                    string procentKolejny = Convert.ToString(olistaDniPrzeznaczonychNaProjekt["Procenty"]).Substring(index + 1,1);

                    sumaProcentow += int.Parse(procentKolejny);
                }

                e.Cell.Controls.Add(new LiteralControl(("Suma = " + sumaProcentow.ToString()+"/8")));

            }
        }

But now I see two times or more sum in the calendar. I must see only one time. enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.