Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2004-11-17
8
Medium Priority
?
1,006 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:fdsafog56
  • 3
  • 3
  • 2
8 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12602032
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
 
LVL 22

Expert Comment

by:CJ_S
ID: 12602081
Dim ExpCode As Int32 = Convert.ToInt32(e.Item.Cells(0).Text)
0
 

Author Comment

by:fdsafog56
ID: 12602366
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 22

Assisted Solution

by:CJ_S
CJ_S earned 60 total points
ID: 12602405
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
 
LVL 28

Accepted Solution

by:
mmarinov earned 90 total points
ID: 12602431
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
 

Author Comment

by:fdsafog56
ID: 12602505
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12602519
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
 

Author Comment

by:fdsafog56
ID: 12603133
Hi all,

Here the solution :

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

Thanks for your help.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

572 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