Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, I have a web application with entity model as backend. I have the settings set to Edit|Update|Delete set to true in the gridview. I have a separate table for storing a list of choices that I want to fill a certain column in the records. Is there a way to make a DropDownList while in edit mode? I have the property AutoGenerateColumns to true.

I got it - partly. So do not use AutoGenerateColumns and add each column in the column's collection in the DataView with the appropriate fields. Create a new entityDatasource for the dropdownlist and link it to the TemplateField. The problem now is to understand the edit part of this model. When I try to select a member in the dropdownlist it does not update and when I try to update any other fields I get an error. How do I fix the update statements with this setup?
Posted
Updated 7-Jul-12 7:02am
v3

Here, have a look at it how we have dropdown in genral to get the idea on how things are wired up and work: MSDN: Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web Server Control[^]
 
Share this answer
 
Comments
Idle_Force 7-Jul-12 15:50pm    
So I need to make my own Insert/Update/Delete statements? Do I need an ObjectDatasource?
Here's the working model:

VB
Sub dgv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles dgv.RowDataBound
   If e.Row.RowState = DataControlRowState.Edit Then
     Dim md As Med = CType(e.Row.DataItem, Med)
     Dim _type As String = md.DType
     Dim ddl As DropDownList = CType(e.Row.FindControl("ddlDType"), DropDownList)
     Dim item As ListItem = ddl.Items.FindByText(_type)
     ddl.SelectedIndex = ddl.Items.IndexOf(item)
   End If
 End Sub

 Sub dgv_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles dgv.RowUpdating
   Dim row As GridViewRow = dgv.Rows(dgv.EditIndex)
   Dim list As DropDownList = CType(row.FindControl("ddlDType"), DropDownList)
   e.NewValues("DType") = list.SelectedValue
 End Sub

 Private Sub dgv_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles dgv.RowEditing
   If Not dgv.EditIndex = -1 Then
     Try
       Dim row As GridViewRow = dgv.Rows(dgv.EditIndex)
       Dim ddl As DropDownList = CType(row.FindControl("ddlDType"), DropDownList)
       ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(CType(row.DataItem, Med).DType))
     Catch
     End Try
   End If
 End Sub


XML
<asp:TemplateField  HeaderText="DType" >
    <ItemTemplate >
       <asp:Label ID="lbDT" Text='<%# Bind("DType")%>' runat="server" ></asp:Label>
    </ItemTemplate>
    <EditItemTemplate >
       <asp:DropDownList ID="ddlDType" DataSourceID="edsDDL" DataTextField="types" DataValueField="types" runat="server" ></asp:DropDownList>
    </EditItemTemplate>
</asp:TemplateField>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900