467,907 Members | 1,432 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,907 developers. It's quick & easy.

e.Item.DataItem in a ItemDataBound event (DataGrid)

I swear I've made this work no problem before and I'm not sure what's
gone wrong.

I have a form with a datagrid that I'm binding to a DatTable I generate
on the fly. If I just bind and load the page I can see all my data fine.

Now I want to add a textbox for each row and pre-populate that box with
values from my DataTable. So I created a OnItemDataBound event and it's
firing. My problem is that any time I try to reference data in the
source table I get a runtime error NullReferenceException.

Below is my ItemDataBound event. Can anyone see what my mistake(s)
is/are? It pukes when it hits that Case statement that refers to
e.Item.DataItem. I don't get it. Thanks!

Sub dgGuest_ItemDataBound(ByVal source As Object, ByVal e As
DataGridItemEventArgs)
Dim editCell As TableCell = e.Item.Controls(1), tb As New TextBox

'only bother with non-header or footer items
If (e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem) Then
End If

Select Case e.Item.DataItem("ColName")
Case "birth_date"
tb.Text = CType(e.Item.DataItem("val"),
Date).ToShortDateString
editCell.Controls.Add(tb)
Case Else
tb.Text = Convert.ToString(e.Item.DataItem("val"))
editCell.Controls.Add(tb)
End Select

End Sub
Nov 19 '05 #1
  • viewed: 24248
Share:
3 Replies
Pls put that inside
If (e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim editCell As TableCell = e.Item.Controls(1), tb As New TextBox

End If

"MattB" wrote:
I swear I've made this work no problem before and I'm not sure what's
gone wrong.

I have a form with a datagrid that I'm binding to a DatTable I generate
on the fly. If I just bind and load the page I can see all my data fine.

Now I want to add a textbox for each row and pre-populate that box with
values from my DataTable. So I created a OnItemDataBound event and it's
firing. My problem is that any time I try to reference data in the
source table I get a runtime error NullReferenceException.

Below is my ItemDataBound event. Can anyone see what my mistake(s)
is/are? It pukes when it hits that Case statement that refers to
e.Item.DataItem. I don't get it. Thanks!

Sub dgGuest_ItemDataBound(ByVal source As Object, ByVal e As
DataGridItemEventArgs)
Dim editCell As TableCell = e.Item.Controls(1), tb As New TextBox

'only bother with non-header or footer items
If (e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem) Then
End If

Select Case e.Item.DataItem("ColName")
Case "birth_date"
tb.Text = CType(e.Item.DataItem("val"),
Date).ToShortDateString
editCell.Controls.Add(tb)
Case Else
tb.Text = Convert.ToString(e.Item.DataItem("val"))
editCell.Controls.Add(tb)
End Select

End Sub

Nov 19 '05 #2
Hello MattB,

Clearly, the problem here is that there is nothing inside your If condition.
Put the End If after the End Select.

The reason for this is that the first item is ListItemType.Header and it
does not have an e.Item.DataItem.

--
Matt Berther
http://www.mattberther.com
I swear I've made this work no problem before and I'm not sure what's
gone wrong.

I have a form with a datagrid that I'm binding to a DatTable I
generate on the fly. If I just bind and load the page I can see all my
data fine.

Now I want to add a textbox for each row and pre-populate that box
with values from my DataTable. So I created a OnItemDataBound event
and it's firing. My problem is that any time I try to reference data
in the source table I get a runtime error NullReferenceException.

Below is my ItemDataBound event. Can anyone see what my mistake(s)
is/are? It pukes when it hits that Case statement that refers to
e.Item.DataItem. I don't get it. Thanks!

Sub dgGuest_ItemDataBound(ByVal source As Object, ByVal e As
DataGridItemEventArgs)
Dim editCell As TableCell = e.Item.Controls(1), tb As New
TextBox
'only bother with non-header or footer items
If (e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem) Then
End If
Select Case e.Item.DataItem("ColName")
Case "birth_date"
tb.Text = CType(e.Item.DataItem("val"),
Date).ToShortDateString
editCell.Controls.Add(tb)
Case Else
tb.Text = Convert.ToString(e.Item.DataItem("val"))
editCell.Controls.Add(tb)
End Select
End Sub

Nov 19 '05 #3
Thanks to you both! Stupid mistake and I don't know why I didn't see that...

Matt

Matt Berther wrote:
Hello MattB,

Clearly, the problem here is that there is nothing inside your If
condition. Put the End If after the End Select.

The reason for this is that the first item is ListItemType.Header and it
does not have an e.Item.DataItem.

--
Matt Berther
http://www.mattberther.com
I swear I've made this work no problem before and I'm not sure what's
gone wrong.

I have a form with a datagrid that I'm binding to a DatTable I
generate on the fly. If I just bind and load the page I can see all my
data fine.

Now I want to add a textbox for each row and pre-populate that box
with values from my DataTable. So I created a OnItemDataBound event
and it's firing. My problem is that any time I try to reference data
in the source table I get a runtime error NullReferenceException.

Below is my ItemDataBound event. Can anyone see what my mistake(s)
is/are? It pukes when it hits that Case statement that refers to
e.Item.DataItem. I don't get it. Thanks!

Sub dgGuest_ItemDataBound(ByVal source As Object, ByVal e As
DataGridItemEventArgs)
Dim editCell As TableCell = e.Item.Controls(1), tb As New
TextBox
'only bother with non-header or footer items
If (e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem) Then
End If
Select Case e.Item.DataItem("ColName")
Case "birth_date"
tb.Text = CType(e.Item.DataItem("val"),
Date).ToShortDateString
editCell.Controls.Add(tb)
Case Else
tb.Text = Convert.ToString(e.Item.DataItem("val"))
editCell.Controls.Add(tb)
End Select
End Sub


Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by localhost | last post: by
reply views Thread by Chris Fink | last post: by
1 post views Thread by Roy | last post: by
2 posts views Thread by Eric | last post: by
1 post views Thread by GaryDean | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.