Friday, December 30, 2011

Displaying Sharepoint Custom List items in SPGridView

  1. using System;  
  2. using System.Runtime.InteropServices;  
  3. using System.Web.UI;  
  4. using System.Web.UI.WebControls;  
  5. using System.Web.UI.WebControls.WebParts;  
  6. using System.Xml.Serialization;  
  7.   
  8. using Microsoft.SharePoint;  
  9. using Microsoft.SharePoint.WebControls;  
  10. using Microsoft.SharePoint.WebPartPages;  
  11.   
  12. namespace FiveNumber  
  13. {  
  14.     [Guid("e6a48f2e-8f31-4738-8ed6-6629f985956d")]  
  15.     public class DisplayListInSpGridView : System.Web.UI.WebControls.WebParts.WebPart  
  16.     {  
  17.         SPGridView myGridView;  
  18.         SPDataSource myDataSource = new SPDataSource();  
  19.   
  20.         protected override void CreateChildControls()  
  21.         {  
  22.             myGridView = new SPGridView();  
  23.             myGridView.Enabled = true;  
  24.             myGridView.AutoGenerateColumns = false;  
  25.             myGridView.ID = "gv_MyGridView";  
  26.   
  27.             myGridView.AllowGrouping = true;  
  28.             myGridView.AllowGroupCollapse = true;  
  29.             myGridView.GroupField = "Country";  
  30.             myGridView.GroupDescriptionField = "Country";  
  31.             //myGridView.GroupFieldDisplayName = "Country Name";  
  32.   
  33.             BoundField colTitle = new BoundField();  
  34.             colTitle.DataField = "Country";  
  35.             colTitle.HeaderText = "Country";  
  36.             this.myGridView.Columns.Add(colTitle);  
  37.   
  38.             BoundField colMission = new BoundField();  
  39.             colMission.DataField = "State";  
  40.             colMission.HeaderText = "State";  
  41.             this.myGridView.Columns.Add(colMission);  
  42.   
  43.             this.Controls.Add(myGridView);  
  44.         }  
  45.   
  46.         protected override void Render(HtmlTextWriter writer)  
  47.         {  
  48.             SPSite site = SPContext.Current.Site;  
  49.             SPWeb web = SPContext.Current.Web;  
  50.             SPList list = web.Lists["Countries"];  
  51.             myDataSource.List = list;  
  52.             myGridView.DataSource = myDataSource;  
  53.             myGridView.DataBind();              
  54.   
  55.             myGridView.RenderControl(writer);  
  56.         }  
  57.     }  
  58. }  

3 comments:

  1. Hey there! I just wanted to ask if you ever have any issues with
    hackers? My last blog (wordpress) was hacked and I ended up
    losing several weeks of hard work due to no data backup.

    Do you have any methods to stop hackers?
    Also see my website: win money online

    ReplyDelete
  2. Those are loans because provide people every way to become money right released.
    Which means ought to be utilized in your current most
    optimum systems.
    Also visit my homepage payday loans uk

    ReplyDelete
  3. In the payday loans one has to pay money when they end
    up being their next take home pay. So, the customer need not
    be afraid about that emploi.
    Here is my page : quick payday loans

    ReplyDelete