📄 defaultvb.aspx.vb
字号:
Imports System.Collections
Imports Telerik.WebControls
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Web
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports Telerik.QuickStart
Imports Telerik.QuickStart.Grid
Namespace Telerik.GridExamplesVBNET.Programming.CommandItem
Public Class DefaultVB
Inherits XhtmlPage
Protected Button1 As System.Web.UI.WebControls.Button
Protected WithEvents RadGrid1 As RadGrid
Protected WithEvents RadGrid2 As RadGrid
Private Sub RadGrid1_NeedDataSource(ByVal [source] As Object, ByVal e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
Me.RadGrid1.DataSource = Me.CustomersTable
End Sub
Private Sub RadGrid2_NeedDataSource(ByVal source As Object, ByVal e As WebControls.GridNeedDataSourceEventArgs) Handles RadGrid2.NeedDataSource
Me.RadGrid2.DataSource = Me.CustomersTable
End Sub
Public ReadOnly Property CustomersTable() As DataTable
Get
Dim res As DataTable = CType(Me.Session("CustomersTable"), DataTable)
If res Is Nothing Then
res = DataSourceHelperVB.GetDataTable("SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City] FROM [Customers]")
Me.Session("CustomersTable") = res
End If
Return res
End Get
End Property
Private Sub RadGrid1_ItemCommand(ByVal sender As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.ItemCommand
If e.CommandName = "EditSelected" Then
If RadGrid1.SelectedIndexes.Count = 0 Then
Return
End If
Dim item As GridDataItem
For Each item In RadGrid1.SelectedItems
item.Edit = True
Next
e.Item.OwnerTableView.Rebind()
Return
End If
If e.CommandName = "UpdateEdited" Then
If RadGrid1.EditIndexes.Count = 0 Then
Return
End If
Dim item As GridDataItem
For Each item In RadGrid1.EditItems
UpdateItem(item)
Next
e.Item.OwnerTableView.Rebind()
Return
End If
If e.CommandName = "DeleteSelected" Then
If RadGrid1.SelectedIndexes.Count = 0 Then
Return
End If
Dim item As GridDataItem
For Each item In RadGrid1.SelectedItems
DeleteItem(item)
Next
e.Item.OwnerTableView.Rebind()
Return
End If
If e.CommandName = "CancelAll" Then
Dim item As GridDataItem
For Each item In RadGrid1.EditItems
item.Edit = False
Next
e.Item.OwnerTableView.IsItemInserted = False
e.Item.OwnerTableView.Rebind()
Return
End If
End Sub
Private Sub UpdateItem(ByVal editedItem As GridEditableItem)
Dim customersTable As DataTable = Me.CustomersTable
'Locate the changed row in the DataSource
Dim changedRows As DataRow() = customersTable.Select("CustomerID = '" + editedItem.GetDataKeyValue("CustomerID").ToString() + "'")
If changedRows.Length <> 1 Then
Return
End If
'Update new values
Dim newValues As Hashtable = New Hashtable
'The GridTableView will fill the values from all editable columns in the hash
editedItem.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)
Dim changedRow As DataRow = changedRows(0)
changedRow.BeginEdit()
Try
Dim entry As DictionaryEntry
For Each entry In newValues
changedRow(CStr(entry.Key)) = entry.Value
Next
changedRow.EndEdit()
Catch ex As Exception
changedRow.CancelEdit()
End Try
editedItem.Edit = False
'Code for updating the database can go here...
End Sub
Private Sub DeleteItem(ByVal editedItem As GridEditableItem)
Dim customersTable As DataTable = Me.CustomersTable
'Locate the changed row in the DataSource
Dim changedRows As DataRow() = customersTable.Select("CustomerID = '" + editedItem.GetDataKeyValue("CustomerID").ToString() + "'")
If changedRows.Length <> 1 Then
Return
End If
'Code for updating the database can go here...
customersTable.Rows.Remove(changedRows(0))
End Sub
Private Sub RadGrid1_InsertCommand(ByVal source As Object, ByVal e As WebControls.GridCommandEventArgs) Handles RadGrid1.InsertCommand
Dim editedItem As GridEditableItem = e.Item.OwnerTableView.GetInsertItem()
Dim customersTable As DataTable = Me.CustomersTable
Dim newRow As DataRow = customersTable.NewRow()
'Set new values
Dim newValues As Hashtable = New Hashtable
'The GridTableView will fill the values from all editable columns in the hash
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)
Try
Dim entry As DictionaryEntry
For Each entry In newValues
newRow(CStr(entry.Key)) = entry.Value
Next
Catch ex As Exception
e.Canceled = True
End Try
customersTable.Rows.Add(newRow)
'Code for updating the database ca go here...
End Sub
Private Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
Dim item As GridItem
For Each item In RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)
CType(item.FindControl("LinkButton1"), LinkButton).Attributes("onclick") = "javascript:return confirm('Delete all selected customers?');"
Next
End Sub
End Class
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -