Suppose  that you have a list and  you created some fields(columns) in that list.Then you need to display these columns in a visual web part when the page is loading.To do this just use  below code in your page_load method in the visual webpart project. 
Display  columns which you created.
 protected void Page_Load(object sender, EventArgs e)  
     {  
       SPWeb spweb = SPContext.Current.Web;  
       SPList List = spweb.GetList("Type url for the list");  
       SPFieldCollection spfieldcollection = List.Fields;  
       ListItemCollection ListColumnNames = new ListItemCollection();  
       if (spfieldcollection.Count != 0)  
       {  
         foreach (SPField field in spfieldcollection)  
         {  
           if (!field.FromBaseType)  
           {  
             ListColumnNames.Add(new ListItem(field.Title));  
           }  
         }  
       }  
       dropdowncontrol.DataSource = ListColumnNames;  
       dropdowncontrol.DataBind();  
     }  
Display all columns
 protected void Page_Load(object sender, EventArgs e)  
     {  
       SPWeb spweb = SPContext.Current.Web;  
       SPList List = spweb.GetList("Type url for the list");  
       SPFieldCollection spfieldcollection = List.Fields;  
       ListItemCollection ListColumnNames = new ListItemCollection();  
       if (spfieldcollection.Count != 0)  
       {  
         foreach (SPField field in spfieldcollection)  
         {  
             ListColumnNames.Add(new ListItem(field.Title));    
         }  
       }  
       dropdowncontrol.DataSource = ListColumnNames;  
       dropdowncontrol.DataBind();  
     }  
For the checkboxlist you change "dropdowncontrol" in to "checkboxlistcontrol".
  checkboxlistcontrol.DataSource = ListColumnNames;  
  checkboxlistcontrol.DataBind();  
 
No comments:
Post a Comment