您可以使用几个选项。一种是使用模板,另一种是在创建行时手动添加控件。模板示例(这使用了一个复选框,但可以轻松切换):
Public Class CheckBoxTemplate
Implements ITemplate
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Dim cb As CheckBox = New CheckBox()
cb.ID = "someId"
cb.AutoPostBack = True
container.Controls.Add(cb)
End Sub
End Class
在您创建 gridview 控件的应用代码中:
Dim gv As New GridView
With gv
.ID = "myGridView"
.AutoGenerateColumns = False
.DataKeyNames = New String() {"somePKID"}
.GridLines = GridLines.Both
.AllowSorting = False
.AllowPaging = False
.PageSize = numRows
.Width = tableWidth
.BorderColor = Drawing.ColorTranslator.FromHtml("#808080")
.PagerSettings.Mode = PagerButtons.NextPrevious
.PagerSettings.NextPageText = "Next"
.PagerSettings.PreviousPageText = "Prev"
.HeaderStyle.CssClass = foundUserHeadStyle
.RowStyle.CssClass = foundUserEvenRows
.AlternatingRowStyle.CssClass = foundUserOddRows
.Columns.Clear()
Dim SelectUserTF As New TemplateField
With SelectUserTF
.HeaderText = "Add"
.ItemStyle.Wrap = False
.ItemTemplate = New CheckBoxTemplate()
End With
.Columns.Add(SelectUserTF)
End With
另一种选择是在创建行事件中执行此操作:
Protected Sub gv_rowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles myGridView.RowCreated
Try
Dim myDDL As New DropDownList
Dim myCollection As New ListItemCollection
With myCollection
Dim newItem As New ListItem
newItem.Text = "item 1"
newItem.Value = "1"
.Add(newItem)
End With
e.Row.Cells(0).Controls.Add(myDDL)
Catch ex As Exception
Finally
End Try
end sub
让我知道这是否有帮助或您对此有疑问。