Solved

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

Posted on 2011-09-13
6
516 Views
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



        Response.Redirect("http://localhost:24820/UserDetails.aspx")

    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.
0
Comment
Question by:codefinger
[X]
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
6 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 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

0
 

Author Comment

by:codefinger
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?

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36532184
I think you can use column name in place of the column index.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:codefinger
ID: 36532289
CodeCruiser:

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
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36532334
Try

GridViewRegistrants.SelectedRow.Cells("LastName").Text

OR

GridViewRegistrants.Rows(GridViewRegistrants.SelectedIndex).Cells("LastName").Text
0
 

Author Closing Comment

by:codefinger
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.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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