Sep 19, 2009

Using SP Grid View inside Webpart

In this post I am using SPGrid view inside a webpart.

First we need to initialize the SPGrid view control in the Create Child control mehod and specify its properties

Use this code inside CreateChildControl method

—————————————————————

//Initialize the control

SPGrid CustomGrid;

CustomGrid = new SPGridView();

CustomGrid.Width = Unit.Pixel(700);

this.CustomGrid.ID = “Customgrid”;

//Specifying the Eventhandler for Paging

this.CustomGrid.PageIndexChanging += new GridViewPageEventHandler(CustomGrid_PageIndexChanging);

this.CustomGrid.AutoGenerateColumns = false;

//Specifying the grid properties

this.CustomGrid.PageSize = 10;

this.CustomGrid.BackColor = System.Drawing.Color.White;

this.CustomGrid.GridLines = GridLines.Horizontal;

//Allowing Paging on the Grid

this.CustomGrid.AllowPaging = true;

this.CustomGrid.EnableViewState = true;

this.CustomGrid.TemplateControl = null;

//Creating Boundfields in the Grid view

BoundField DealName = new BoundField();

DealName.DataField = “Name”;

DealName.ItemStyle.Width = Unit.Percentage(30);

DealName.ItemStyle.Wrap = true;

this.Controls.Add(CustomGrid);

CustomGrid.PagerTemplate = null;

—————————————————————–

Once the declaration and initialization of control is done we need to define the pagination of the SPGrid

This is the method for the pagination event

—————————————————–

// Method for Page indexing

void CustomGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

try

{

CustomGrid.PageIndex = e.NewPageIndex;

CustomGrid.DataBind();

}

catch (Exception ex)

{

}

}

Your job is done ..

Enjoy!!!

No comments:

Post a Comment