none
Grouping in a SharePoint Gridview RRS feed

  • Question

  • I've done grouping before on a SharePoint Gridview, but at that time I was just calling a view that had grouping selected.  This time I am filling a spgridview with data from a datatable since I am having to do some manipulation of the data before it is presented to the user.  Basically what I have is a list of requests that need to be grouped by the month that the request was entered into the list.  I'm using the Created Date to order the items within my SPQuery object, so everything comes out with the most recent request at the top of the gridview. 

    So, how would one group the gridview based on the month in the Created Date?

    Tuesday, February 3, 2015 9:10 PM

Answers

All replies

  • Hi,

    Please follow steps given in below blog

    http://myspexp.com/2010/10/31/grouping-with-expansioncollapse-a-gridview-in-c-and-asp-net-or-sharepoint/


    Thanks.... ________________ Baba (MCTS, MCPD)

    • Marked as answer by David Coxsey Tuesday, February 10, 2015 9:18 PM
    Wednesday, February 4, 2015 6:59 AM
  • Looked at both of those links, and they are both informative, but I don't know if they can help with my current issue.  Here is the code I'm using to populate my gridview:

    SPList DonationsLog = SPContext.Current.Web.Lists.TryGetList(DonationRequests.ListName);
                if (DonationsLog == null)
                {
                    errorMess.InnerText = "Sorry, but the Donations Log could not be loaded at this time.  Please try again or contact the IT Department Help Desk @ ext 2195.";
                    return;
                }
    
                //get the current year
                String curYear = DateTime.Now.Year.ToString();
                //now, using the current year, get the previous year
                String prevYear = DateTime.Now.AddYears(-1).Year.ToString();
    
                //now, create the start and end dates needed for the query
                string startDate = (SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime("10/01/" + prevYear)));
                string endDate = (SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime("09/30/" + curYear)));
    
                //create the query to get the donations
                SPQuery donationsQuery = new SPQuery();
                donationsQuery.Query = @"<Where><And><Geq><FieldRef Name='Created'/><Value Type='DateTime'>" + startDate +
                    @"</Value></Geq><Leq><FieldRef Name='Created'/><Value Type='DateTime'>" + endDate +
                    @"</Value></Leq></And></Where><OrderBy><FieldRef Name='Created'/></OrderBy>";
    
                //get the requests
                SPListItemCollection donationRequests = DonationsLog.GetItems(donationsQuery);
                //make sure something was returned
                if ((donationRequests == null) || (donationRequests.Count == 0))
                {
                    errorMess.InnerText = "Sorry, but the Donation Log could not be loaded.  Please try again or contact the IT Department Help Desk @ ext 2195.";
                    return;
                }
    
                //place the results into a datatable that we will create now
                DataTable donationsDT = new DataTable();
    
                //create the columns
                donationsDT.Columns.Add("updateRequest", typeof(String));
                donationsDT.Columns.Add("requestDate", typeof(String));
                donationsDT.Columns.Add("receivedDate", typeof(String));
                donationsDT.Columns.Add("formOfRequest", typeof(String));
                donationsDT.Columns.Add("requester", typeof(String));
                donationsDT.Columns.Add("description", typeof(String));
                donationsDT.Columns.Add("amountRequested", typeof(String));
                donationsDT.Columns.Add("amountDonated", typeof(String));
                donationsDT.Columns.Add("action", typeof(String));
    
                //now, add the results to the datatable
                foreach (SPListItem request in donationRequests)
                {
                    //create the data row
                    DataRow donationRow = donationsDT.NewRow();
                    donationRow["updateRequest"] = @"<a href=""#"" onclick=""javascript:UpdateRequest(" + request.ID.ToString() +
                        @""">Update Request</a>";
                    donationRow["requestDate"] = Convert.ToDateTime(request[DonationRequests.RequestDate].ToString()).ToShortDateString();
                    donationRow["receivedDate"] = Convert.ToDateTime(request["Created"].ToString()).ToShortDateString();
                    donationRow["formOfRequest"] = request[DonationRequests.FormofRequest].ToString();
                    donationRow["requester"] = request[DonationRequests.Requester].ToString();
                    donationRow["description"] = request[DonationRequests.Descript].ToString();
                    donationRow["amountRequested"] = "$" + request[DonationRequests.RequestAmount].ToString();
                    if (request[DonationRequests.DonatedAmount] == null)
                        donationRow["amountDonated"] = string.Empty;
                    else
                        donationRow["amountDonated"] = "$" + request[DonationRequests.DonatedAmount].ToString();
                    donationRow["action"] = request[DonationRequests.Action].ToString();
                    //add row to the datatable
                    donationsDT.Rows.Add(donationRow);
                }
    
                //now, bind to the gridview
                spgvDonations.DataSource = null;
                spgvDonations.DataBind();
                spgvDonations.DataSource = donationsDT;
                spgvDonations.DataBind();

    What I need to do is take the column "requestDate", get the month from it, and group on that.

    Thursday, February 5, 2015 3:49 PM