Get a value of an item on a DataGrid in Edit mode

Hi,

I try to get the value of an item on datagrid in edit mode. here the code:
---- The code ----
Private Sub DataGrid_UpdateCommnd (.....)
...
   Dim ExpCode As Integer = CType(e.Item.Cells(0).Text, Integer)
...
End Sub

---- The exception ----
Run-time exception thrown : System.InvalidCastException - Cast from string "" to type 'Integer' is not valid.      
 
In a normal mode of the DataGrid I get the value I need from this field with the same code.The exception occur just in edit mode when I try to update the grid in the UpdateCommnd event.

Why?

Thanks

fdsafog56Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmarinovCommented:
Hi fdsafog56,

what does first column containt (as controls ) ?
may you have to try

Dim ExpCode As Integer = CType(e.Item.Cells(0).Controls(1).Text, Integer)


Regards!
B..M
mmarinov
0
CJ_SCommented:
Dim ExpCode As Int32 = Convert.ToInt32(e.Item.Cells(0).Text)
0
fdsafog56Author Commented:
Hi,
This column is a bound column.
For mmarinov:
I tried your code and I get an exception that 'Text' is not a member of Control.

For CJ_S :
I tried your code and I get an exception- System.FormatException - 'Input string was not in a correct format'

I tried (e.Item.Cells(0).Text) in watch window and I get in the value column : ""

What else can I try ?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

CJ_SCommented:
Add the following function

Private Function ConvertToInt(ByVal _Text as String) as Int32
  Dim _Value as Int32
  Try
    _Value = Convert.ToInt32(_Text)
  Catch
    _Value = 0
  End Try
  Return _Value
End Function

And use

Dim ExpCode As Int32 = ConvertToInt(e.Item.Cells(0).Text)



0
mmarinovCommented:
fdsafog56,

try this

Dim ExpCode As Integer = Int32.Parse(CType(e.Item.Cells(0).Controls(1), TextBox).Text)
if this errors
check what controls do youhave in the e.Item.Cells(0).Controls collection, and set the appropiate index in the line above to a textbox control within the collection

Regards!
B..M
mmarinov
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fdsafog56Author Commented:
The problem is that when I turn the grid to edit mode and then I try to update the grid the code: 'e.Item.Cells(0).Text' return an empty string always.But in normal mode when I try the delete event  for deleting a row from the grid I get with 'e.Item.Cells(0).Text'  the correct value.

Why there is a different  between normal mode and edit mode ?

Thanks

0
mmarinovCommented:
fdsafog56,
cause in normal mode the boundcolumn produce div in the current cell, but for the edit mode it produce textbox control

Regards!
B..M
mmarinov
0
fdsafog56Author Commented:
Hi all,

Here the solution :

CType(e.Item.Cells(0).Controls(0),TextBox).Text

Thanks for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.