如何在GridView中设置DropDownList的SelectedValue [英] How to set SelectedValue of DropDownList in GridView
本文介绍了如何在GridView中设置DropDownList的SelectedValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
报价:"ddl1"具有一个SelectedValue,该值无效,因为它不存在于项目列表中.参数名称:值
这是我的代码
Dim da As 新 SqlDataAdapter(" Dim drdList As DropDownList 对于 每个 grdRow As GridViewRow 在 GridView2.Rows中 drdList = DirectCast (GridView2.Rows(grdRow.RowIndex).Cells( 2 ).FindControl(" ),DropDownList) drdList.DataSource = dt drdList.DataValueField = " drdList.DataTextField = " drdList.DataBind() drdList.SelectedValue = GridView2.DataKeys(grdRow.RowIndex)(" ).ToString() 下一步
我已经在GridView2_RowDataBound
中编写了以上代码 请帮助我摆脱困境.
在此先感谢解决方案
Dim con 新建 SqlConnection(ConfigurationManager.ConnectionStrings( 数据源").ConnectionString) Dim da1 As 新建 SqlDataAdapter(" ,等等) Dim dtCities As 新建 DataTable() da1.Fill(dtCities) ' 检查它是否是DataRow. ' 将行项目转换为DatarowView Dim drv As DataRowView = TryCast (e .Row.DataItem,DataRowView) ' 获取当前行的价值城市 暗城市 As [字符串] = drv (" ).ToString() ' 在行内查找DropDownList Dim ddl As DropDownList = DirectCast (e .Row.FindControl(" ),DropDownList) ' 绑定我们创建的临时表 ddl.DataSource = dtCities ' 设置显示值 ddl.DataTextField = " ' 将值绑定到DropDownList ddl.DataBind() ' 找到当前城市并将其设置为选定" ddl.Items.FindByText(City).Selected = 真 '
Quote:''ddl1'' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
here is my code
Dim da As New SqlDataAdapter("select * from item_master", con) da.Fill(dt) Dim drdList As DropDownList For Each grdRow As GridViewRow In GridView2.Rows drdList = DirectCast(GridView2.Rows(grdRow.RowIndex).Cells(2).FindControl("ddl1"), DropDownList) drdList.DataSource = dt drdList.DataValueField = "item_id" drdList.DataTextField = "units" drdList.DataBind() drdList.SelectedValue = GridView2.DataKeys(grdRow.RowIndex)("units").ToString() Next
i have written the above code inGridView2_RowDataBound
Please help me to get out of this.
Thanks in advance解决方案Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("datasource").ConnectionString) Dim da1 As New SqlDataAdapter("select * from item_master", con) Dim dtCities As New DataTable() da1.Fill(dtCities) 'Check if it is a DataRow. 'Cast the row item as DatarowView Dim drv As DataRowView = TryCast(e.Row.DataItem, DataRowView) 'Get the Value City for the Current Row Dim City As [String] = drv("units").ToString() 'Find the DropDownList inside the Rows Dim ddl As DropDownList = DirectCast(e.Row.FindControl("ddl1"), DropDownList) 'Bind the Temporory Table we have Created ddl.DataSource = dtCities 'Set the Display value ddl.DataTextField = "units" 'Bind the values to DropDownList ddl.DataBind() 'Find the Current City and set that as Selected ddl.Items.FindByText(City).Selected = True '
这篇关于如何在GridView中设置DropDownList的SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文