Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CType Problems

Status
Not open for further replies.

ColinM

Programmer
Jun 29, 2000
189
TH
Hi,
I'm fairly new to ASP.NET to hopefully an easy one to solve :)
Whats wrong with this?
Code:
Dim Resolved as Boolean = CType(e.Item.Cells(4).Controls(0), Checkbox).Checked

I'm trying to store the value from a datagrid for an update in a datagrid. It works for text but not for a checkbox
Code:
System.InvalidCastException: Specified cast is not valid.
 
I've done this but with a few extra steps:

Dim chkCheck as new Checkbox
Dim Resolved as Boolean

chkCheck = CType(e.Item.Cells(4).Controls(0), Checkbox)
Resolved = chkCheck.Checked

Also, the cell is usually considered the first control, so to get the checkbox you should try using
e.Item.Cells(4).Controls(1)

hth

D'Arcy
 
Yeah, that'll do nicely, thanks
I've got this now

Code:
Dim EnteredOn as DateTime = CType(CType(e.Item.Cells(3).Controls(0), TextBox).Text , datetime)
Dim Resolved as Boolean = Ctype( CType(e.Item.Cells(4).Controls(1), Checkbox).Checked , boolean)

and it works (just about)

Although I don't really understand why their should be a difference between the controls(num) for a textbox as opposed to a checkbox.
Changing controls(0) to controls(1) for the textbox results in an out of range exception.

 
Are you sure that its the textbox that its grabbing?

if you have any text in the cell, it might be grabbing the cell and casting it into a textbox.

Just a thought anyway
:)

D'Arcy
 
AAah, I think I see what you mean.

Better do some more reading.
 
Small point. You do not need New on the Dim statement
Dim chkCheck as new Checkbox
because chkCheck is not used until after
chkCheck = CType(e.Item.Cells(4).Controls(0), Checkbox)
which sets the variable to an existing instance of a checkbox. Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top