Add blank item to dropdown list in datagrid

B

Big Dave

I know it's been asked a million times before, but I still can't seem to
find an answer that works.

I've got a dropdown list in the footer template of a datagrid. The
dropdown list databinds, but I can't seem to add a blank item to the top
of the dropdown list.

I've tried using the code to add this by placing it in both the
page_load and the datagrid.itemDataBound event, but neither seems to
work. I've also referenced the control by
ctype(e.items.findControl("ddlName"), Dropdownlist) and by it's actual
id. I've tried both the .add and .insert method's of the
dropdownlist.items collection.

I've been able to get this to work outside of the datagrid, but can't
seem to get it to work inside the datagrid. Any help would be greatly
appreciated!!!

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Footer Then
'option 1 - doesn't work
CType(e.Item.FindControl("cmbAddFeatureType2"),
DropDownList).Items.Insert(0, New ListItem("Select", ""))
'option 2 - doesn't work
Me.cmbAddFeatureType2.Items.Insert(0, New ListItem("Select",
""))
End If
End Sub

Big Dave
 
E

Eliyahu Goldin

Big Dave,

If you want to add the item into the ddl inside the grid, you should use
PreRender event. But I don't think it's the best idea. You can achieve the
same effect by adding an empty row to the datatable the ddl is bound to.
That's what I am doing in my projects.

Eliyahu
 
B

Big Dave

Okay, I tried this, it still didn't work.


Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.PreRender
Me.cmbAddFeatureType2.Items.Insert(0, New ListItem("Select",
""))
End Sub

I also tried it on the dropdownlist's prerender event, and it didn't
work. It never even fired the event.

I'm pretty hesitant about adding a blank record to my database lookup
tables. I'm certainly no expert, but that just seems like a bad idea.
Adding a blank item to the dropdown seems like it should be easy, but I
just can't find a way to do it in the datagrid. So, I'm still looking
for help, thanks!!!

Big Dave
 

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

Top