Referencing controls in a DataGrid

  • Thread starter mystical_potato
  • Start date
M

mystical_potato

From VS.NET 03 AND VB.NET

I added a dropdown list to a data grid control in response to the
EditCommand event to force users to pick values from a list and not
type them in. I used the following code:

'Hide the existing txt box
Dim txt As TextBox
txt = CType(e.Item.Cells(0).Controls(0), TextBox)
txt.Visible = False

'display a ddl instead
Dim ds As New DataSet
ds = dbCalls.GetData
Dim ddl As New DropDownList
ddl.ID = "myID"
ddl.DataSource = ds
ddl.DataTextField = "myTxt"
ddl.DataValueField = "myID"
ddl.DataBind()
ddl.Attributes.Add("class", "myCSS")
e.Item.Cells(0).Controls.Add(ddl)

This works great (everything displays as intended in the UI).
However, when I code the UpdateCommand I cannot find the DropDownList
control I created above. When I debug and add a watch for
e.Item.Cells(0) it only shows a single control
(e.Item.Cells(0).controls(0) which is the text box). How can I
reference the DropDownList control? I am using the following code:

Dim ddlMaintCatID As DropDownList
ddlMaintCatID = CType(e.Item.Cells(0).Controls(X), DropDownList)

For X i tried the following vales (0 and 1). For 0 I get and invalid
cast error becuase controls(0) is a text box and for 1 I get an index
out of range error.

What am I missing???

thanks in advance for your time and assistance.
Scott
 
T

Thomas Dodds

ddlMaintCatID = CType(e.Item.Cells(0).FindControl("myID"), DropDownList)

the above will work if you used

ddl.ID = "myID" and "myID" is a string, if it is the unique DB id then you
will have to substitue something to determine the currect ID (use a hidden
column and grab the text property or something)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top