Solved

I am having trouble with data types

Posted on 2003-11-24
8
288 Views
Last Modified: 2010-04-17
I am coding the UpdateCommand of a DataGrid in a VB.NET Webform.
I am having trouble with data types.
Specifically, the following statement works fine except that I don't want to read Cells(5).

        Dim ModelIDText As TextBox = CType(e.Item.Cells(5).Controls(0), TextBox)

When I replace Cells(5) with Cells(1), I get the following error:
[InvalidCastException: Specified cast is not valid.]

Cells(5) in my datagrid is type string
Cells(1) in my datagrid is type integer

How do I get ModelID to have the integer value of Cells(1)?

Thanks
Michael
0
Comment
Question by:michael5865
  • 2
8 Comments
 
LVL 6

Accepted Solution

by:
bkthompson2112 earned 250 total points
ID: 9811983
When you change to Cells(1), you're trying to cast an integer as a textbox.  Can't do.

I'm not at a .NET installation, but this should work:

Try:
CType(CType(e.Item.Cells(1).Controls(0),String),TextBox)

The inner CType(,String) will cast the integer to a string.
The outer Ctype(,TextBox) will cast _that_ string as a TextBox

0
 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 250 total points
ID: 9812094
what are you trying to accomplish?  do you want to have a Textbox, in Cell1, that holds the Value of the ModelID?

If so, then you need to use the .ToString Method of the Integer in Cell(1)

AW
0
 

Author Comment

by:michael5865
ID: 9817610
Thanks for the help. The update is now working correctly.
To get it to work correctly, I had to make "visible" two coloums that are used as identifiers.
I believe I can set these column widths to zero so that in practice they are not visible.
I tried to do this in the properties of the datagrid but it didn't seem to make them dissappear. Can you tell me how to make two colums of a datagrid (that are required for the code but not for the user) -  invisible?

Thanks,

Michael
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 10244979
Right Click on the Datagrid, select Property Builder (I think that is the option - do not have .NET on this PC), then select column Builder.

Click on the first two columns listed (one at a time), and UNCHECK the Visible checkbox.

Those tow columns will then NOT be Visible, but will be present, and you can then get the value, as I indicated, earlier.

AW
0

Featured Post

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!

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

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