Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NET
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 7 Jul '12 - 4:47
Edited 7 Jul '12 - 7:02


3 solutions

Here's the working model:
 
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
 
<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>
  Permalink  
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[^]
  Permalink  
Comments
Idle_Force - 7 Jul '12 - 15:50
So I need to make my own Insert/Update/Delete statements? Do I need an ObjectDatasource?
take a look at these articles, they would explain how to use dropdownlist in the gridview.
Using RadioButtonList and DropDownList in GridView Row
Use DropDownList in ASP.NET GridView Control
  Permalink  
Comments
Idle_Force - 8 Jul '12 - 0:48
It's close, but not an entity example. I'll keep searching.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,286
1 OriginalGriff 6,561
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 11 Jul 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid