Quick Objects Documentation Akal Tech Logo
Allowing Users to Dynamically Choose Fields To Display

Glossary Item Box

Business Logic Framework - Tutorial 13:

Our thirteenth tutorial shows you how to display a list of Fields dynamically and allow the user to select which fields he/she wants to see in the result set.  The process is rather very simple. You can iterate over the Fields collection of a business object to display a list of fields that can be selected by the user. Once the user makes the selection simply set the visibility of the fields according to the selection.

1 private void Page_Load()  
2 {  
3     using (Customers customer = new Customers())  
4     {  
5         //Iterate all the fields of the BusinessObject and set corresponding Checkbox to show   
6         //or hide the field. Fields property contains all the corresponding table column names of the  
7         //Business object.  
8  
9         foreach (IField field in customer.Fields)  
10         {  
11             CheckBoxList_Fields.Items.Add(field.Name.Value, true);  
12         }  
13     }  
14 }  
15  
16 private void Button_Show_Click(object sender, EventArgs e)  
17 {  
18     bool show = false;  
19     using (Customers customer = new Customers())  
20     {  
21         // all field's visible properties will be set to false  
22         customer.UseAllFieldsForDisplay(false);  
23         // loop through each checked item in the CheckBoxList  
24         foreach (Object obj in this.CheckBoxList_Fields.CheckedItems)  
25         {  
26             // Set the checked Field's Visible property to true by access the field by its name from the Field's collection.  
27             customer.Fields[obj.ToString()].Visible = true;  
28             show = true;  
29         }  
30         if (show)  
31         {  
32             //In the following we find the records of the object  
33             //this method bind the affected rows to the gridView  
34             this.GridView.DataSource = customer.Find();  
35             this.GridView.DataMember = customer.GetResultSetName();  
36         }  
37         else 
38         {  
39             this.GridView.DataSource = null;  
40         }  
41     }  
42 }  
43  
1 Private Sub Page_Load()  
2     Using customer = New Customers()  
3         'Iterate all the fields of the BusinessObject and set corresponding Checkbox to show   
4         'or hide the field. Fields property contains all the corresponding table column names of the  
5         'Business object.  
6  
7         Dim field As IField  
8         For Each field In customer.Fields  
9             CheckBoxList_Fields.Items.Add(field.Name.Value, True)  
10         Next 
11     End Using  
12 End Sub 
13 Private Sub Button_Show_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button_Show.Click  
14     Dim show As Boolean = False 
15     Using customer As Customers = New Customers()  
16         ' all field's visible properties will be set to false  
17         customer.UseAllFieldsForDisplay(False)  
18         ' loop through each checked item in the CheckBoxList  
19         Dim obj As Object 
20         For Each obj In Me.CheckBoxList_Fields.CheckedItems  
21             ' Set the checked Field's Visible property to true by access the field by its name from the Field's collection.  
22             customer.Fields(obj.ToString()).Visible = True 
23             show = True 
24         Next 
25         If (show) Then 
26             'In the following we find the records of the object  
27             'this method bind the affected rows to the gridView  
28             Me.GridView.DataSource = customer.Find()  
29             Me.GridView.DataMember = customer.GetResultSetName()  
30         Else 
31             Me.GridView.DataSource = Nothing 
32         End If 
33     End Using  
34 End Sub 
35