Home > SharePoint 2010 > Bind Gridview To SharePoint List or Library

Bind Gridview To SharePoint List or Library


Hi all,

try this example to Bind Gridview To SharePoint List or Library

In this example i will bind my Gridview to sharepoint Library called “MyLibrary”

1) Open VS2010 and create a new Project and select Visual Web Part

2) From Toolbox add Gridview control


<asp:GridView ID="grdDocuments" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:HyperLinkField DataTextField="Title" DataNavigateUrlFields="URL" />
</Columns>
</asp:GridView>

3) In Code behind add the following Code


void BindDocuments()
 {
 SPWeb mySite = SPContext.Current.Web;
 SPList myList = mySite.Lists["MyLibrary"];
 SPListItemCollection items = myList.Items;
 //Here we will make a datatable and we will put our document library data to the data table
 DataTable table;
 table = new DataTable();
 table.Columns.Add("Title", typeof(string));
 table.Columns.Add("URL", typeof(string));
 // Create rows for each splistitem
 DataRow row;
 foreach (SPListItem result in items)
 {
 row = table.Rows.Add();
 row["Title"] = result.Name;
 row["URL"] = "Your site url" + result.Url;
 }
 grdDocuments.DataSource = table.DefaultView;
 grdDocuments.DataBind();
 }

4) Now build the Project and Deploy it

Hope this helps

Good Luck.

Advertisements
Categories: SharePoint 2010
  1. No comments yet.
  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: