Need help with FindControl in DataGrid

T

Terry Olsen

I've got a datagrid set up to display data. I've also got an
Edit,Update,Cancel column set up to allow editing of data.

I've got a DropDownList (ID="ddl3")in the EditItemTemplate for a certain
column that I need to populate while in Edit Mode.

Two questions: Is there a way to directly use another DropDownList as a
DataSource? If so how?

I'm using the following code to try to populate the DropDownList, but
getting an "Object Reference not set to an Instance of an Object" error
when trying to add items to it. The column is the 4th column (index=3).

If I remove the code after the "LoadGrid()" line, then everything
populates fine except for the DropDownList, which is empty.

Hopefully someone can help me get this code working.

-----------------------------------------
Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
btnAddServer.Visible = False
LoadGrid()
Dim ddl As DropDownList =
CType(e.Item.Cells(3).FindControl("ddl3"), DropDownList)
For i As Integer = 0 To ddlTechs.Items.Count - 1
ddl.Items.Add(ddlTechs.Items.Item(i).Value)
Next
End Sub
 
S

Scott Allen

Hi Terry:

One tip I can offer is to use FindControl on the Item instead of on a
Cell. This allows the column to move and you don't need to change your
code, i.e. e.Item.FindControl("..."). I have a little article with
more info: http://odetocode.com/Articles/116.aspx

Also, you might want to step through with the debugger and find out
exactly what variable is undefined (Nothing).
 
T

Terry Olsen

Thanks for the tip. I'll check it out and see what I can do.

I moved the code over to the Update Event and it can find the control
just fine. Does the control not exist until AFTER the DataGrid is
rendered and we exit the Edit Event Handler? If so, how would I
populate it so that the user can select an item from it before clicking
the Update button?
 
S

Scott Allen

Hi Terry:

Since you are pulling items from another drop down list it would
depend on the order of events and when the items appear in the other
list. Are you using databinding to populate ddlTechs?
 
T

Terry Olsen

No. I'm pulling the items in ddlTechs from an SQL database, but ddlTechs is
not bound to anything. ddlTechs is a drop down list that appears on the page
outside of the Datagrid. When I want to edit a row inside the DataGrid, I
want the DropDownList in the DataGrid to populate its items from the
ddlTechs control. But I'm not getting that far.

Response.write(Ctype(e.items.FindControl("ddl3").GetType.ToString)) <--
Typed from memory, may not be correct.

This works fine in the Update event and shows "DropDownList". But when I try
the same thing in the Edit Event, I get the "Object Reference Not Set..."
error.
 
S

Scott Allen

Hi Terry:

Apologies for not getting thorugh to me, I can see now the edit event
must fire before the drop down exists, then you reload the grid, but
of course e.Item must still be pointing to the old item withought a
drop down list.

A solution would be to override ItemDataBound on the DataGrid, then
when the edit item is being bound (e.Item.ItemType ==
ListItemType.EditItem) you can FindControl on the list and populate
it.

Make sense?
 
T

Terry Olsen

Well, just reading it here doesn't make sense. Let me go play with it and
get back to you.
 
E

Elton W

Hi Terry,

It's little bit tricky problem when using FindControl in
EditCommand event. You know that the editable DatagridItem
has two states, normal and edit. In normal state, it shows
stuff in ItemTemplate tags, on the other hand, in edit
state, it shows stuff in EditItemTemplate tags.
Accordingly using FindControl method you can only lookup
corresponding controls. In EditCommand event, after

DataGrid.EditItemIndex = e.Item.ItemIndex
Datagrid.DataSource = data_source_object
Datagrid.DataBind()

The datagrid.Items(e.Item.ItemIndex) becomes edit item,
but e.Item still keeps in normal state. Hence if you use
datagrid.Items(e.Item.ItemIndex).FindControl("ddl3"), it
will work.

BTW, Scott's suggestion to use DataGridItem.FindControl is
much better than use TableCell.FindControl, unless you
have different controls with same ID in different cells.


HTH

Elton Wang
(e-mail address removed)
 
T

Terry Olsen

You guys are awesome! I used both suggestions and got the routine
working. Here is the code that works:

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
btnAddServer.Visible = False
LoadGrid()
Dim ddl As DropDownList =
CType(DataGrid1.Items(e.Item.ItemIndex).FindControl("ddl3"),
DropDownList)
For i As Integer = 0 To ddlTechs.Items.Count - 1
ddl.Items.Add(ddlTechs.Items.Item(i).Value)
Next
End Sub

Thanks again!
 
T

Terry Olsen

Okay, now i've got my code completely working. I wish I better
understood the scope of the various ways of doing it (cells vs. items,
etc). Anyhow, here's the completed code for the Edit event. Notice the
last 2 lines concerning the label. I'm taking the value from the label
in the Normal Template and using it to change the SelectedValue in the
DropDownList. Is this the "preferred" way, or is there a better way (as
was pointed out earlier in this thread) of finding the Label Control?

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
btnAddServer.Visible = False
LoadGrid()
Dim ddl As DropDownList =
CType(DataGrid1.Items(e.Item.ItemIndex).FindControl("ddl3"),
DropDownList)
For i As Integer = 0 To ddlTechs.Items.Count - 1
ddl.Items.Add(ddlTechs.Items.Item(i).Value)
Next
Dim lb As Label = CType(e.Item.Cells(3).FindControl("Label3"),
Label)
ddl.SelectedValue = lb.Text
End Sub
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top