Solved

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

Posted on 2011-09-13
6
514 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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