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.

Wednesday, February 06, 2013

Group By with Asp.Net GridView using SharePoint Object Model




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="GBAspGridViewUserControl.ascx.cs" Inherits="GBAspGridView.GBAspGridView.GBAspGridViewUserControl" %>
 <script language="javascript" type="text/javascript" src="/UserPages/JS/jquery.min.1.8.3.js"></script>
<script language="javascript" type="text/javascript">
    function OpenDiv(strRegionID) {
        $("#divOpenDiv" + strRegionID).hide();
        $("#divCloseDiv" + strRegionID).show();
        $("#divCountryData" + strRegionID).show();
    }
    function CloseDiv(strRegionID) {
        $("#divOpenDiv" + strRegionID).show();
        $("#divCloseDiv" + strRegionID).hide();
        $("#divCountryData" + strRegionID).hide();
    }
</script>
<asp:GridView ID="dgvRegion" runat="server" AutoGenerateColumns="False"
    Width="100%" DataKeyNames="ID" CellPadding="4" EnableModelValidation="True"
    ForeColor="#333333" GridLines="None">
    <AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<div>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
Country
</td>
</tr>
</table>
</div>
</HeaderTemplate>
<ItemTemplate>
<div>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="10px">
<div id="divOpenDiv<%# Eval("ID") %>"><a href="javascript:OpenDiv('<%# Eval("ID") %>')"><img src="/_layouts/images/plus.gif" border="0" /></a></div>
<div id="divCloseDiv<%# Eval("ID") %>" style="display:none;"><a href="javascript:CloseDiv('<%# Eval("ID") %>')"><img src="/_layouts/images/minus.gif" border="0" /></a></div>
</td>
<td>Region: <%# Eval("Title") %>
</td>
</tr>
</table>
</div>
<div id="divCountryData<%# Eval("ID") %>" style="display:none">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td style='padding-left:60px;'>
<asp:Label ID="lblCountry" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
    <EditRowStyle BackColor="#7C6F57" />
    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#E3EAEB" />
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
Ascx.Cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
namespace GBAspGridView.GBAspGridView
{
    public partial class GBAspGridViewUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            dgvRegion.RowDataBound += new GridViewRowEventHandler(dgvRegion_RowDataBound);
            if (!IsPostBack)
            {
                BindRegion();
            }
           
        }

        void dgvRegion_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string strRegionID = dgvRegion.DataKeys[e.Row.RowIndex].Value.ToString();
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["lst_Country"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><Where><Eq><FieldRef Name='Region' LookupId='TRUE' /><Value Type='Lookup'>" + strRegionID + "</Value></Eq></Where>";
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        Label lblCountry1 = (Label)e.Row.Cells[0].FindControl("lblCountry");
                        lblCountry1.Text += "<Table width='100%' cellpadding='0' cellspacing='0'>";
                        int i = 0;
                        foreach (SPListItem item in myColl)
                        {
                            if (i % 2 == 0)
                            {
                                lblCountry1.Text += "<Tr style='background-color:#e3eaeb;'>";
                            }
                            else
                            {
                                lblCountry1.Text += "<Tr style='background-color:#ffffff'>";
                            }
                            lblCountry1.Text += "<Td>";
                            lblCountry1.Text += item.Title.ToString();
                            lblCountry1.Text += "</Td>";
                            lblCountry1.Text += "</Tr>";
                            i++;
                        }
                        lblCountry1.Text += "</Table>";

                    }
                }
            }
            catch (Exception Ex)
            {

                Response.Write(Ex.ToString());
            }
        }
        public void BindRegion()
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Region"];
            SPListItemCollection myColl = lst.Items;
            if (myColl.Count > 0)
            {
                dgvRegion.DataSource = myColl.GetDataTable();
                dgvRegion.DataBind();
            }
        }
    }
}

No comments:

Post a Comment