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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting type DataRow to Control

Status
Not open for further replies.

gregburningham

Programmer
Jul 12, 2000
40
0
0
GB
Hi

I have some code where I read a list of data items from a DataTable. Each of these rows although stored as a string is also a Control on my page. I would therefore like to convert the row.column returned from my loop into a Control that I can set properties on (such as enabled=true) etc.

Unfortunately I am getting an "Invalid Cast" error when I execute the following code ....



While vRowCount < vDataTable.Rows.Count
vDataRow = vDataTable.Rows(vRowCount)
lblMessage.Text = lblMessage.Text & vDataRow(&quot;UpdateAllowed&quot;)
If vDataRow(&quot;UpdateAllowed&quot;).ToString = &quot;Y&quot; Then
lblMessage.Text = lblMessage.Text & &quot;UPD!&quot;

vControl = CType(vDataRow(&quot;ItemName&quot;), DropDownList)
vControl.Enabled = True
End If
vRowCount = vRowCount + 1
End While

Please can someone advise me on where I am going wrong ?

Thanks in advance

Greg B



 
CType Function - Returns the result of explicitly converting an expression to a specified data type, object, structure, class, or interface.

CType(expression, typename)
expression - Any valid expression. If the value of expression is outside the range allowed by typename, an error occurs.
typename - Any expression that is legal within an As clause in a Dim statement, that is, the name of any data type, object, structure, class, or interface.

You are trying to convert a string (returned by 'vDataRow(&quot;ItemName&quot;)') to a DropDownList which is not possible.

I'm not exactly sure what it is you're trying to accomplish?

If you want to add a new DropDownList:

Code:
Dim vControl As System.Web.UI.WebControls.DropDownList
vControl = New System.Web.UI.WebControls.DropDownList()
vControl.ID = vDataRow(&quot;ItemName&quot;)
Me.FindControl(&quot;Form1&quot;).Controls.Add(vControl)

-Stephen Paszt
_________________________________________________

Man usually avoids attributing cleverness to somebody else
- unless it is an enemy. -- Albert Einstein

 
Thanks for the advice what I am actually doing is the following:

I am returning a text string eg. &quot;ManagerDropDownList&quot; from my table

I then want to compare this text string with any controls (ID's) on the form to see if a control with a corresponding ID exists - then I will set that controls property ...
 

Oh, I see. Try this:

Code:
Dim wctrl As System.Web.UI.WebControls.WebControl
For Each wctrl In Me.FindControl(&quot;Form1&quot;).Controls
    If wctrl.ID = vDataRow(&quot;ItemName&quot;) Then
        'set controls properties
    End If
Next

-Stephen Paszt
____________________________________________________
&quot;God does not care about our mathematical difficulties. He integrates empirically.&quot; -- Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top