PDA

View Full Version : Problem getting data from DataGrid on update


petersza
10-30-03, 07:54 PM
I have set up a DataGrid with an EditCommandColumn. When the user clicks the update button, the associated event procedure runs. Of course, one of the things that needs to be done in that procedure is to get the data that is to be updated from the current row of the grid. I am miserably confused about how to do this after reading several books, searching google, and so on. I can get the ID, which is an integer, like this:

Public Sub dgTrailers_Update(ByVal Sender As Object, ByVal e as DataGridCommandEventArgs)
Dim ID as Integer = e.Item.Cells(0).Text
End Sub

This works without any errors. But this doesn't work for the other columns, one of which is also an integer (!) and the other a string. Here's one attempt to get the data in the second column (Integer):

Public Sub dgTrailers_Update(ByVal Sender As Object, ByVal e as DataGridCommandEventArgs)
Dim TrailerYear as Integer = e.Item.Cells(1).Text
End Sub

I get this error: Input string was not in a correct format.

Here's another attempt to get the same data:

Public Sub dgTrailers_Update(ByVal Sender As Object, ByVal e as DataGridCommandEventArgs)
Dim TrailerYear as Integer = CType(e.Item.Cells(1).Controls(0), TextBox).Text
End Sub

I get this error: Specified cast is not valid.

Now, here's a similar attempt to get the String value in the third column, which gives me the same "Specified cast is not valid" error.

Public Sub dgTrailers_Update(ByVal Sender As Object, ByVal e as DataGridCommandEventArgs)
Dim TrailerMake as String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
End Sub


I'd like to know:

1. Specifically, what am I doing wrong in this case?
2. In general, what is the proper syntax for retrieving data for the different data types? Can anyone provide a comprehensive explanation of this, or point me to somewhere that explains it clearly so that the day that I need to grab some other kind of data from the grid, I will be able to?

Thank you!

Peter

Pvialoux
11-17-03, 11:48 PM
I have been having a similar problem.
However, on reviewing e.item.cells(2).controls(0) in quickwatch, I discovered that it was NOT a textbox!
I checked e.item.cells(2).controls(1) and this was a textbox.
I have not created any other controls that I am aware of in this cell. But, when I tested the code with the controls(1) reference it worked.
I'm still working on the "Why"

devnoronha
12-19-03, 07:37 AM
here's what i think is wrong . the cells collection starts from iterating from 0 , not 1 . My guess is that the databound column is the first one , not the second . so, instead of
Dim TrailerYear as Integer = CType(e.Item.Cells(1).Controls(0), TextBox).Text
try
Dim TrailerYear as Integer = CType(e.Item.Cells(0).Controls(0), TextBox).Text

see if that works and tell me .

Rbytez
05-07-04, 03:48 PM
This are the errors was i found in your code..

Public Sub dgTrailers_Update(ByVal Sender As Object, ByVal e as DataGridCommandEventArgs)
Dim ID as Integer = e.Item.Cells(0).Text
End Sub

This works without any errors. But this doesn't work for the other columns, one of which is also an integer (!) and the other a string. Here's one attempt to get the data in the second column (Integer):

THIS WORK , BECUASE THE FIRST COLUMN IS "READ ONLY", I GUESS.
IN THE OTHER COLUMS YOU HAVE USE THE Val() FUNCTION FOR THE COLUMNS WHICH ARE INTEGER, FOR EXAMPLE :


Public Sub dgTrailers_Update(ByVal Sender As Object, ByVal e as DataGridCommandEventArgs)
Dim TrailerYear as Integer = VAL(e.Item.Cells(1).Text)
End Sub

Public Sub dgTrailers_Update(ByVal Sender As Object, ByVal e as DataGridCommandEventArgs)
Dim TrailerYear as Integer = VAL(CType(e.Item.Cells(1).Controls(0), TextBox).Text)
End Sub

HERE, I DONT KNOW WHY NOT WORK, WHICH IS THE NUMBER OF THE EDITCOLUMN?, MAYBE YOU HAVE WRONG INDEX, CHECK IT

Public Sub dgTrailers_Update(ByVal Sender As Object, ByVal e as DataGridCommandEventArgs)
Dim TrailerMake as String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
End Sub

GREETINGS FROM CHILE

alexsaves
09-17-04, 02:40 PM
I'm not sure that's the solution.. will think about this

----------------------
ASP Grid control - http://www.aspgrids.com

thuenb
10-20-04, 12:37 AM
add the following into your code before the error line to see if you can figure out the type of control it thinks you are actually trying to work with...

Then you can change the values around until you get the correct control...
response.write(e.Item.Cells(2).Controls(0).GetType .ToString)

Hope this helps...