Solved

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

Posted on 2011-09-13
6
507 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
  • 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now