Showing posts with label SharePoint List. Show all posts
Showing posts with label SharePoint List. Show all posts

Tuesday, August 14, 2012

Read and Display SharePoint List Data in Gridview Control



I have developed a custom web part which reads data from SharePoint List and display in a GridView control by Visual Studio 2010.
In this scenario, I have a list name "Employee" with 4 collumns named Title, Birthday, Male, Position.

Add the following code to the .ascx file

ID="grid" runat="server" AutoGenerateColumns="false">
   
       
       
           
              
   

    
Add the following code to the .cs file:

protected void Page_Load(object sender, EventArgs e)
{
           
 SPWeb web = SPContext.Current.Web;
           
 SPList list = web.Lists["Employee"];
           
 SPListItemCollection items = list.Items;
            grid.DataSource = items.GetDataTable();
            grid.DataBind();
}

Add the web part to the Sharepoint page, we have the following result


Hope this helps!