Click to See Complete Forum and Search --> : How can I dynamically add items to a dropdownlist in a datagrid edit mode ?


Robert Chu
09-26-2006, 09:51 PM
Hello,
I want to dynamically add items to a dropdownlist in a datagrid. I wrote the following code. There is an error message:object reference not set to an instance of an object. How can I fix it ? Thanks.

Private Sub dgdMessageVoxDetail_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgdMessageVoxDetail.EditCommand

Dim ddlVoxNoReference As DropDownList

dgdMessageVoxDetail.EditItemIndex = e.Item.ItemIndex
ddlVoxNoReference = e.Item.FindControl("ddlDgdVoxNo")

ddlVoxNoReference.Items.Add("1")
ddlVoxNoReference.Items.Add("2")
bindDgdMessageVoxDetail()
End Sub

drallab
09-27-2006, 08:52 AM
The object reference error indicates that you are trying to use an object it cannot find.

Your problem could be reference of the dropdownlist in the datagrid.

Try combining "Dim ddlVoxNoReference As DropDownList" and "ddlVoxNoReference = e.Item.FindControl("ddlDgdVoxNo")" into one statement:
Dim ddlVoxNoReference As DropDownList = CType(e.item.findcontrol("ddlDgdVoxNo"), DropDownList)

That should eliminate the object reference error as long as the control name in the datagrid matches the control name in the findcontrol("XXX") part.

:)