Solved

I am having trouble with data types

Posted on 2003-11-24
8
281 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now