Monday, January 28, 2013

How to use Image as a Bullet in GridView

How to use Image as a Bullet in GridView



How to use Image as a Bullet in 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="TestGridBFUserControl.ascx.cs" Inherits="TestGridBF.TestGridBF.TestGridBFUserControl" %>
<style type="text/css">
#navlist
{
margin-left: 0;
padding-left: 0;
list-style: none;
}
#navlist li
{
padding-left: 10px;
background-image:url('/Shared Documents/arrow.gif');
background-repeat: no-repeat;
background-position: 0 .5em;
}
</style>
<ul id="navlist">
<asp:GridView ID="dgvBulletList" runat="server" AutoGenerateColumns="False"
        CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
        GridLines="None" Width="100%">
    <AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Title">
<HeaderTemplate>
Title
</HeaderTemplate>
<ItemTemplate>
<li><%# Eval("Title") %></li>
</ItemTemplate>
<AlternatingItemTemplate>
<li><%# Eval("Title") %></li>
</AlternatingItemTemplate>
</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>
</ul>
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 TestGridBF.TestGridBF
{
    public partial class TestGridBFUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                getData();
            }
            catch (Exception)
            {
               
                throw;
            }
        }
        public void getData()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["Employees"];
                SPListItemCollection myColl = lst.Items;
                if (myColl.Count > 0)
                {
                    dgvBulletList.DataSource = myColl.GetDataTable();
                    dgvBulletList.DataBind();
                }
            });
        }
    }
}

Saturday, January 26, 2013

Bind even and Odd SharePoint ListItems Side by Side within a GridView

Bind even and Odd SharePoint ListItems Side by Side within a 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="EOGridViewUserControl.ascx.cs" Inherits="EOGridView.EOGridView.EOGridViewUserControl" %>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>
<table width="100%" cellpadding="0" cellspacing="0">
<tr><td align="center"><strong>Bind even & Odd SharePoint ListItems Side by Side within a GridView</strong></td></tr>
</table>
</div>
<div>&nbsp;</div>
<div>
<asp:GridView runat="server" ID="dgvEvenOddGridView"
    AutoGenerateColumns="False" Width="100%" BackColor="White"
    BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
    EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="OddRecord" HeaderText="Odd Record" />
<asp:BoundField DataField="EvenRecord" HeaderText="Even Record" />
</Columns>
    <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" />
</asp:GridView>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</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.Data;
using System.Collections;
namespace EOGridView.EOGridView
{
    public partial class EOGridViewUserControl : UserControl
    {
        ArrayList ArrOdd = new ArrayList();
        ArrayList ArrEven = new ArrayList();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getData();
            }
        }
        public void getData()
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Articles"];
            SPListItemCollection myColl = lst.Items;
            if (myColl.Count > 0)
            {
                int i = 0;
                foreach (SPListItem item in myColl)
                {
                    if (i % 2 != 0)
                    {
                        ArrOdd.Add(item.Title.ToString());
                    }
                    else
                    {
                        ArrEven.Add(item.Title.ToString());
                    }
                    i++;
                }
                DataTable dt = new DataTable();
                dt.Columns.Add("OddRecord");
                dt.Columns.Add("EvenRecord");
                for (int k = 0; k < ArrOdd.Count; k++)
                {
                    dt.Rows.Add();
                    dt.Rows[k]["OddRecord"] = ArrOdd[k].ToString();
                }
                for (int L = 0; L < ArrEven.Count; L++)
                {
                    dt.Rows[L]["EvenRecord"] = ArrEven[L].ToString();
                }
                dgvEvenOddGridView.DataSource = dt;
                dgvEvenOddGridView.DataBind();
            }
        }
    }
}