Datagrid - Dropdown list question

lorena

Centurion
Joined
Oct 23, 2003
Messages
134
Location
Phoenix, Arizona
I hava a datagrid that I am setting up for editing. Two fields have a limited selection of values so the cells with those fields are set up as dropdowns when "Edit" is clicked.
I am having a problem setting the initial value for the dropdown list. The values load into the list okay but I can't figure out how to make the initial value in the dropdown match the value of the database field.
Here is the code for the datagrid row:
<asp:TemplateColumn HeaderText="Type">
<ItemTemplate><asp:Label Runat="server" id="lblType"
Width="60px" text='<%# DataBinder.Eval(Container.DataItem, "Type") %>'/></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList Runat="server" ID="ddlType" Font-Name="Verdana" Font-Size="xx-small" DataTextField="Type" DataValueField="Type" DataSource="<%# GetTypes() %>">
</asp:DropDownList></EditItemTemplate></asp:TemplateColumn>
This is the code for the function that fills the dropdown:
Function GetTypes() As DataSet
Const strSQLT As String = "Select DISTINCT Type from toolroom"
Dim objConn As New OleDb.OleDbConnection(strConnString)
Dim myDA As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(strSQLT, objConn)
myDA.Fill(ddlType, "toolroom")
objConn.Close()
Return ddlType
End Function
Any suggestions would be appreciated
 
I go at this a different way than most, but whatever..

Anyway...

Visual Basic:
Private Sub myDataGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDataGrid.EditCommand

        myDataGrid.EditItemIndex = e.Item.ItemIndex

        Session.Contents("SelIndex") = e.items.cells(:CellRow:(Starts at 0)).text
        ' This will store the previous text into a session variable....Other ways to do it, but i prefer this way as requires less work...
Than in your
Visual Basic:
    Private Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles myDataGrid.ItemDataBound

If e.Item.ItemType = ListItemType.EditItem Then

            Ctype(e.Item.FindControl("ddlTypes"),DropDownList).SelectedValue = Session.Contents("SelIndex")

        End If
        
    End Sub

I think this will work... ... Worth a shot anyway :P
 
Back
Top