Bind List Data to JQuery Data Table

Many a time we come across the situation to bind list collection to GridView or Data table. The below code snippet is used to bind list collection to a JQuery Data table.
 
If we used JQuery data table then we are able to search particular values in the table without any custom code. It also sorts the table based on column.
 
For this we need to add reference of js files in .ascx file. We need to download the below files and need to upload it into our style library and accordingly we need to add the reference as shown below. 
  1. <link rel="stylesheet" type="text/css" href="/DatatableAssets/css/jquery.dataTables.css">  
  2. <script type="text/javascript" src="/DatatableAssets/js/jquery.js"></script>  
  3. <script type="text/javascript" src="/DatatableAssets/js/jquery.dataTables.min.js"></script>  
  4. <script type="text/javascript" src="/DatatableAssets/js/dataTables.tableTools.js"></script>  
Then we need to add table as shown below:
  1. <table id="tblGroup" class="display cell-border" width="100%" border="0" cellspacing="0" cellpadding="0">  
  2.     <thead width="100%">  
  3.         <tr>  
  4.             <th>ID</th>  
  5.             <th>Group Name</th>  
  6.         </tr>  
  7.     </thead>  
  8.     <tbody>  
  9.         <asp:Repeater runat="server" ID="repGroupDetails">  
  10.             <ItemTemplate>  
  11.                 <tr>  
  12.                     <td>  
  13.                         <%# DataBinder.Eval(Container.DataItem, "ID")%>  
  14.                     </td>  
  15.                     <td>  
  16.                         <%# DataBinder.Eval(Container.DataItem, "Title")%>  
  17.                     </td>  
  18.                 </tr>  
  19.             </ItemTemplate>  
  20.         </asp:Repeater>  
  21.     </tbody>  
  22. </table>   
To bind above table with JQuery table we need to add the below script.
  1. <script>  
  2.     $(document).ready(function()  
  3.     {  
  4.         $('#tblGroup').dataTable  
  5.         ({  
  6.             "scrollY""325px",  
  7.             "scrollCollapse"true,  
  8.             "paging"false  
  9.         });  
  10.     });  
  11. </script>   
First we need to get data from SharePoint list. Add below list of code into .cs file. 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     GetGroupDetails();  
  4. }  
  5.   
  6.   
  7. public void GetGroupDetails()  
  8. {  
  9.     try  
  10.     {  
  11.         string cQuery = "";  
  12.         string cViewFields = "<FieldRef Name='ID'/><FieldRef Name='Title'/><FieldRef Name='Created'/>";  
  13.         cQuery = "<OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>";  
  14.         SPListItemCollection itemColl = GetListItems(SPContext.Current.Site.Url, "ListName", cQuery, cViewFields);  
  15.         if (itemColl.Count != 0)  
  16.         {  
  17.             dtItemColl = itemColl.GetDataTable();  
  18.             dtCP = dtItemColl.Clone();  
  19.             foreach(SPListItem item in itemColl)  
  20.             {  
  21.                 DataRow drCP = dtCP.NewRow();  
  22.                 drCP["ID"] = item["ID"];  
  23.                 drCP["Title"] = item["Title"];  
  24.                 dtCP.Rows.Add(drCP);  
  25.             }  
  26.         }  
  27.         if ((dtCP != null) || (dtCP.Rows.Count != 0))   
  28.         {  
  29.             repGroupDetails.DataSource = dtCP;  
  30.             repGroupDetails.DataBind();  
  31.         }  
  32.     } catch (Exception ex) {}  
  33. }  
  34.   
  35.   
  36. public SPListItemCollection GetListItems(string sWebUrl, string sListName, string sQuery, string sViewFields)  
  37. {  
  38.     SPListItemCollection objListItemCollection = null;  
  39.     SPSecurity.RunWithElevatedPrivileges(delegate()  
  40.     {  
  41.         using(SPSite objSite = new SPSite(sWebUrl))  
  42.         {  
  43.             using(SPWeb objWeb = objSite.OpenWeb())  
  44.             {  
  45.                 SPList objList = objWeb.Lists.TryGetList(sListName);  
  46.                 if (objList != null)  
  47.                 {  
  48.                     SPQuery objSPQuery = new SPQuery();  
  49.                     objSPQuery.Query = sQuery;  
  50.                     objSPQuery.ViewFields = sViewFields;  
  51.                     objListItemCollection = objList.GetItems(objSPQuery);  
  52.                 }  
  53.             }  
  54.         }  
  55.     });  
  56.     return objListItemCollection;  
  57. }   
Build and deploy the web part. Create one web part page and add the above web part and it will show you the result.
Ebook Download
View all
Learn
View all