Hello,
I am trying to create a ItemTemplate at runtime for a ComponentArt
Grid.
This GridView works in a very similar way to the Asp.Net 2.0 GridView
so the implementation method should be the same.
When I don't use the ItemTemplate everything works fine.
However, when I use the ItemTemplate I get the following error:
"Exception Details: System.NullRefe renceException: Object reference
not set to an instance of an object."
On the code line
lCollaborator.T ext = tcCollaborator. DataItem("Name" ).ToString &
tcCollaborator. DataItem("City" ).ToString
I think my problem is really the binding method.
Could someone please tell me what might be wrong? I also tried using
the Asp.Net 2.0 GridView and I am getting the same problems.
I post my VB.NET code. Please, fell free to answer in VB.NET or C#.
GRID.aspx.vb
1 Partial Class Grid
2 Inherits System.Web.UI.P age
3
4 ' -- [Controls] -------------------------------------------
5
6 Protected WithEvents cagCollaborator s As New
ComponentArt.We b.UI.Grid
7
8
9 ' -- [Events and Methods]
-------------------------------------------
10
11 ' Page_Init
12 Protected Sub Page_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Init
13
14 phGrid.Controls .Add(cagCollabo rators)
15
16 End Sub ' Page_Init
17
18 ' Page_Load
19 Protected Sub Page_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Load
20
21 ' Create grid server template
22 Dim cagstCollaborat ors As ComponentArt.We b.UI.GridServer Template
= New ComponentArt.We b.UI.GridServer Template()
23
24 ' Create grid template
25 Dim gitCollaborator s As GridITemplate = New GridITemplate
26 cagstCollaborat ors.Template = gitCollaborator s
27 cagstCollaborat ors.ID = "cagstCollabora tors"
28
29 ' Add grid server template to grid
30 cagCollaborator s.ServerTemplat es.Add(cagstCol laborators)
31
32 ' Create grid column
33 Dim gcCollaborators As ComponentArt.We b.UI.GridColumn = New
ComponentArt.We b.UI.GridColumn
34 gcCollaborators .DataCellServer TemplateId = "cagstCollabora tors"
35
36 ' Create grid level
37 Dim glCollaborators As New ComponentArt.We b.UI.GridLevel
38 glCollaborators .Columns.Add(gc Collaborators)
39
40 ' Add grid level to grid
41 cagCollaborator s.Levels.Add(gl Collaborators)
42
43 ' Bind grid
44 cagCollaborator s.DataBind()
45
46 End Sub ' Page_Load
47
48 ' {Grid} ...
49
50 ' cagCollaborator s_Init
51 Private Sub cagCollaborator s_Init(ByVal sender As Object, ByVal e
As EventArgs) Handles cagCollaborator s.Init
52
53 ' Define cagCollaborator s properties
54 With cagCollaborator s
55 .ApplyStyleShee tSkin(Me.Page)
56 .AllowEditing = False
57 .ID = "cagCollaborato rs"
58 .RunningMode = GridRunningMode .Client
59 .ShowHeader = False
60 .Width = Unit.Percentage (100)
61 End With
62
63 End Sub ' cagCollaborator s_Init
64
65 ' cagCollaborator s_NeedRebind
66 Public Sub cagCollaborator s_NeedRebind(By Val sender As Object,
ByVal oArgs As System.EventArg s) Handles cagCollaborator s.NeedRebind
67
68 ' Bind grid
69 cagCollaborator s.DataBind()
70
71 End Sub ' cagCollaborator s_NeedRebind
72
73 ' cagCollaborator s_NeedDataSourc e
74 Public Sub cagCollaborator s_NeedDataSourc e(ByVal sender As
Object, ByVal oArgs As System.EventArg s) Handles
cagCollaborator s.NeedDataSourc e
75
76 ' Define collaborators grid datasource
77 cagCollaborator s.DataSource = Collaborators()
78
79 ' Bind collaborators grid
80 cagCollaborator s.DataBind()
81
82 End Sub ' cagCollaborator s_NeedDataSourc e
83
84 ' cagCollaborator s_PageIndexChan ged
85 Public Sub cagCollaborator s_PageIndexChan ged(ByVal sender As
Object, ByVal oArgs As
ComponentArt.We b.UI.GridPageIn dexChangedEvent Args) Handles
cagCollaborator s.PageIndexChan ged
86
87 ' Define current page index
88 cagCollaborator s.CurrentPageIn dex = oArgs.NewIndex
89
90 End Sub ' cagCollaborator s_PageIndexChan ged
91
92 ' Collaborators
93 Public Shared Function Collaborators() As DataTable
94
95 ' Create collaborators data table
96 Dim dtCollaborators As New DataTable
97
98 ' Add columns to collaborators data table
99 With dtCollaborators .Columns
100 .Add(New DataColumn("Nam e", GetType(String) ))
101 .Add(New DataColumn("Mob ile", GetType(String) ))
102 .Add(New DataColumn("Ema il", GetType(String) ))
103 .Add(New DataColumn("Cit y", GetType(String) ))
104 End With
105
106 ' Create and add a new collaborator row
107 Dim drRow01 As DataRow
108 drRow01 = dtCollaborators .NewRow
109
110 ' Define collaborator row values
111 drRow01("Name") = "John"
112 drRow01("Mobile ") = "983498223"
113 drRow01("Email" ) = "jo**@mydomain. com"
114 drRow01("City") = "New York"
115
116 ' Add row to collaborators data table
117 dtCollaborators .Rows.Add(drRow 01)
118
119 ' Create and add a new collaborator row
120 Dim drRow02 As DataRow
121 drRow02 = dtCollaborators .NewRow
122
123 ' Define collaborator row values
124 drRow02("Name") = "Andrew"
125 drRow02("Mobile ") = "983498223"
126 drRow02("Email" ) = "an****@mydomai n.com"
127 drRow02("City") = "Paris"
128
129 ' Add row to collaborators data table
130 dtCollaborators .Rows.Add(drRow 02)
131
132 ' Return collaborators data table
133 Return dtCollaborators
134
135 End Function ' Collaborators
136
137
138 ' -- [Sub Classes] -------------------------------------------
139
140 Public Class GridITemplate
141 Implements System.Web.UI.I Template
142
143
144 ' -- [Controls] -------------------------------------------
145
146 ' ---- {ASP.NET} ----
147
148 ' {Label} ...
149 Protected WithEvents lCollaborator As New Label
150
151 ' -- [Events and Methods]
-------------------------------------------
152
153 ' {Label} ...
154
155 ' lCollaborator_I nit
156 Private Sub lCollaborator_I nit(ByVal sender As Object, ByVal e As
System.EventArg s) Handles lCollaborator.I nit
157
158 ' Define lCollaborator properties
159 With lCollaborator
160 .CssClass = Me.CssClass
161 .ID = "lCollabora tor"
162 End With
163
164 End Sub ' lCollaborator_I nit
165
166
167 ' ---- {ASP.NET Item Template} ----
168
169 ' InstantiateIn
170 Public Sub InstantiateIn(B yVal container As Control) Implements
ITemplate.Insta ntiateIn
171
172 ' Define template container
173 Dim tcCollaborator As
ComponentArt.We b.UI.GridServer TemplateContain er
174 tcCollaborator = CType(container ,
ComponentArt.We b.UI.GridServer TemplateContain er)
175
176 ' Define lCollaborator text
177 lCollaborator.T ext = tcCollaborator. DataItem("Name" ).ToString &
tcCollaborator. DataItem("City" ).ToString
178
179 ' Add item template child controls
180 container.Contr ols.Add(lCollab orator)
181
182 End Sub ' InstantiateIn
183
184 End Class ' GridITemplate
185
186 End Class
187
188
189
Thank You Very Much,
Miguel