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.

Tuesday, February 19, 2013

Using Array to Bind Data from SharePoint List to GridView




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="ArrayGridViewUserControl.ascx.cs" Inherits="ArrayGridView.ArrayGridView.ArrayGridViewUserControl" %>

<asp:GridView ID="dgvPerson" runat="server" BackColor="White"  AutoGenerateColumns="false"
        BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
        EnableModelValidation="True" Width="100%">
        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
        <RowStyle BackColor="White" ForeColor="#330099" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
        <Columns>
        <asp:BoundField HeaderText="First Name" DataField="FirstName" />
        <asp:BoundField HeaderText="Last Name" DataField="LastName" />
        <asp:BoundField HeaderText="Phone Number" DataField="Phone" />
        </Columns>
    </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;
using System.Collections;
using System.Collections.Generic;

namespace ArrayGridView.ArrayGridView
{
    public partial class ArrayGridViewUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SPListItemCollection myColl = SPContext.Current.Web.Lists["Contacts"].Items;
            var array = new Person[myColl.Count];
            if (myColl.Count > 0)
            {
                for (int i = 0; i < myColl.Count; i++)
                {
                    SPListItem item = myColl[i];
                    array[i] = new Person()
                    {
                        FirstName = item.Title.ToString(),
                        LastName = item["LastName"].ToString(),
                        Phone = item["Phone"].ToString()
                    };
                }
                dgvPerson.DataSource = array;
                dgvPerson.DataBind();
            }
        }

        class Person
        {
            public string FirstName
            {
                get;
                set;
            }

            public string LastName
            {
                get;
                set;
            }

            public string Phone
            {
                get;
                set;
            }
        }
    }
}

No comments:

Post a Comment