如何绑定DropDownList的GridView的中从gridview的数据不 [英] How to bind DropDownList in Gridview with data NOT from gridview
问题描述
得到的答案是知道如何问这个问题的一半。我不能肯定我做的一个很好的工作,但是这是我最好的拍摄。
Half the battle of getting an answer is knowing how to ask the question. I am not certain I am doing a good job of that but this is my best shot.
我想绑定的GridView中的数据,是不是在GridView本身来一个DDL。这是EditItemTemplate模板内。这样做的目的是为了给用户,以开始,所选的值,并从查找存储过程的一系列其他的值。
I'm trying to bind a ddl with data inside a gridview that is NOT coming from the gridview itself. This is within the EditItemTemplate. The purpose for doing so is to give the user, to start, a selected value and a series of other values from a lookup stored procedure.
在这里我要提到这一点,成功之前,但使用一个ObjectDataSource,我已经做了。我试图避免这一次从后面现在然后将其移动到数据层后,code做完全。
I'll mention here that I have done this successfully before but using an ObjectDataSource. I am trying to avoid that this time and do it entirely from the code behind for now then move it to a data layer later.
下面是我迄今为止...
Here is what I have so far...
<asp:GridView ID="usersGrid" runat="server"
DataKeyNames="userID"
AutoGenerateColumns="false" Width="580"
OnRowUpdating="usersGrid_RowUpdating"
OnRowEditing="usersGrid_RowEditing"
OnRowCancelingEdit="usersGrid_RowCancelingEdit" OnRowDeleting="usersGrid_RowDeleting"
>
...
<EditItemTemplate>
<div class="gridName">
<asp:TextBox ID="txtFirstName" Text='<%#Eval("firstName") %>' runat="server" Width="95" />
</div>
<div class="gridName">
<asp:TextBox ID="txtLastName" Text='<%#Eval("lastName") %>' runat="server" Width="95" />
</div>
<div class="gridEmail">
<asp:TextBox ID="txtEmail" Text='<%#Eval("email") %>' runat="server" Width="245" />
</div>
<div class="gridName">
<asp:DropDownList ID="ddl_GetLists"
DataSourceID="GetListData()"
AppendDataBoundItems="true"
DataValueField="listID"
DataTextField="listName"
SelectedValue='<%#Bind("listID") %>'
runat="server"
>
</asp:DropDownList>
</div>
</EditItemTemplate>
...
Protected Sub usersGrid_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
usersGrid.EditIndex = e.NewEditIndex
BindData()
End Sub
...
Private Sub BindData()
Dim conn As New SqlConnection(connectionString)
Dim ad As New SqlDataAdapter("MAINT_DIST_GET_USERS", conn)
Dim ds As New DataSet()
ad.Fill(ds)
GetListData()
usersGrid.DataSource = ds
usersGrid.DataBind()
End Sub
我包括最后两个,以及我已经尝试和失败其他方法。
I'm including last two as well as other approaches I've tried and failed.
...
Protected Sub usersGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowState = DataControlRowState.Edit Then
Dim ddl As DropDownList = DirectCast(e.Row.FindControl("ddl_GetLists"), DropDownList)
Dim conn As New SqlConnection(connectionString)
Dim ad As New SqlDataAdapter("MAINT_DIST_GET_LISTS", conn)
Dim ds As New DataSet()
ad.Fill(ds)
ddl.DataSource = ds
ddl.DataBind()
End If
End Sub
Public Function BindDropdown() As DataSet
Dim conn As New SqlConnection(connectionString)
Dim ad As New SqlDataAdapter("MAINT_DIST_GET_LISTS", conn)
Dim ds As New DataSet()
ad.Fill(ds)
ddl_GetLists.DataSource = ds
ddl_GetLists.DataBind()
End Function
我也会问为什么,在最后的功能,为什么控制,ddl_GetLists,不承认呢?电网里面从设计师,但外面重新出现电网的消失。
I'll also ask why, in the final function, why is the control, ddl_GetLists, not recognized as well? Inside the grid it disappears from the designer but outside of the grid it reappears.
感谢大家的帮助。
推荐答案
您有几个选项。您可以使用一个DataSource控件,或者您也可以绑定在$ C $在GridView的的RowDataBound
事件C-后面的下拉菜单。
You have a couple of options. You can use a datasource control, or you can bind the dropdowns in code-behind in the RowDataBound
event of the GridView.
我注意到几个问题在code了。在你的榜样,你指派的DataSourceID
不正确。在的DataSourceID
应指向页面上的数据源控件的ID:
I noticed a couple of issues in your code too. In your example you're assigning the DataSourceID
incorrectly. The DataSourceID
should point to the ID of a datasource control on the page:
<asp:DropDownList ID="ddl_GetLists"
DataSourceID="SqlDataSource1"
AppendDataBoundItems="true"
DataValueField="listID"
DataTextField="listName"
SelectedValue='<%#Bind("listID") %>'
runat="server">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT listID, listName FROM SomeTable">
</asp:SqlDataSource>
如果你想要做的code-背后的绑定,就可以通过的RowDataBound
事件做到这一点:
If you want to do the binding in code-behind, you can do this through the RowDataBound
event:
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow AndAlso (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit Then
Dim ddl As DropDownList = TryCast(e.Row.FindControl("ddl_GetLists"), DropDownList)
If ddl IsNot Nothing Then
ddl.DataSource = RetrieveDataSource()
ddl.DataBind()
End If
End If
End Sub
这篇关于如何绑定DropDownList的GridView的中从gridview的数据不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!