Introduction
Microsoft .NET Framework offers many collection classes but sometimes these collection classes don't fulfill the development requirements, which motivates developers to create their own Custom Collections. In our previous articles, we have practiced data binding feature of Aspose.Grid.Web with databases but it is also possible for developers to bind their custom collections with Aspose.Grid.Web and enjoy the benefits of its data binding features.
Binding a Worksheet with a Custom Collection
We will try to practice this feature by creating a sample application step by step. First of all, we will create a custom collection and then we will use that custom collection for binding with a worksheet.
Step 1: Creating a Custom Record
Before creating a custom collection, we should create a class to hold custom records (that will be stored in a custom collection). You can define a Custom Record class according to your own needs. The purpose of this article is jus to give you an idea to create your own custom collections and bind them with Aspose.Grid.Web.
We have created a MyCustomRecord class that contains five private fields and five public properties to control the access of these private fields. Here is the structure of properties creates by us:
- StringField1 property to read and write stringfield1. It's data type is string.
- ReadonlyField2 property to only read stringfield2. It's data type is also string.
- DateField1 property to read and write datefield1. It's data type is DateTime.
- IntField1 property to read and write intfield1. It's data type is int.
- DoubleField1 property to read and write doublefield1. It's data type is double.
Example:
[C#]
//Creating a class that will act as record for the custom collection
public class MyCustomRecord
{
//Private data members
private string stringfield1;
private string stringfield2 = "ABC";
private DateTime datefield1;
private int intfield1;
private double doublefield1;
//Creating a string property
public string StringField1
{
get { return stringfield1;}
set { stringfield1 = value;}
}
//Creating a readonly string property
public string ReadonlyField2
{
get { return stringfield2; }
}
//Creating a DateTime property
public DateTime DateField1
{
get { return datefield1; }
set { datefield1 = value; }
}
//Creating an int property
public int IntField1
{
get { return intfield1; }
set { intfield1 = value; }
}
//Creating a double property
public double DoubleField1
{
get { return doublefield1; }
set { doublefield1 = value; }
}
}
[VB.NET]
'Creating a class that will act as record for the custom collection
Public Class MyCustomRecord
'Private data members
Private stringfield1 As String
Private stringfield2 As String = "ABC"
Private datefield1 As DateTime
Private intfield1 As Integer
Private doublefield1 As Double
'Creating a string property
Public Property StringField1() As String
Get
Return stringfield1
End Get
Set (ByVal Value As String)
stringfield1 = value
End Set
End Property
'Creating a readonly string property
Public ReadOnly Property ReadonlyField2() As String
Get
Return stringfield2
End Get
End Property
'Creating a DateTime property
Public Property DateField1() As DateTime
Get
Return datefield1
End Get
Set (ByVal Value As DateTime)
datefield1 = value
End Set
End Property
'Creating an int property
Public Property IntField1() As Integer
Get
Return intfield1
End Get
Set (ByVal Value As Integer)
intfield1 = value
End Set
End Property
'Creating a double property
Public Property DoubleField1() As Double
Get
Return doublefield1
End Get
Set (ByVal Value As Double)
doublefield1 = value
End Set
End Property
End Class
Step 2: Creating a Custom Collection
Now, we can create our Custom Collection in which we can add and access custom records. We can create a more complex custom collection but to make it more simple and easier for our readers, we have created a MyCollection class that contains a readonly indexer. Using this indexer, we can get any custom record stored in the collection.
Example:
[C#]
//Creating a custom collection
public class MyCollection : CollectionBase
{
//Leaving the collection constructor empty
public MyCollection()
{
}
//Creating a readonly property for custom collection. This Item property is used by GridWeb control to
//determine the collection's type
public MyCustomRecord this[int index]
{
get { return (MyCustomRecord)this.List[index]; }
}
}
[VB.NET]
'Creating a custom collection
Public Class MyCollection
Inherits CollectionBase
'Leaving the collection constructor empty
Public Sub New()
End Sub
'Creating a readonly property for custom collection. This Item property is used by GridWeb control to
'determine the collection's type
Default Public ReadOnly Property Item(index As Integer) As MyCustomRecord
Get
Return CType(Me.List(index), MyCustomRecord)
End Get
End Property
End Class
Step 3: Binding Worksheet with Custom Collection
The process of creating a custom collection is finished so, this is the time to use the custom collection for binding with a worksheet in Aspose.Grid.Web. So, we will create a Web Form, add GridWeb control to it and add some code to bind custom collection with the worksheet.
To use the custom collection for binding, we will first need to create an object of MyCollection class (that we created in above step). Then we can create and add objects of MyCustomRecord class (also created in first step) to the object of MyCollection class. You must be wondering that there wasn't any method in MyCollection class that could be used to add an object of MyCustomRecord class in the collection. Well, if you again take a look at the above code then you will find that MyCollection class is inherited from CollectionBase class (that has implemented IList interface. IList interface provides an Add method for adding an object to the collection). So, we can use Add method of IList by upcasting the MyCollection object to IList.
At the end, we can set MyCollection object as the Data Source of worksheet and bind the worksheet with the collection. We can also create validation rules for the bound columns of the worksheet.
NOTE: We will add all code to Page_Load event handler of our Web Form and before adding code to Page_Load event handler, make sure that there is not any PostBack because data should be loaded and bound only once.
Example:
[C#]
//Implementing Page_Load event handler
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
//Creating an object of custom collection
MyCollection list = new MyCollection();
//Creating an instance of Random class
System.Random rand = new System.Random();
//Creating a loop that will run 5 times
for (int i=0; i<5; i++)
{
//Creating an object of Custom Record
MyCustomRecord rec = new MyCustomRecord();
//Initializing all properties of Custom Record
rec.DateField1 = DateTime.Now;
rec.DoubleField1 = rand.NextDouble();
rec.IntField1 = rand.Next();
rec.StringField1 = "ABC_" + i;
//Adding Custom Record to Collection
((IList)list).Add(rec);
}
//Accessing a desired worksheet
WebWorksheet sheet = GridWeb1.WebWorksheets[0];
//Setting the Data Sorce of worksheet
sheet.DataSource = list;
//Creating columns automatically
sheet.CreateAutoGenratedColumns();
//Setting the validation type of value to DateTime
sheet.BindColumns[2].Validation.ValidationType = ValidationType.DateTime;
//Binding worksheet
sheet.DataBind();
//Assigning an event handler to InitializeNewBindRow event of the worksheet
sheet.InitializeNewBindRow += new InitializeNewBindRowHandler(GridWeb1_InitializeNewBindRow);
}
}
[VB.NET]
'Implementing Page_Load event handler
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
'Creating an object of custom collection
Dim list As MyCollection = New MyCollection()
'Creating an instance of Random class
Dim rand As System.Random = New System.Random()
'Creating a loop that will run 5 times
Dim i As Integer
For i = 0 To 5- 1 Step i + 1
'Creating an object of Custom Record
Dim rec As MyCustomRecord = New MyCustomRecord()
'Initializing all properties of Custom Record
rec.DateField1 = DateTime.Now
rec.DoubleField1 = rand.NextDouble()
rec.IntField1 = rand.Next()
rec.StringField1 = "ABC_" + i
'Adding Custom Record to Collection
(CType(list, IList)).Add(rec)
Next
'Accessing a desired worksheet
Dim sheet As WebWorksheet = GridWeb1.WebWorksheets(0)
'Setting the Data Sorce of worksheet
sheet.DataSource = list
'Creating columns automatically
sheet.CreateAutoGenratedColumns()
'Setting the validation type of value to DateTime
sheet.BindColumns(2).Validation.ValidationType = ValidationType.DateTime
'Binding worksheet
sheet.DataBind()
'Assigning an event handler to InitializeNewBindRow event of the worksheet
sheet.InitializeNewBindRow += New InitializeNewBindRowHandler(GridWeb1_InitializeNewBindRow)
End If
End Sub
Step 4: Handling InitializeNewBindRow Event of Worksheet
In the above code, you might have noticed an extra line of code that we used to assign the GridWeb1_InitializeNewBindRow event handler to InitializeNewBindRow event of the worksheet. This event is triggered whenever a new bound row is added to worksheet. We created an event handler for this this event because of DateField1 property of MyCustomRecord object. Aspose.Grid.Web automatically initializes int and double values with Zero (0) whenever a new bound row is added to GridWeb control. But for date, we would like GridWeb control to automatically add the current date from system. To do so, we have created GridWeb1_InitializeNewBindRow event handler for the InitializeNewBindRow event.
We can access a particular instance of MyCustomRecord class from the GridWeb using bindObject argument and then we can assign the current system date to its DateField1 property.
Example:
[C#]
//Creating GridWeb1_InitializeNewBindRow event handler
private void GridWeb1_InitializeNewBindRow(WebWorksheet sender, object bindObject)
{
//Accessing that custom record object that is newly bound
MyCustomRecord rec = (MyCustomRecord)bindObject;
//Initializing the DateTime of a property when a new row gets bound to the database
rec.DateField1 = DateTime.Now;