问题描述
先生,我有一个gridview,其属性为autogenerate列true。我想用下拉列表绑定每一列。
我的代码如下: -
sir, i have a gridview with the property of autogenerate column true. i want to bind each column with dropdownlist.
My code is like :-
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound
Dim dl As New DropDownList()
cmd1.CommandText = "Select * from ClassRoom order by ClassRoom"
Dim dr2 = cmd1.ExecuteReader
dl.DataSource = dr2
dl.AppendDataBoundItems = True
dl.DataTextField = "ClassRoom"
dl.DataValueField = "ClassRoom"
dl.DataBind()
dl.SelectedItem.Text = ""
dr2.Close()
For Each row As GridViewRow In Gridview1.Rows
row.Cells(2).Controls.Add(dl)
row.Cells(3).Controls.Add(dl)
Next
End Sub
此代码仅添加下拉列表单元格(3)不在单元格中(2)如何在两个单元格中添加下拉列表。
this code add dropdownlist only cell(3) not in cell(2) how can i add dropdownlist in both cell.
推荐答案
For Each row As GridViewRow In Gridview1.Rows
For i = 1 to 2
Dim dl As New DropDownList()
cmd1.CommandText = "Select * from ClassRoom order by ClassRoom"
Dim dr2 = cmd1.ExecuteReader
dl.DataSource = dr2
dl.AppendDataBoundItems = True
dl.DataTextField = "ClassRoom"
dl.DataValueField = "ClassRoom"
dl.DataBind()
dl.SelectedItem.Text = ""
dr2.Close()
row.Cells(i).Controls.Add(dl)
Next i
Next
看到类似的问题: []
我真的建议创建 DropDownList
并在设计时设置其属性: []
See similar question: Re: GridView DropDownList programmatically add items on RowDataBound[^]
I really do recommend to create DropDownList
and set its properties on design time: Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web Server Control[^]
这篇关于当autogenerate列属性id为true时,gridview with dropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!