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

Using GridView Inside a GridView along with Group By Functionality without a single line of Loop using SharePoint Data


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") %>
    <asp:Label ID="lblRegionTitle" runat="server" Text=<%# Eval("Title") %> Visible="false"></asp:Label>
</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;'>
<div  style="height: 200px; overflow: auto;">
<asp:GridView ID="dgvCountries" runat="server" CellPadding="4"
    EnableModelValidation="True" ForeColor="#333333" GridLines="Both" AutoGenerateColumns="false">
    <AlternatingRowStyle BackColor="White" />
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <Columns></Columns>
</asp:GridView>
</div>
</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 lblRegionName = (Label)e.Row.Cells[0].FindControl("lblRegionTitle");
                        GridView dgvCountries1 = (GridView)e.Row.Cells[0].FindControl("dgvCountries");
                        BoundField bTitle = new BoundField();
                        bTitle.HeaderText = "The Countries Under Region " + lblRegionName.Text + " are given below";
                        bTitle.DataField = "Title";
                        dgvCountries1.Columns.Add(bTitle);
                        dgvCountries1.DataSource = myColl.GetDataTable();
                        dgvCountries1.DataBind();

                    }
                }
            }
            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