GridView DropDownList列. [英] GridView DropDownList column.
问题描述
大家好,我有一个以实体模型为后端的Web应用程序.我在gridview中将设置设置为Edit | Update | Delete设置为true.我有一个单独的表,用于存储要填充记录中特定列的选项列表.在编辑模式下,有没有办法制作DropDownList?我将属性AutoGenerateColumns设置为true.
我明白了-部分.因此,请勿使用AutoGenerateColumns,并在DataView的列集合中的每个列中添加适当的字段.为下拉列表创建一个新的entityDatasource,并将其链接到TemplateField.现在的问题是要了解此模型的编辑部分.当我尝试在下拉列表中选择一个成员时,它不会更新,并且当我尝试更新任何其他字段时,我得到一个错误.如何使用此设置修复更新语句?
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?
推荐答案
在这里,请看一下我们如何从下拉菜单中获得有关如何接线的想法正常工作: MSDN:演练:编辑时显示下拉列表在GridView Web服务器控件中 [ ^ ]
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[^]
这是工作模型:
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>
这篇关于GridView DropDownList列.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!