This is a migrated thread and some comments may be shown as answers.

Problem Update Data With RadGrid

3 Answers 131 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Peper
Top achievements
Rank 1
Peper asked on 05 Nov 2008, 11:37 AM
Hi,

I'm try the telerik component 2008 Q2. i'm using vs2005. i try from the example of telerik, which is RadGrid -> Insert/Update/Delete -> Extracting Values -> Using Column Editor. i never can to updating data, even when i try to update data from the telerik example. the data only update on the web, but when i close the browser and load the example again, the data back to normal (no changes). this happen also in my project. i already follow all the instruction from the example, but still can't update the data. is there something missing?
here my aspx code:

<

telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">

 

 

<AjaxSettings>

 

 

<telerik:AjaxSetting AjaxControlID="RadGrid1">

 

 

<UpdatedControls>

 

 

<telerik:AjaxUpdatedControl ControlID="RadGrid1" />

 

 

</UpdatedControls>

 

 

</telerik:AjaxSetting>

 

 

</AjaxSettings>

 

 

</telerik:RadAjaxManager>

 

 

<telerik:RadGrid ID="RadGrid1" Skin="Default" runat="server" Width="45%" GridLines="None"

 

 

AutoGenerateColumns="False" PageSize="10" AllowSorting="True" AllowPaging="True"

 

 

ShowStatusBar="true">

 

 

<PagerStyle Mode="NumericPages"></PagerStyle>

 

 

<MasterTableView DataKeyNames="id_divisi" AllowMultiColumnSorting="True" Width="100%"

 

 

CommandItemDisplay="Top">

 

 

<Columns>

 

 

<telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" CommandName="Delete" />

 

 

<telerik:GridEditCommandColumn UpdateText="Update" UniqueName="EditCommandColumn"

 

 

CancelText="Cancel" EditText="Edit">

 

 

<HeaderStyle Width="45px"></HeaderStyle>

 

 

</telerik:GridEditCommandColumn>

 

 

<telerik:GridBoundColumn UniqueName="nama_divisi" SortExpression="nama_divisi" HeaderText="Nama Divisi"

 

 

DataField="nama_divisi" />

 

 

<telerik:GridBoundColumn UniqueName="keterangan" SortExpression="keterangan" HeaderText="Keterangan"

 

 

DataField="keterangan" />

 

 

</Columns>

 

 

<EditFormSettings CaptionFormatString="Edit Divisi ID {0}" CaptionDataField="id_divisi">

 

 

<FormTableItemStyle Width="100%" Height="29px"></FormTableItemStyle>

 

 

<FormTableStyle GridLines="None" CellSpacing="0" CellPadding="2"></FormTableStyle>

 

 

<FormStyle Width="100%" BackColor="#eef2ea"></FormStyle>

 

 

<EditColumn ButtonType="ImageButton" />

 

 

</EditFormSettings>

 

 

</MasterTableView>

 

 

</telerik:RadGrid>

 


and this is my code behind:


Partial

Class Master_divisi

 

 

Inherits System.Web.UI.Page

 

 

Dim cs As String = ConfigurationManager.ConnectionStrings("CsTs").ConnectionString

 

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 

 

If Not IsPostBack Then

 

RadGrid1.MasterTableView.EditMode = GridEditMode.EditForms

 

End If

 

 

End Sub

 

 

Public ReadOnly Property DivisiData() As DataSet

 

 

Get

 

 

Dim obj As Object = Me.Session("DivisiData")

 

 

If Not obj Is Nothing Then

 

 

Return CType(obj, DataSet)

 

 

End If

 

 

Dim MyDivisiData As DataSet = New DataSet

 

 

Dim ConnString As String = cs

 

 

Dim conn As SqlConnection = New SqlConnection(ConnString)

 

 

Dim adapter As SqlDataAdapter = New SqlDataAdapter

 

adapter.SelectCommand =

New SqlCommand("SELECT id_divisi, nama_divisi, keterangan FROM tbl_divisi", conn)

 

adapter.Fill(MyDivisiData,

"tbl_divisi")

 

 

Me.Session("DivisiData") = MyDivisiData

 

 

Return MyDivisiData

 

 

End Get

 

 

End Property

 

 

Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource

 

RadGrid1.DataSource =

Me.DivisiData

 

 

Me.DivisiData.Tables("tbl_divisi").PrimaryKey = New DataColumn() {Me.DivisiData.Tables("tbl_divisi").Columns("id_divisi")}

 

 

End Sub

 

 

Private 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.DivisiData.Tables("tbl_divisi").Select("id_divisi = " & editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("id_divisi"))

 

changedRows(0)(column.UniqueName) = editorValue

 

Me.DivisiData.Tables("tbl_divisi").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

 

RadGrid1.MasterTableView.Rebind()

 

End Sub

 

 

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

 

 

If Not MyBase.IsPostBack Then

 

 

Me.RadGrid1.MasterTableView.Rebind()

 

 

End If

 

 

End Sub

 

End

Class

 

Please help me.

thanks
peper

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 05 Nov 2008, 01:45 PM

Hi Peper,

In the example, the update command updates  the datatable, not the actual database. It is only for the session and the result will not be updated in the database. You can achieve permanent update of database by using the "Insert/Update/Delete at database level with queries" in place of making change to the current datatable. You can refer the link for more details.


http://www.telerik.com/help/aspnet-ajax/grdinsertupdatedeleteatdatabaselevel.html


Thanks,
Princy.

0
Daniel
Telerik team
answered on 05 Nov 2008, 03:02 PM
Hello Peper,

The mentioned example is not intended to update your database. It uses DataSet stored in the Session for the update/insert/delete operations. You need to modify the demo in order to behave as expected.

Let us know if you need assistance for the implementation of the depicted functionality.

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Peper
Top achievements
Rank 1
answered on 06 Nov 2008, 02:12 PM

Hi Princy, thanks for the instruction. it really work. and also thanks for the telerik admin. i wonder, what should i modify in order to commit all the change made in dataset.

 

thanks

peper

Tags
Grid
Asked by
Peper
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Daniel
Telerik team
Peper
Top achievements
Rank 1
Share this question
or