dataitem in SelectedIndexChanged not recognized

Discussion in 'ASP .Net Datagrid Control' started by aamirghanchi@gmail.com, Mar 9, 2007.

  1. Guest

    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)
    , Mar 9, 2007
    #1
    1. Advertising

  2. Guest

    On Mar 9, 11:01 am, wrote:
    > 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."
    , Mar 9, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S. Justin Gengo
    Replies:
    0
    Views:
    496
    S. Justin Gengo
    Aug 26, 2003
  2. Zenobia
    Replies:
    1
    Views:
    7,212
    Zenobia
    Jul 6, 2004
  3. Nathan Sokalski
    Replies:
    1
    Views:
    480
    Teemu Keiski
    Sep 13, 2007
  4. Nathan Sokalski
    Replies:
    1
    Views:
    987
    Teemu Keiski
    Sep 13, 2007
  5. Nathan Sokalski
    Replies:
    1
    Views:
    207
    Teemu Keiski
    Sep 13, 2007
Loading...

Share This Page