<telerik:RadGrid ID="RadGrid1" runat="server"OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender"AllowSorting="True"> <MasterTableView HierarchyLoadMode="ServerBind" AllowSorting="true" DataKeyNames="Unique_Id, Parent_Id"> <SelfHierarchySettings ParentKeyName="Parent_Id" KeyName="Unique_Id" /> </MasterTableView> <ClientSettings AllowExpandCollapse="true"> <Selecting AllowRowSelect="True"></Selecting> </ClientSettings></telerik:RadGrid>
Then, in the code-behind, using OnNeedDataSource event:protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e){ RadGrid1.DataSource = GetDataTable(); //fetches data, returns DataTable}protected void RadGrid1_PreRender(object sender, EventArgs e){ HideOrShowExpandColumnRecursive(RadGrid1.MasterTableView);}protected void HideOrShowExpandColumnRecursive(GridTableView tableView){ GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView); foreach (GridNestedViewItem nestedViewItem in nestedViewItems) { foreach (GridTableView nestedView in nestedViewItem.NestedTableViews) { if (nestedView.Items.Count == 0) { TableCell cell = nestedView.ParentItem["ExpandColumn"]; cell.Controls[0].Visible = false; nestedViewItem.Visible = false; } if (nestedView.HasDetailTables) { HideOrShowExpandColumnRecursive(nestedView); } } }}<telerik:RadMaskedTextBox ID="txtPhone" runat="server" Mask="(###)###-####" Text='<%# DataBinder.Eval( Container, "DataItem.Phone" ) %>' TabIndex="6" EmptyMessage="-- Enter Phone Number --" HideOnBlur="true" ZeroPadNumericRanges="true" DisplayMask="(###)###-####"> </telerik:RadMaskedTextBox> 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
