📄 defaultvb.aspx.vb
字号:
Imports System
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb
Imports System.Globalization
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Telerik.WebControls
Imports Telerik.QuickStart
Imports System.Collections
Namespace Telerik.GridExamplesVBNET.DataEditing.EditModes
Public Class DefaultVB
Inherits XhtmlPage
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = String.Empty
End Sub
Private Sub CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
Select Case CType(sender, CheckBox).ID
Case "RadioButton1"
RadGrid1.MasterTableView.EditMode = GridEditMode.EditForms
RadioButton1.Checked = True
RadioButton2.Checked = False
Case "RadioButton2"
RadGrid1.MasterTableView.EditMode = GridEditMode.InPlace
RadioButton1.Checked = False
RadioButton2.Checked = True
End Select
RadGrid1.Rebind()
End Sub
Private Sub RadGrid1_ItemCommand(ByVal source As System.Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.ItemCommand
If (e.CommandName = "Update") Then
Page.Validate()
If Not Page.IsValid Then
'UpdateCommand event will not fire
e.Canceled = True
Return
End If
End If
End Sub
Private Sub RadGrid1_NeedDataSource(ByVal source As System.Object, ByVal e As Telerik.WebControls.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
RadGrid1.DataSource = Me.OrdersData
End Sub
Private Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As WebControls.GridCommandEventArgs) Handles RadGrid1.UpdateCommand
Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
Dim ordersTable As DataTable = Me.OrdersData.Tables("Orders")
'Locate the changed row in the DataSource
Dim changedRows As DataRow() = ordersTable.Select("OrderID = " + editedItem("OrderID").Text)
If changedRows.Length <> 1 Then
Me.Label1.Text += "Unbale to locate the Order for updating."
e.Canceled = True
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
e.Item.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()
Label1.Text += "Unable to update Orders. Reason: " + ex.Message
e.Canceled = True
End Try
'Code for updating the database can go here...
Label1.Text += "Order " + CStr(changedRow("OrderID")) + " updated"
End Sub
Private Sub Page_PreRender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
If Not MyBase.IsPostBack Then
Me.RadGrid1.MasterTableView.Items(0).Edit = True
Me.RadGrid1.MasterTableView.Rebind()
End If
End Sub
Public ReadOnly Property OrdersData() As DataSet
Get
Dim obj As Object = Me.Session("OrdersData")
If Not obj Is Nothing Then
Return CType(obj, DataSet)
End If
Dim MyOrdersData As DataSet = New DataSet
Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/Grid/Data/Access/Nwind.mdb"))
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter
adapter.SelectCommand = New OleDbCommand("SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, PostalCode, Bool FROM Customers", conn)
adapter.Fill(MyOrdersData, "Customers")
adapter.SelectCommand = New OleDbCommand("SELECT Orders.*, (E.FirstName + ' ' + E.LastName) AS EmployeeName " & _
"FROM Orders, Employees E Where Orders.EmployeeID = E.EmployeeID", conn)
adapter.Fill(MyOrdersData, "Orders")
adapter.SelectCommand = New OleDbCommand("SELECT EmployeeID, (FirstName + ' ' + LastName) AS FullName FROM Employees", conn)
adapter.Fill(MyOrdersData, "Employees")
Me.Session("OrdersData") = MyOrdersData
Return MyOrdersData
End Get
End Property
' Fields
Protected Button1 As Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected daCustomers As OleDbDataAdapter
Protected daOrderDetails As OleDbDataAdapter
Protected daOrders As OleDbDataAdapter
Protected DataGrid1 As DataGrid
Protected dsNWind1 As dsNWind
Protected Label1 As Label
Protected Linkbutton1 As LinkButton
Protected Linkbutton2 As LinkButton
Protected Linkbutton4 As LinkButton
Protected WithEvents RadioButton1 As System.Web.UI.WebControls.RadioButton
Protected WithEvents RadioButton2 As System.Web.UI.WebControls.RadioButton
Protected WithEvents CheckBox1 As System.Web.UI.WebControls.CheckBox
Protected WithEvents RadGrid1 As RadGrid
Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
Me.RadGrid1.AllowMultiRowEdit = Me.CheckBox1.Checked
Me.RadGrid1.Rebind()
End Sub
End Class
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -