This code is loosely based on several of the demos. My main problem here is that when i run my program and happen to see my gridview the datas are not the same as with my datas in my table in my database.
And when i tried to hit the edit button.. and make some few changes.. it updates my gridview but not the same as my datas in my table in my database..
hope someone could lend a helping hand..
here's my code.. by the way i'm just a newbie in asp.net ..please be gentle.. thanks
ASPX
VB
And when i tried to hit the edit button.. and make some few changes.. it updates my gridview but not the same as my datas in my table in my database..
hope someone could lend a helping hand..
here's my code.. by the way i'm just a newbie in asp.net ..please be gentle.. thanks
ASPX
<%@ Page Language="VB" MasterPageFile="~/Admin/Admin.master" AutoEventWireup="false" CodeFile="TeacherRole.aspx.vb" Inherits="Admin_TeacherRole" title="Untitled Page" %><%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %><asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server"> <html><body> <!-- content start --> <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server"> <script type="text/javascript"> function RowDblClick(sender, eventArgs) { sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical()); } </script> </telerik:RadCodeBlock><telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1"> <AjaxSettings> <telerik:AjaxSetting AjaxControlID="RadGrid1"> <UpdatedControls> <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" /> </UpdatedControls> </telerik:AjaxSetting> </AjaxSettings> </telerik:RadAjaxManager> <telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" OnUpdateCommand = "RadGrid1_UpdateCommand" AutoGenerateEditColumn="True" GridLines="None" Skin="Black" AllowAutomaticUpdates="True" ShowGroupPanel="True" GroupPanel-ID = "RadAjaxLoadingPanel1"> <MasterTableView EditMode="PopUp" AutoGenerateColumns="False" DataKeyNames = "user_id" CommandItemDisplay="Top"> <Columns> <telerik:GridBoundColumn DataField="user_id" DataType="System.Int32" HeaderText="User ID" SortExpression="user_id" UniqueName="user_id" ReadOnly="True"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="lastname" HeaderText="Last Name" SortExpression="lastname" UniqueName="lastname" ReadOnly="False"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="firstname" HeaderText="First Name" SortExpression="firstname" UniqueName="firstname" ReadOnly="False"> </telerik:GridBoundColumn> </Columns> <EditFormSettings CaptionFormatString="Edit User ID: {0}" CaptionDataField="user_id" PopUpSettings-Modal="True" PopUpSettings-ScrollBars="Auto" EditFormType= "AutoGenerated"> <FormTableItemStyle Width="100%" Height="29px"></FormTableItemStyle> <FormTableStyle GridLines="None" CellSpacing="0" CellPadding="2"></FormTableStyle> <FormStyle Width="100%" BackColor="#eef2ea"></FormStyle> <EditColumn ButtonType="PushButton" /> <PopUpSettings ScrollBars="Auto" Modal="True"></PopUpSettings> </EditFormSettings> </MasterTableView> <ClientSettings AllowDragToGroup="True"> <Scrolling AllowScroll="True" UseStaticHeaders="True" /> <ClientEvents OnRowDblClick="RowDblClick" /> </ClientSettings> </telerik:RadGrid> <!-- content end --></body></html></asp:Content>VB
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.Globalization
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Telerik.Web.UI
Partial Class Admin_TeacherRole
Inherits System.Web.UI.Page
Private Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
If Not IsPostBack Then
For Each item As GridItem In RadGrid1.MasterTableView.Items
If TypeOf item Is GridEditableItem Then
Dim editableItem As GridEditableItem = CType(item, GridDataItem)
editableItem.Edit = True
End If
Next
RadGrid1.Rebind()
End If
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 UserData() As DataSet
Get
Dim obj As Object = Me.Session("UserData")
If Not obj Is Nothing Then
Return CType(obj, DataSet)
End If
Dim MyUserData As DataSet = New DataSet
Dim ConnString As String = ConfigurationManager.ConnectionStrings("ProLearnConnectionString").ConnectionString
Dim conn As SqlConnection = New SqlConnection(ConnString)
Dim adapter As SqlDataAdapter = New SqlDataAdapter
adapter.SelectCommand = New SqlCommand("SELECT user_id, lastname, firstname FROM tblStudents ORDER BY user_id", conn)
adapter.Fill(MyUserData, "tblStudents")
Me.Session("UserData") = MyUserData
Return MyUserData
End Get
End Property
Private Sub RadGrid1_NeedDataSource(ByVal source As System.Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
RadGrid1.DataSource = Me.UserData
Me.UserData.Tables("tblStudents").PrimaryKey = New DataColumn() {Me.UserData.Tables("tblStudents").Columns("user_id")}
End Sub
Protected Sub RadGrid1_UpdateCommand(ByVal source As System.Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.UpdateCommand
Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
Dim editMan As GridEditManager = editedItem.EditManager
Dim column As GridColumn
For Each column In e.Item.OwnerTableView.Columns
If TypeOf column Is IGridEditableColumn Then
Dim editableCol As IGridEditableColumn = CType(column, IGridEditableColumn)
If (editableCol.IsEditable) Then
Dim editor As IGridColumnEditor = editMan.GetColumnEditor(editableCol)
Dim editorType As String = CType(editor, Object).ToString()
Dim editorText As String = "unknown"
Dim editorValue As Object = Nothing
If (TypeOf editor Is GridTextColumnEditor) Then
editorText = CType(editor, GridTextColumnEditor).Text
editorValue = CType(editor, GridTextColumnEditor).Text
End If
If (TypeOf editor Is GridBoolColumnEditor) Then
editorText = CType(editor, GridBoolColumnEditor).Value.ToString()
editorValue = CType(editor, GridBoolColumnEditor).Value
End If
If (TypeOf editor Is GridDropDownColumnEditor) Then
editorText = CType(editor, GridDropDownColumnEditor).SelectedText & "; " & CType(editor, GridDropDownColumnEditor).SelectedValue
editorValue = CType(editor, GridDropDownColumnEditor).SelectedValue
End If
Try
Dim changedRows As DataRow() = Me.UserData.Tables("tblStudents").Select("user_id = " & editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("user_id"))
changedRows(0)(column.UniqueName) = editorValue
Me.UserData.Tables("tblStudents").AcceptChanges()
Catch ex As Exception
RadGrid1.Controls.Add(New LiteralControl("<strong>Unable to set value of column '" & column.UniqueName & "'</strong> - " + ex.Message))
e.Canceled = True
End Try
End If
End If
Next
End Sub
End Class
