Wednesday, December 16, 2009

Useing SPGridView in Code Behind

1] Create ASPX and add following code

<%@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
    Namespace="Microsoft.SharePoint.WebControls" TagPrefix="cc1" %>


<cc1:SPGridView ID="SPGridView1" runat="server" AutoGenerateColumns="False" >
    </cc1:SPGridView>
    <cc1:SPDataSource ID="SPDataSource1" runat="server">
    </cc1:SPDataSource>

2] Create Cs file and add following code

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI ;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;

public class ShowList : LayoutsPageBase
{
public SPGridView SPGridView1;
public SPDataSource SPDataSource1;
protected override void OnLoad(EventArgs e)
{
SPWeb site = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context);
SPList mylist = site.Lists["Tasks"];
this.SPDataSource1.List = mylist;
BoundField colTitle = new BoundField();
colTitle.DataField = "Title";
colTitle.HeaderText = "Title";
this.SPGridView1.Columns.Add(colTitle);
this.SPGridView1.DataSource = SPDataSource1;
int a =this.SPGridView1.Columns.Count;
this.SPGridView1.DataBind();
int b = this.SPGridView1.Columns.Count;
}

No comments:

Post a Comment