SelectedItem in a dropdownlist in a datagrid

Results 1 to 2 of 2

Thread: SelectedItem in a dropdownlist in a datagrid

  1. #1
    Join Date
    Dec 1969

    Default SelectedItem in a dropdownlist in a datagrid

    I have a dropdownlist in a DataGrids editItem Template. I want to have an item preselected when it is displayed, based on the current value of that field. <BR><BR>How can I achieve this? <BR><BR>I wrote a method to loop through the ListItems but the dropdown list hasn&#039;t even been rendered by that point! So I get a null reference error. Is there a cleaner way to do this?<BR><BR><BR>-Thanks in advance. Kevin Mesiab

  2. #2
    Join Date
    Dec 1969

    Default RE: SelectedItem in a dropdownlist in a datagrid

    this code worked for me<BR><BR>dgenterdata: is the datagridname<BR>dept: is the database or the text value to be used as the default value<BR>ddldept is the dropdownlist i am using<BR><BR>Sub Dgenterdata_ItemDataBound(ByVal sender As Object, _<BR> ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _<BR> Handles dgenterdata.ItemDataBound<BR> If e.Item.ItemType = ListItemType.EditItem Then<BR> Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)<BR> Dim currentvalue As String = CType(drv("dept"), String)<BR> Dim ddl As DropDownList<BR> ddl = CType(e.Item.FindControl("ddldept"), DropDownList)<BR> ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(currentvalu e))<BR><BR> End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts