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.

Friday, January 25, 2013

Using arrayList to Bind SharePoint List Data



Using arrayList to Bind SharePoint List Data.

Note:-
We can bind only two columns using Array List. If you want bind more than two columns again you have to use DataTable.
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="ArrGridViewUserControl.ascx.cs" Inherits="ArrGridView.ArrGridView.ArrGridViewUserControl" %>
<div>
<asp:GridView runat="server" ID="dgvBindArrayListData" AutoGenerateColumns="false" CellPadding="4"
    EnableModelValidation="True" ForeColor="#333333" GridLines="Both" Width="100%">
    <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>
    <asp:BoundField HeaderText="Player Name" DataField="Text" />
    <asp:BoundField HeaderText="Location" DataField="Value" />
    </Columns>
</asp:GridView>
</div>
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;

namespace ArrGridView.ArrGridView
{
    public partial class ArrGridViewUserControl : UserControl
    {
        ArrayList ArrItems = new ArrayList();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dgvBindArrayListData.DataSource = getData();
                dgvBindArrayListData.DataBind();
            }
        }

        public ArrayList getData()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["Players Location"];
                SPListItemCollection myColl = lst.Items;
                if (myColl.Count > 0)
                {
                    int i = 0;
                    foreach (SPListItem item in myColl)
                    {
                       ArrItems.Add(new ListItem(item.Title.ToString(),item["Location"].ToString()));
                    }
                }
            });
            return ArrItems;
        }
    }
}

No comments:

Post a Comment