Open the team site.

team site

Create a sample list “Employee”.

Open the team site

Add some values into the list.

Employee

Open Visual Studio then create a new Visual Webpart.



Enter a valid site URL then select Deploy as a Farm solution.

new visual webpart

Create a new Visual Webpart in the project.

Select Deploy as a farm solution

Now open EmployeeRegistration.ascx.

Create a GridView.

Create a new visual webpart

Code

  1. <asp:GridView ID="mygrid" runat="server" AutoGenerateColumns="false">
  2. <Columns>
  3. <asp:BoundField DataField="Name" HeaderText="
  4. Name" />
  5. <asp:BoundField DataField="Email" HeaderText="Email" />
  6. </Columns>
  7. </asp:GridView>
Create a GridView

Now open EmployeeRegistration.ascx.cs.

Code
  1. using System;
  2. using System.ComponentModel;
  3. using System.Web.UI.WebControls.WebParts;
  4. using Microsoft.SharePoint;
  5. using System.Data;
  6. namespace BindListData.EmployeeRegistration
  7. {
  8. [ToolboxItemAttribute(false)]
  9. public partial class EmployeeRegistration : WebPart
  10. {
  11. // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
  12. // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
  13. // for production. Because the SecurityPermission attribute bypasses the security check for callers of
  14. // your constructor, it's not recommended for production purposes.
  15. // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
  16. public EmployeeRegistration()
  17. {
  18. }
  19. protected override void OnInit(EventArgs e)
  20. {
  21. base.OnInit(e);
  22. InitializeControl();
  23. }
  24. protected void Page_Load(object sender, EventArgs e)
  25. {
  26. SPWeb web = SPContext.Current.Web;
  27. SPList list = web.Lists["Employee"];
  28. SPListItemCollection items = list.Items;
  29. mygrid.DataSource = items.GetDataTable();
  30. mygrid.DataBind();
  31. }
  32. }
  33. }
Now deploy the webpart.

Deploy

Now the result.

result

output

Happy SharePointing!

Regards: Vinodh.N (SharePoint developer / administrator).