Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop down list in Datagrid

Status
Not open for further replies.

jshurst

Programmer
Joined
Oct 27, 2004
Messages
1,158
Location
US
Well I managed to make a drop down list in the datagrid. This list is in the EditItem Template column. When the normal datagrid is displayed there is a textbox containing a name. I would like to have this name preselected so that if the user hits the edit button it be preselected to the value in the textbox. Does anyone know how to do this? I can do it normally using a textbox, but am having a difficult time making this work within the datagrid. Here is how I normally do this...

[code}lstType.Items.FindByText(lblType.Text).Selected = True[/code]

However I am having to refenence the list in the datagrid like this...
Code:
e.Item.FindControl("lstType")

I don't know how to go about setting this. Thanks.
 
Hi Try this,

Public Sub SetDropDownIndex(ByVal sender As Object, ByVal e As System.EventArgs)

Dim strSubject As String
strSubject = CType(e.Item.FindControl("lblsubject"), Label).Text

Dim ddlx As System.Web.UI.WebControls.DropDownList
ddlx = sender
ddlx.SelectedIndex = ddlx.Items.IndexOf(ddlx.Items.FindByText(strSubject))

End Sub


Then in your Datagrid editItem Template..
add - OnPreRender="SetDropDownIndex" to the Dropdownlist

You will need to change strSubject to whatever yours is to find the label (or textbox) you have that shows the value when not in edit mode.

So...

Dim strSubject As String
strSubject = CType(e.Item.FindControl("YourLabel"), Label).Text

HTH
 
when editing rows, the datagrid is often rebound, so you could do the same kind of thing in the DataGrid.ItemDataBound event, and just check e.Item.ItemType for the edit item type and find/set there. Pull the value out of the original data source (e.Item.DataItem) rather than the old textbox from its row/altrow template.

________________________________________
Andrew

I work for a gift card company!
 
Another Scott Mitchell trick. If you're loading your ddl dynamically:
Code:
Dim ddlDS as DataSet = New DataSet()

' get the data for the ddl
Function GetDDL() as DataSet
Dim strConn as String = "yourConnectionString"
Dim conn as New SqlConnection(strConn)
conn.Open()
Dim strSql as String = "SELECT ID,YourColumn FROM YourTable"
Dim myDA as SqlDataAdapter
myDA = New SqlDataAdapter(strSql,conn)
myDA.Fill(ddlDS,"yourTable")
conn.Close()

Return ddlDS
End Function


' get the selected index of the item
Function GetIndex(ID as String) as Integer
Dim i as Integer
Dim dt as DataTable = ddlDS.Tables("YourTable")
For i = 0 to dt.Rows.Count -1
  If ID = dt.Rows(i)("ID").ToString() then
    Return i
  End If
Next i
End Function

Then in your EditItemTemplate:
Code:
<asp:DropDownList id="whatever" runat="server"
DataTextField = "YourColumn"
DataValueField = "ID"
DataSource = "<%#GetDDL()%>"
SelectedIndex = '<%#GetIndex(DataBinder.Eval(Container.DataItem("ID")).ToString())%>' />
Please excuse any typos or syntax. I don't do too much vb anymore.
 
Ok. I have tried the first way however, I am getting an error on this line
Code:
strSubject = CType(e.Item.FindControl("lblsubject"), Label).Text
It says that "Item" is not part of System.EventArgs.
 
It looks like you're already in the [tt]DataGrid.ItemDataBound[/tt] event handler (and if not, you should be), so just say
Code:
If e.Item.ItemType = ListItemType.EditItem Then
    e.Item.FindControl("lstType").Items.FindByText(e.Item.DataItem("Name")).Selected = True
End If
where "Name" is whatever the field name is in your data source that you're binding that field to. Depending on how strict you're running, you may need to cast/convert [tt]e.Item.FindControl("lstType")[/tt] as a [tt]DropDownList[/tt] and [tt]e.Item.DataItem[/tt] as a [tt]DataRowView[/tt] or whatever your data source item is.

________________________________________
Andrew

I work for a gift card company!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top