Populating the grid

Once you have added the grid to your application, you will normally populate it using the ItemsSource property (like most other grids). The ItemsSource property expects an object that implements the IEnumerable interface, but in most cases you will work at a slightly higher level and use an object that implements the ICollectionView interface.

The ICollectionView interface is the main data-binding interface in Silverlight and WPF (in WinForms, that role was played by the IBindlingList interface).

You could also bind the grid directly to the customer list, of course. But binding to an ICollectionView is usually a better idea because it retains a lot of the data configuration for the application, and that can be shared across controls.

If many controls are bound to the same ICollectionView object, they will all show the same view. Selecting an item in one control will automatically update the selection on all other controls. Filtering or sorting will also be shared by all controls bound to the same view.

The editing and sorting & filtering samples in the ControlExplorer sample installed with this product demonstrate how to populate the C1FlexGrid. The ControlExplorer sample is located in the C:\Documents and Settings\<username>\My Documents\ComponentOne Samples\Studio for Windows Phone or C:\Users\<username>\Documents\ComponentOne Samples\Studio for Windows Phone folder.

In these samples, the grid is bound to a customer list using the following code:

            // create the data

            var data = Customer.GetCustomerList(100);

            flexgrid.ItemsSource = data;

 

            // hide read-only "Country" column

            var col = flexgrid.Columns["Country"];

            col.Visible = false;

 

            // map countryID column so it shows country names instead of their IDs

            Dictionary<int, string> dct = newDictionary<int, string>();

            foreach (var country inCustomer.GetCountries())

            {

                dct[dct.Count] = country;

            }

            col = flexgrid.Columns["CountryID"];

            col.ValueConverter = newColumnValueConverter(dct);

            col.HorizontalAlignment = HorizontalAlignment.Left;

            col.Header = "Country";

            col.Width = newGridLength(140);

 

            // provide auto-complete lists for first and last name columns

            col = flexgrid.Columns["First"];

            col.ValueConverter = newColumnValueConverter(Customer.GetFirstNames(), false);

            col = flexgrid.Columns["Last"];

            col.ValueConverter = newColumnValueConverter(Customer.GetLastNames(), false);

 

            col = flexgrid.Columns["Hired"];

            col.Width = newGridLength(140);

 

            col = flexgrid.Columns["Weight"];

            col.Width = newGridLength(100);

 

            col = flexgrid.Columns["Active"];

            col.Width = newGridLength(100);


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.