Binding GridEX Control to an IList [Janus GridEX WinForms Control v3.5 for .NET]
This tutorial is intended to show you how to bind GridEX control to a collection in your project that supports IList interface.
Note: When binding to an IList, a data bound control can not add records to the list. To let the users add records in a GridEX control bound to a collection you should implement the IBindingList interface in your collection or handle the GetNewRow event to do create the new object in code.
Follow these steps to create a simple form using a GridEX control to display an IList.
1) Create a new Visual Basic or C# project using "Windows Application" Template.
2) Add a new class into the project and name it Person.
3) Add the following properties to the Person class: Title, Name, LastName and Suffix.
4) To create the collection or person objects, add a new class to the project and name it PersonCollection.
5) Inherit the PersonCollection class from System.Collections.CollectionBase class. CollectionBase class implements IList interface.
6) Create the indexer method as well as the Add and Remove methods in the PersonCollection class.
7) Open Form1 that was created when the project was created.
8) Drop a GridEX control into Form1.
9) Add a button, set the Text property of this button to "Fill Collection"
10) In the Click event of Button1 create a new instance of the PersonCollection class add a few Person objects to the collection and bind it to the GridEX control as follows:
In VB .NET:
'Creating the collection
people = New PersonCollection()
people.Add(New Person("Mr.", "John", "Smith", "Sr."))
people.Add(New Person("Mrs.", "Mary", "Jones", ""))
people.Add(New Person("Miss", "Sally", "Porter", ""))
people.Add(New Person("Mr.", "Joseph", "Gold", "Jr."))
people.Add(New Person("Dr.", "Ian", "Goldsmith", ""))
'Binding GridEX to the people collection
'Forcing GridEX control to generate the columns needed
'to display all the properties in the Person class.
In C# .NET:
//Creating the collection
people = new PersonCollection();
//Binding GridEX to the people collection
//Forcing GridEX control to generate the columns needed
//to display all the properties in the Person class.
11) To allow add and remove rows from the collection set the following properties:
AllowAddNew = Janus.Windows.GridEX.TriState.True
AllowDelete = Janus.Windows.GridEX.TriState.True
12) Since IList doesn't implement an AddNew method to allow the CurrencyManager to add new records you will get an exception when the user tries to add a record in the GridEX control. If you know how to create a new object of the same type the list holds, use the GetNewRow event. This event is raised to let you create a new instance of an object presented by the GridEX control.
In VB .NET:
Private Sub gridEX1_GetNewRow(ByVal sender As Object,ByVal e As GetNewRowEventArgs) Handles gridEX1.GetNewRow
e.NewRow = New Person()
In C# .NET:
private void gridEX1_GetNewRow(object sender, GetNewRowEventArgs e)
e.NewRow = new Person();
13) Press F5 and run the project.
Source Of Information : Janus v3.5 Help Files for VS 2008