Solved

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

Posted on 2011-09-13
6
509 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

21 Experts available now in Live!

Get 1:1 Help Now