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.
| private void Page_Load() | |
| { | |
| using (Customers customer = new Customers()) | |
| { | |
| //Iterate all the fields of the BusinessObject and set corresponding Checkbox to show | |
| //or hide the field. Fields property contains all the corresponding table column names of the | |
| //Business object. | |
| foreach (IField field in customer.Fields) | |
| { | |
| CheckBoxList_Fields.Items.Add(field.Name.Value, true); | |
| } | |
| } | |
| } | |
| private void Button_Show_Click(object sender, EventArgs e) | |
| { | |
| bool show = false; | |
| using (Customers customer = new Customers()) | |
| { | |
| // all field's visible properties will be set to false | |
| customer.UseAllFieldsForDisplay(false); | |
| // loop through each checked item in the CheckBoxList | |
| foreach (Object obj in this.CheckBoxList_Fields.CheckedItems) | |
| { | |
| // Set the checked Field's Visible property to true by access the field by its name from the Field's collection. | |
| customer.Fields[obj.ToString()].Visible = true; | |
| show = true; | |
| } | |
| if (show) | |
| { | |
| //In the following we find the records of the object | |
| //this method bind the affected rows to the gridView | |
| this.GridView.DataSource = customer.Find(); | |
| this.GridView.DataMember = customer.GetResultSetName(); | |
| } | |
| else | |
| { | |
| this.GridView.DataSource = null; | |
| } | |
| } | |
| } | |
| Private Sub Page_Load() | |
| Using customer = New Customers() | |
| 'Iterate all the fields of the BusinessObject and set corresponding Checkbox to show | |
| 'or hide the field. Fields property contains all the corresponding table column names of the | |
| 'Business object. | |
| Dim field As IField | |
| For Each field In customer.Fields | |
| CheckBoxList_Fields.Items.Add(field.Name.Value, True) | |
| Next | |
| End Using | |
| End Sub | |
| Private Sub Button_Show_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Show.Click | |
| Dim show As Boolean = False | |
| Using customer As Customers = New Customers() | |
| ' all field's visible properties will be set to false | |
| customer.UseAllFieldsForDisplay(False) | |
| ' loop through each checked item in the CheckBoxList | |
| Dim obj As Object | |
| For Each obj In Me.CheckBoxList_Fields.CheckedItems | |
| ' Set the checked Field's Visible property to true by access the field by its name from the Field's collection. | |
| customer.Fields(obj.ToString()).Visible = True | |
| show = True | |
| Next | |
| If (show) Then | |
| 'In the following we find the records of the object | |
| 'this method bind the affected rows to the gridView | |
| Me.GridView.DataSource = customer.Find() | |
| Me.GridView.DataMember = customer.GetResultSetName() | |
| Else | |
| Me.GridView.DataSource = Nothing | |
| End If | |
| End Using | |
| End Sub | |
