程序代码: | [ 复制代码到剪贴板 ] |
Imports System.Web.UI.WebControls
Imports System.Web.UI
Namespace MyCustomColumn
Public Class DropDownColumn
Inherits DataGridColumn
Public DataSource As ICollection
Public DataField As String
Public DataTextField As String
Public DataValueField As String
Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal columnIndex As Integer, ByVal itemType As ListItemType)
MyBase.InitializeCell(cell, columnIndex, itemType)
Select Case itemType
Case ListItemType.Header
cell.Text = HeaderText
Case ListItemType.Item, ListItemType.AlternatingItem
AddHandler cell.DataBinding, AddressOf ItemDataBinding
Case ListItemType.EditItem
AddHandler cell.DataBinding, AddressOf EditItemDataBinding
Dim DDL As New DropDownList
cell.Controls.Add(DDL)
End Select
End Sub
Private Sub ItemDataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim cell As TableCell = CType(sender, TableCell)
Dim DGI As DataGridItem = CType(cell.NamingContainer, DataGridItem)
Try
cell.Text = DGI.DataItem(DataField)
Catch RangeEx As IndexOutOfRangeException
Throw New Exception("Specified DataField was not found.")
Catch OtherEx As Exception
Throw New Exception(OtherEx.InnerException.ToString)
End Try
End Sub
Private Sub EditItemDataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim cell As TableCell = CType(sender, TableCell)
Dim DDL As DropDownList = CType(cell.Controls(0), DropDownList)
Dim DataSourceItem As Object
Dim item As ListItem
Dim DGI As DataGridItem
'Add a first, blank option
DDL.Items.Add(New ListItem(""))
For Each DataSourceItem In DataSource
Select Case DataSourceItem.GetType.ToString
Case "System.String" 'Applies to my ArrayList example
item = New ListItem(DataSourceItem, DataSourceItem)
DDL.Items.Add(item)
Case "System.Data.DataRowView"
Dim DRV As DataRowView = CType(DataSourceItem, DataRowView)
item = New ListItem(DRV(DataTextField), DRV(DataValueField))
DDL.Items.Add(item)
Case Else
Throw New Exception("Invalid DataSource type.")
End Select
Next
Try
DGI = CType(cell.NamingContainer, DataGridItem)
item = DDL.Items.FindByValue(DGI.DataItem(DataField))
Catch RangeEx As IndexOutOfRangeException
Throw New Exception("Specified DataField was not found.")
Catch OtherEx As Exception
Throw New Exception(OtherEx.InnerException.ToString)
End Try
If Not item Is Nothing Then item.Selected = True
End Sub
End Class
End Namespace
Imports System.Web.UI
Namespace MyCustomColumn
Public Class DropDownColumn
Inherits DataGridColumn
Public DataSource As ICollection
Public DataField As String
Public DataTextField As String
Public DataValueField As String
Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal columnIndex As Integer, ByVal itemType As ListItemType)
MyBase.InitializeCell(cell, columnIndex, itemType)
Select Case itemType
Case ListItemType.Header
cell.Text = HeaderText
Case ListItemType.Item, ListItemType.AlternatingItem
AddHandler cell.DataBinding, AddressOf ItemDataBinding
Case ListItemType.EditItem
AddHandler cell.DataBinding, AddressOf EditItemDataBinding
Dim DDL As New DropDownList
cell.Controls.Add(DDL)
End Select
End Sub
Private Sub ItemDataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim cell As TableCell = CType(sender, TableCell)
Dim DGI As DataGridItem = CType(cell.NamingContainer, DataGridItem)
Try
cell.Text = DGI.DataItem(DataField)
Catch RangeEx As IndexOutOfRangeException
Throw New Exception("Specified DataField was not found.")
Catch OtherEx As Exception
Throw New Exception(OtherEx.InnerException.ToString)
End Try
End Sub
Private Sub EditItemDataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim cell As TableCell = CType(sender, TableCell)
Dim DDL As DropDownList = CType(cell.Controls(0), DropDownList)
Dim DataSourceItem As Object
Dim item As ListItem
Dim DGI As DataGridItem
'Add a first, blank option
DDL.Items.Add(New ListItem(""))
For Each DataSourceItem In DataSource
Select Case DataSourceItem.GetType.ToString
Case "System.String" 'Applies to my ArrayList example
item = New ListItem(DataSourceItem, DataSourceItem)
DDL.Items.Add(item)
Case "System.Data.DataRowView"
Dim DRV As DataRowView = CType(DataSourceItem, DataRowView)
item = New ListItem(DRV(DataTextField), DRV(DataValueField))
DDL.Items.Add(item)
Case Else
Throw New Exception("Invalid DataSource type.")
End Select
Next
Try
DGI = CType(cell.NamingContainer, DataGridItem)
item = DDL.Items.FindByValue(DGI.DataItem(DataField))
Catch RangeEx As IndexOutOfRangeException
Throw New Exception("Specified DataField was not found.")
Catch OtherEx As Exception
Throw New Exception(OtherEx.InnerException.ToString)
End Try
If Not item Is Nothing Then item.Selected = True
End Sub
End Class
End Namespace