dataitem in SelectedIndexChanged not recognized

A

aamirghanchi

Hi,


I have scoured this user group for this answer to no avail. I am
trying to get the Datatitem property of the DataGridItem in the
SelectedIndexChanged event handler of a Dropdownlist in a
Templatecolumn of a DataGrid (mouthfull huh!).

Due to client requirements, I am not doing it through EditColumn way.
User does not want to go through clicking Edit and Update buttons for
selection change. Therefore the DropdownList is in the ItemTemplate of
Templatecolumn.

The DataGrid is being bind in the Page_Load event dynamically to a
datasource.
In the SelectedIndexChanged handler I am trying to get the Primary key
column value in the DataItem so that it can be used to update the
database table row. I am able to get to the DataGridItem though, but
further than that it blows up on me when I try to get the DataItem out
of DataGridItem with NullReferenceException: just to keep it simple I
am casting the DataItem into DataRowView and trying to get the
GetType() from it. Here is the code snippet:

Public Sub ddlMyDropDownList_OnSelectedIndexChanged(ByVal sender As
Object, ByVal e As EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim dgi As DataGridItem = CType(ddl.NamingContainer, DataGridItem)
Dim strObjType As String = CType(dgi.DataItem,
DataRowView).GetType().ToString()
End Sub
It raises NullReferenceException on he last line before End Sub
"Object reference not set to an instance of an object.
System.NullReferenceException"

Does anyone know of a reason why it won't recognize the DataItem
object of the DataGridItem in SelectedIndexchanged event?

Will appreciate any leads.

Thanks

(Doing it in .Net 1.1 and VB grudgingly on a legacy web app)
 
A

aamirghanchi

Hi,

I have scoured this user group for this answer to no avail. I am
trying to get the Datatitem property of the DataGridItem in the
SelectedIndexChanged event handler of a Dropdownlist in a
Templatecolumn of a DataGrid (mouthfull huh!).

Due to client requirements, I am not doing it through EditColumn way.
User does not want to go through clicking Edit and Update buttons for
selection change. Therefore the DropdownList is in the ItemTemplate of
Templatecolumn.

The DataGrid is being bind in the Page_Load event dynamically to a
datasource.
In the SelectedIndexChanged handler I am trying to get the Primary key
column value in the DataItem so that it can be used to update the
database table row. I am able to get to the DataGridItem though, but
further than that it blows up on me when I try to get the DataItem out
of DataGridItem with NullReferenceException: just to keep it simple I
am casting the DataItem into DataRowView and trying to get the
GetType() from it. Here is the code snippet:

Public Sub ddlMyDropDownList_OnSelectedIndexChanged(ByVal sender As
Object, ByVal e As EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim dgi As DataGridItem = CType(ddl.NamingContainer, DataGridItem)
Dim strObjType As String = CType(dgi.DataItem,
DataRowView).GetType().ToString()
End Sub
It raises NullReferenceException on he last line before End Sub
"Object reference not set to an instance of an object.
System.NullReferenceException"

Does anyone know of a reason why it won't recognize the DataItem
object of the DataGridItem in SelectedIndexchanged event?

Will appreciate any leads.

Thanks

(Doing it in .Net 1.1 and VB grudgingly on a legacy web app)

answering my own post.
I guess the DataItem object is no more available right after the
ItemDataBound event of DataGrid as it says in the documentation
ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/
frlrfSystemWebUIWebControlsDataGridClassItemDataBoundTopic.htm

"After this event is raised, the data item is nulled out and no longer
available."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,818
Messages
2,569,727
Members
45,664
Latest member
Phil79581

Latest Threads

Top