Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Data Bound Drop Downs in a Datagrid Footer.

I have a list of things in a database that I want in a drop down list, the list is buried in the footer of a datagrid and so I am using this method of calling a function to get the data.

<asp:DropDownList Runat="server" ID="DropDownList" AutoPostBack="true" onSelectedIndexChanged="footerchanged"                            DataTextField="OTCDrug" DataSource="<%# db.GetOTCDrugs %>" />

the meat of the  db.GetOTCDrugs  function is this

          sqlDataAdapter = New sqlDataAdapter(objCommand)
          ds = New DataSet()

          'populate the DataSet

          Return ds

I would like to be able to insert a "please make a selection" at the top of my drop down, but I am not sure how to do it! It is not an option to have a db entry. My current plan is to create a row and add it into the dataset, but I cannot get this to work.

Thursday, May 12, 2005

Put in an onItemDataBound event for your datagrid, and then look to see if your event type is for a footer row.  I'm not sure of the exact syntax, but you will be able to work it out I'm sure.

Then, use findcontrol to refer to the dropdown box, and use the insert method to add the new entry at the top.

onItemDataBound fires for every row in the data grid - header, item, alternating item, footer.

Ken Ray
Thursday, May 12, 2005

Thank you!

This is the code if you are trying to do a similar thing :)

in the datagrid definition have "OnItemDataBound = databound"

Then wherever you keep your code.

    Sub databound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
        If e.Item.ItemType = ListItemType.Footer Then
          dim dd as DropDownList
            dd = CType(e.Item.FindControl("DropDownList"), DropDownList)
            dd.Items.Insert(0, New ListItem("Make a selection", "0"))

        End If
    End Sub

Friday, May 13, 2005

Is there any way on this forum to display code in a bit more friendly fashion!

another forum i bother has [code] tags which displays the code in monotype and with a different background.

Friday, May 13, 2005

*  Recent Topics

*  Fog Creek Home