Saturday, July 01, 2006

WSS v3 Development Pattern - Binding a SPGridView to a SPDataSource in C#

The WSS v3 development pattern below illustrates a simple example for binding a SPGridView object to a SPDataSource object.  The pattern assumes a member variable m_grid of type SPGridView has been declared in your web part.


protected override void CreateChildControls()
  SPWeb web = SPContext.Current.Web;
  using (SPDataSource ds = new SPDataSource())
    ds.List = 
      web.Lists[new Guid("FB0ADC39-3792-4FC7-949C-F033D0671691")];
    ds.DataSourceMode = SPDataSourceMode.List;
    ds.IncludeHidden = false;
    ds.Scope = SPViewScope.Recursive;

    m_grid = new SPGridView();
    m_grid.DataSource = ds;
    m_grid.AutoGenerateColumns = false;


    BoundField t = new BoundField();
    t.DataField = "Title";
    t.HeaderText = "Title";

    BoundField b = new BoundField();
    b.DataField = "Body";
    b.HeaderText = "Body";    
    b.HtmlEncode = false;


Here’s the ouput:



Anonymous said...

Hi Tony

I found your article interesting and useful, since I've also been investigating the controls in the Microsoft.SharePoint.WebControls namespace.

I'm particularly interested in using the SPGridView control with SPMenuField, in order to reproduce the "list column with context menu" effect used throughout in SharePoint.

However, I'm having trouble displaying text within a SPMenuField column. Currently I can only produce a menu column with no text in it, which I'm sure you'll agree is not terribly useful. :). I note that at least one other person has written about the same problem (see this article for a good description).

Examining the object model of SPMenuField reveals a property called "TextFields". This looks promising, but due the property's type (it's a string) and lack of documenation it's unclear what value to pass. I've tried a number of things, including the name of a column from the SPGridView's data source, however without success.

Additionally, I've tried:

1. Searching exhaustively online for a solution.
2. Performing textual file search through an existing WSS 3.0 installation for usages of SPMenuField.
3. Decompiling various SharePoint assemblies to find usages of SPMenuField.

But no luck so far.

Do you have any ideas how to get this to work?

Many thanks


Anonymous said...

Hi Jonathan,

I'm also having trouble with SPGridView and SPMenuField. I don't know how to add a context menu to gridview. Could you show me how to do this?

Thanks before hand.

Anonymous said...

Here's a good article about SPMenuField's within SPGridView's:

SPGridView and SPMenuField

Anonymous said...

I have enabled inline editing in the SPGridView but cannot save the changed values back to the datasource. On postback it gives the same values as shown initially.
Any help with code would be appreciated.

Anonymous said...

This is a good post. I want to bind the "Type" field from the document library to my spgridview.
I tried to add as hyperlink column but it shows me only 1 and o in "Type" filed. can u please give me some suggestion how to show the document type in spgridview using list datasource.

Anonymous said...

Hi Jonathan,

How can we set two fields value as a text for single field using SPGridView. Is there any way to do this.

Ex: Say a list has fields with FirstName, LastName, Email, etc.

Now using SPGridView and BoundField can't we combine the fields FirstName and LastName and show them as FullName.

Thanx in Advance.

Arun said...

Hi Tony,
I have tried the SPGridView. Everything is fine except paging. The problem is I couldn't set the pager buttons. In fact, I couldn't see any of the pager buttons on my list. I tried same code with GridView and there the same code worked fine and I could see the pager buttons.

Please help me to figure out what is going wrong with my SPGridView.

here is the code I am tring out.
protected override void CreateChildControls()


oGrid = new SPGridView();
oDS = new SPDataSource();
oGrid.AllowPaging = true;
oGrid.PageSize = 3;
oGrid.EnableViewState = true;
oGrid.PagerTemplate = null;
oGrid.TemplateControl = null;
oGrid.AutoGenerateColumns = false;
oGrid.PageIndexChanging += new GridViewPageEventHandler(oGrid_PageIndexChanging);
oDS.List = SPContext.Current.Web.Lists["Contacts"];
oGrid.AutoGenerateColumns = false;
BoundField colTitle = new BoundField();
colTitle.DataField = "Last Name";
colTitle.HeaderText = "Last Name";

oGrid.DataSource = oDS;


void oGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)


oGrid.PageIndex = e.NewPageIndex;

thank you,

Popovic Sasa said...

Nice article.

I made a class that inherits from SPGridView and allows custom paging. Using that class you can bind to grid only the rows/items that should be shown on a particular page and not the whole result set.

It may be very usefull in cases when you have a very large result sets and don't want to fetch the whole result set from database and bind it to grid. I published my control on CodePlex so that everyone can use it:

I made a sample web part and a few classes (including SQL) too that demonstrate how my grid control can be used (sample is included in the same solution where my inherited SPGridView control is).

If someone is interested feel free to take a look and use my control.