I recommend that you download the source code from the link
given at the end of the article, which contains the code of the custom control
with a sample website; you can look at it in more details.
Let us begin creating our new custom GridView control, open
your Visual Studio then create a new Class library project and name it as
"MyCustomControl."
First of all, you have to add some references to allow you to
use the needed namespaces and classes, below are the needed references:
1.
System.Design
2.
System.Drawing
3.
System.Web
4.
System.Web.Extensions
5.
System.Web.Extensions.Design
Then create a new class and name it "CustomGrid."
Inheriting the GridView class is much better than creating a
new databound control from scratch, that give us the benefits of functions and
subroutines already existing in the GridView class.
Listing 1
<Assembly: TagPrefix("MyCustomControl.CustomsControls", "asp")>
<Assembly: WebResource("MyCustomControl.CustomGridJS.js", "text/javascript")>
Namespace CustomsControls
<ToolboxData("<{0}:CustomGrid runat=""server""></{0}:CustomGrid>")> _
Partial Public Class CustomGrid
Inherits GridView
'My Code Here
End Class
We will create a property for including the checkbox column
to allow the developer to add the column automatically, which is implemented in
Listing 2.
Listing 2
''' <summary>
''' Add checkbox column to the gridview.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<Category("Behavior")> _
<Description("Add checkbox column to the gridview.")> _
<DefaultValue(False)> _
Public Property IncludeColumnCheckBox() As Boolean
Get
If String.IsNullOrEmpty(ViewState("IncludeColumnCheckBox")) Then
Return False
Else
Return DirectCast(ViewState("IncludeColumnCheckBox"), Boolean)
End If
End Get
Set(ByVal Value As Boolean)
ViewState("IncludeColumnCheckBox") = Value
End Set
End Property
Now we will override the CreateColumns function which is
responsible for creating the columns, and there we will add our new checkbox
column, as in Listing 3.
Listing 3
Protected Overrides Function CreateColumns(ByVal dataSource As PagedDataSource, _
ByVal useDataSource As Boolean) As ICollection
Dim columnList As ICollection = MyBase.CreateColumns(dataSource, useDataSource)
If Not IncludeColumnCheckBox Then
Return columnList
End If
Dim list As ArrayList = New ArrayList(columnList)
Dim _CheckBoxColumn As New MyTemplateField
list.Insert(0, _CheckBoxColumn)
Return list
End Function
You may note that I am declaring an object from a template
class that I created before I called MyTemplateField,
and that the GridView column consisted from the set of templates classes
(Header, Item, Alternate and footer).
I will illustrate these templates classes in the Implementing Templates classes section,
Developers do not need to write multi lines of code to get
the ID's of the selected rows; we will return these ID's simply by writing a
new property called GetCheckedRows which is shown
below.
Listing 4
''' <summary>
''' Return list of IDs joined with ',' separators
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<Browsable(False)> _
Public ReadOnly Property GetCheckedRows() As String
Get
Dim _collectionIds As String = String.Empty
If DataKeys.Count > 0 Then
For i As Integer = 0 To Rows.Count - 1
If Rows(i).RowType = DataControlRowType.DataRow Then
If DirectCast(Rows(i).FindControl("ChkItem"), CheckBox).Checked Then
If _collectionIds = String.Empty Then
_collectionIds = DataKeys(Rows(i).RowIndex).Value
Else
_collectionIds & = "," & DataKeys(Rows(i).RowIndex).Value
End If
End If
End If
Next
End If
Return _collectionIds
End Get
End Property