473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
3 24612
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: localhost | last post by:
A DataGrid with shows a label in one of the columns when in view mode. When in edit mode, I want to show a dropdown, and have the default selection set to what the textbox used to be. Right now...
0
by: Chris Fink | last post by:
I am having a problem highlighting a row in a datalist during the itemdatabound event. What is occurring is that a new td row is being wrapped around the datalist item which is highlighted (very...
1
by: Roy | last post by:
Hey all, This is a fairly broad question, but why is it that my datagrid hits the ItemDataBound event 3 times? Basically, I placed a response.write in all sub and functions in my codebehind and...
4
by: Girish | last post by:
Im trying to create a grid within a grid programmatically. Ive been successful in doing this but I need the embedded grid to fire its ItemDataBound event so I can handle it. The event does not seem...
1
by: needin4mation | last post by:
Hi, I have a datalist. In this datalist I have a datagrid. The datalist is the master. The datagrid is the detail. It works fine. I populate the datagrid inside of the datalist using the...
2
by: Eric | last post by:
Hello, I have a repeater control bound to an XmlDataSource. Works great. Now, I want to handle the ItemDataBound event--the e.Item.DataItem object in particular. Its type is...
1
by: GaryDean | last post by:
It seems every time I try to use the GridView to do what I used to do with the DataGrid I run into differences that make me want to got back to the datagrid. for instance... the DataGrid...
1
by: Nathan Sokalski | last post by:
I am trying to access the DataItem of other DataListItems in a DataList of mine from within the ItemDataBound. The code I am currently using is: CStr(CType(Me.datMain.Items(e.Item.ItemIndex -...
0
by: Suresh S | last post by:
Here i give sample code to change datagrid in ItemDataBound Event. DataRowView drv = (System.Data.DataRowView)e.Item.DataItem; string Node_MAC = drv.ToString().Trim(); ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.