Solved

I am having trouble with data types

Posted on 2003-11-24
8
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …

739 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