Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, March 19, 2012

SpGridView GroupBy Sample within a User Control.


ascx
 <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SpGridViewGroupByUserControl.ascx.cs" Inherits="SpGridViewGroupBy.SpGridViewGroupBy.SpGridViewGroupByUserControl" %>
<script type="text/javascript">
    function ShowCollapsed()
    {
        var rows = document.getElementsByTagName('tr');
        var numRows = rows.length;
        for (var i = 0; i < numRows; i++)
        {
            if (rows[i].getAttribute("isexp") != null && rows[i].getAttribute("isexp").toLowerCase() == "true")
            {
                if (rows[i].firstChild.firstChild != null && rows[i].firstChild.firstChild.tagName.toLowerCase() == "a")
                {
                    if (rows[i].firstChild.firstChild.title.toLowerCase() != "expand/collapse")
                    {
                        rows[i].firstChild.firstChild.fireEvent("onclick");
                    }
                }
            }

        }
    }
    _spBodyOnLoadFunctionNames.push("ShowCollapsed");
</script>
<SharePoint:SPGridView ID="sgvGroupBy" runat="server" AutoGenerateColumns="false" EnableViewState="false">
<Columns>
<asp:BoundField HeaderText="Title" DataField="Title" />
<asp:BoundField HeaderText="Priority" DataField="Priority"  />
<asp:BoundField HeaderText="% Complete" DataField="PercentComplete"  />
</Columns>
</SharePoint:SPGridView>
<asp:Label ID="lblMessage" runat="server"></asp:Label>


Ascx.cs Page Load
try
            {
                sgvGroupBy.AllowGrouping = true;
                sgvGroupBy.AllowGroupCollapse = true;
                sgvGroupBy.GroupFieldDisplayName = "Status";
                sgvGroupBy.GroupField = "Status";
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb objWeb = SPContext.Current.Web;
                    SPList lst = objWeb.Lists["Tasks"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='Status' Ascending='True' /></OrderBy>";
                    sQuery.ViewFields = "<FieldRef Name='Status'  /><FieldRef Name='Title'  /><FieldRef Name='Priority'  /><FieldRef Name='PercentComplete'  />";
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        sgvGroupBy.DataSource = myColl.GetDataTable();
                        sgvGroupBy.DataBind();
                    }

                });
            }
            catch (Exception ex)
            {

                lblMessage.Text = ex.ToString();
            }


No comments:

Post a Comment