DataItem on bound datagrid is nothing....why?

Posted on 2011-09-13
Medium Priority
Last Modified: 2012-05-12
On a web form, I have a grid bound to a list of business objects.   The grid is not editable but it has two auto-generated buttons, Edit and Select.   After clicking Select, I click Edit on the same row activates the code attached....

Private Sub GridViewRegistrants_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridViewRegistrants.RowEditing

        Session("PatientDetails") = GridViewRegistrants.SelectedRow.DataItem


    End Sub

Open in new window

But for some reason, debugging shows the value of DataItem to be nothing, even though SelectedRow is not.    (Yeah, I know I need to fix localhost, I will get to that soon...)

What am I doing wrong?   How do I get a reference to the underlying object?   That is what I expected DataItem to provide.
Question by:codefinger
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 39

Accepted Solution

Pratima Pharande earned 2000 total points
ID: 36528122
I think you need something like this
mention the number of cell you want to get result Cells.Item(4)

 Dim strString As String

        strString= GridView1.SelectedRow.Cells.Item(4).Text.ToString


Author Comment

ID: 36530025

That will work.  

I found out the reason my approach does not is that a web form does not maintain a connection to the list of items  (my last two projects were desktop winforms applications...I am spoiled!)
It uses the ViewState, so DataItem = Nothing is actually true.

My displayed column order is going to be changing around a lot as I work on the display.  Is there any way to reference the values by names instead of numbers?

LVL 83

Expert Comment

ID: 36532184
I think you can use column name in place of the column index.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 36532289

I thought I remembered that as well, but it does not work:

GridViewRegistrants.SelectedRow.Cells.Item("LastName").Text      {"Conversion from string "LastName" to type 'Integer' is not valid."}      System.InvalidCastException
LVL 83

Expert Comment

ID: 36532334




Author Closing Comment

ID: 36532472
I found out the reason my approach does not work is that a web form does not maintain a connection to the list of items  (my last two projects were desktop winforms applications...I am spoiled!)
It uses the ViewState, so DataItem = Nothing is actually true.

So you have to take your values from the grid as the expert described.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question