Purvish's Blog

September 16, 2010

Step by step to display SharePoint List data into GridView using C# code

Filed under: SharePoint Administration,SharePoint Development — Purvish Shah @ 10:49 pm

Step by step to display SharePoint List data into GridView using C# code

Step by step to display SharePoint List data into GridView using C# code

Here I am trying to show you step by step.

1)      Drag GridView from tool bar

2)      Click “GridView Task”

3)      Add column which you want to show in GridView, you can create  Bound Fields and Link Table according to your requirement

4)      You can hide some of the columns, which you don’t want to show users. You can set visible property false for that

5)      Next steps is GetSPListData()

5.1) GetSPListData()


We need to get sharepoint list name

SPList list = Web.Lists[“Your List Name”];

Create query(CAML) which brings data from SharePoint List

SPListItemCollection itemColl;

SPQuery query = new SPQuery();

itemColl = list.GetItems(query);

In below, line we are adding only sharePoint list column name    which we want to   bind in gridview

query.ViewFields = CreateViewFields(list,SharePointList Column1, SharePointList Column2,…..);

We can filter SharePoint list data also

QueryString += “<Where><Eq><FieldRef Name=” + ‘”‘ + filterVariable(e.g. project_status)+ ‘”‘ + ” /><Value Type=’Text’>Active</Value></Eq></Where>”;

query.Query = QueryString;

In my case, I am gonna read each SharePoint list row and adding in to   Data Table.

itemColl = list.GetItems(query);

if (itemColl != null && itemColl.Count > 0)


for (int icount = 0; icount < itemColl.Count; icount++)






private void GetProjectItemFromList(SPListItem listItem)


if (listItem[PROJECT_NAME] != null)


ProjectName = listItem[PROJECT_NAME].ToString();



..You need to add all Sharepoint column name which you want to bind with gridview


//Below Line will add all SharePoint list data row by row to DataTabe

AddDataToTable(ProjectName,…. All yourSharePoint list column name……., (DataTable)Session[ProjectsTable]);



5.3) CreateViewFields() function which we have mentioned in GetSPListData()

public string CreateViewFields(string listName, params string[] fieldNames)


StringBuilder sb = new StringBuilder();

foreach (string fieldName in fieldNames)


string internalFieldName = GetListFieldInternalName(listName, fieldName);

sb.AppendFormat(“<FieldRef Name=’{0}’/>”, internalFieldName);


return sb.ToString();


5.4) GetListFieldInternalName() which we have mentioned CreateViewFields()

private string GetListFieldInternalName(string listName, string fieldName)


String internalFieldName = “”;

string QueryString = string.Empty;

SPList list = Web.Lists[SPListName];

SPListItemCollection itemColl;

SPQuery query = new SPQuery();

itemColl = list.GetItems(query);

if (itemColl.Count > 0)


if (itemColl[0].Fields.GetField(fieldName) != null)

internalFieldName = itemColl[0].Fields.GetField(fieldName).InternalName;


return internalFieldName;


5.5) Before we add data to table we need to create table.

private DataTable CreateDataTable()


DataTable dtProjectsInfo = new DataTable();

DataColumn dColumnProjects;

dColumnProjects = new DataColumn();

dColumnProjects.DataType = Type.GetType(“System.String”);

dColumnProjects.ColumnName = PROJECT_NAME;


Above four lines for

all yourSharePoint list column name that you want to     bind with grid


return dtProjectsInfo;



AddDataToTable(ProjectName,…. All yourSharePoint list column name……., (DataTable)Session[ProjectsTable]);


DataRow row;

row = dtTable.NewRow();

row[PROJECT_NAME] = ProjectName;

All yourSharePoint list column name…….



5.7)We need one for function called which is responsible to bind data table to Grid

protected void BindTableData()


Gridview.DataSource = Session[ProjectsTable];



6) We have done with all material which we need to bind SharePoint list data to Grid View

I am gonna use Page_Load event to wrap it up all funtions.

private const string ProjectsTable = “ProjectsTable”;

protected void Page_Load(object sender, EventArgs e)


DataTable projectsTable;

projectsTable = new DataTable();

projectsTable = CreateDataTable();

Session[ProjectsTable] = projectsTable;




I Think you are now ready to go !!!!!!!!!!!!!!!!!

Cheers !!!!!!!!!!

Questions and Suggestions most welcome


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: