Changeing RadGrid Datasource At Runtime Edit Problems

2 posts, 0 answers
  1. Avery
    Avery avatar
    2 posts
    Member since:
    Apr 2010

    Posted 04 Nov 2010 Link to this post

    I have a form with a grid and a combo box where a user can select the table name they wish to edit.  I have the grid binding to the table name selected by calling Rebind() when the user makes a table name selection, and by handling the NeedDataSource event of the grid to bind to a DataTable.  That seems to work fine.

    The problem comes in when I try to edit.  I added a GridEditCommandColumn to the MasterTableView's Columns collection so I get a link to start editing a row, and the editor form appears just fine.  But when I click on 'update', and I try to handle the UpdateCommand event raised by the grid (from code cobbled together from forums), I get an argument out of range exception, that I believe is coming from the edited item's OwnerTableView.DataKeyValues collection which has zero elements. 

    One other thing to note, I am using Postgres as the database, but I don't think this is the issue, as I am using npgsql to get standard ado.net objects to bind to.

    Any ideas?

     

     

     

     

     

        Private Sub RadGrid1_UpdateCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.UpdateCommand
            Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
            Dim table As DataTable = Me.GridSource
            'Locate the changed row in the datasource
            Dim pk As String = GetPrimaryTablePKColumn(cboTable.Text)
            If pk Is Nothing OrElse pk.Trim = String.Empty Then
                e.Canceled = True
                Return
            End If
      
    'Error Happens Here
            Dim changedRows As DataRow() = table.Select(pk & " = " &
                                                        editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)(pk).ToString)
      
            If changedRows.Length <> 1 Then
                e.Canceled = True
                Return
            End If
            'Update new values
            Dim newValues As New Hashtable
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)
            changedRows(0).BeginEdit()
            Try
                For Each entry As DictionaryEntry In newValues
                    changedRows(0)(DirectCast(entry.Key, String)) = entry.Value
                Next
                changedRows(0).EndEdit()
            Catch ex As Exception
                changedRows(0).CancelEdit()
                e.Canceled = True
            End Try

     

     

     

     

     

     

     

     

     

  2. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 09 Nov 2010 Link to this post

    Hi Avery,

    Thank you for contacting us and for your question.

    When rebinding the grid to the new table you should also add the name of the primary key column to the DataKeyNames collection of the grid. The in the UpdateCommand you can easily get the primary key value for the given row as follows:

    editedItem.GetDataKeyValue(pk)

    Hope this information will prove helpful.
     
    Regards,
    Tsvetoslav
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top