Grid is not refreshed after ItemCommand event
Protected Sub Parts_ItemCommand(sender As Object, e As GridCommandEventArgs) Handles Parts.ItemCommand
If e.CommandName = "DeleteInventory" Then
Dim filename As String = _uploadSystemPath & "tree.xml"
Dim XMLDoc As XDocument = XDocument.Load(_uploadSystemPath & "tree.xml")
XMLDoc.Root.Elements("Node")(e.Item.ItemIndex).Remove()
XMLDoc.Save(filename)
XMLBookBuilderSource.DataFile = _uploadSystemPath & "tree.xml"
Parts.DataSource = XMLBookBuilderSource
Parts.DataBind()
End If
End Sub
3 Answers, 1 is accepted
From the provided code it seems that you are using simple data binding for RadGrid. That is, the grid is bound via the DataBound() method. Note that this type of binding us suitable only for the most simple scenarios.
In most cases it is recommended to use advanced data binding. You can either use declarative datasource or handle the NeedDataSurce event. Check out the following resources that elaborate in more detail on both approaches.
- http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-binding/understanding-data-binding/server-side-binding/declarative-datasource
- http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-binding/understanding-data-binding/server-side-binding/advanced-data-binding-%28using-needdatasource-event%29
With this approach if you need to rebind RadGrid explicitly you should call Rebind().
Regards,
Viktor Tachev
Telerik

Hi Viktor,
I used the below code still the Needdatasource is not calling after the Itemcommand.
Protected Sub Parts_ItemCommand(sender As Object, e As GridCommandEventArgs) Handles Parts.ItemCommand
If e.CommandName = "DeleteInventory" Then
Dim filename As String = _uploadSystemPath & "tree.xml"
Dim XMLDoc As XDocument = XDocument.Load(_uploadSystemPath & "tree.xml")
XMLDoc.Root.Elements("Node")(e.Item.ItemIndex).Remove()
XMLDoc.Save(filename)
'XMLBookBuilderSource.DataFile = _uploadSystemPath & "tree.xml"
'Parts.DataSource = XMLBookBuilderSource
'Parts.DataBind()
Parts.DataSource = Nothing
Parts.Rebind()
End If
End Sub
Protected Sub Parts_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs) Handles Parts.NeedDataSource
If File.Exists(_uploadSystemPath & "tree.xml") Then
XMLBookBuilderSource.DataFile = _uploadSystemPath & "tree.xml"
Parts.DataSource = XMLBookBuilderSource
End If
End Sub
<telerik:RadGrid ID="Parts" ClientIDMode="Static" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" GridLines="None" ShowStatusBar="True" CssClass="Parts" ClientSettings-AllowDragToGroup="true" ClientSettings-AllowRowsDragDrop="true">
<ClientSettings AllowRowsDragDrop="true">
<Selecting AllowRowSelect="True" EnableDragToSelectRows="false" />
</ClientSettings>
<MasterTableView Name="PartsTableView" DataKeyNames="InventoryID" AllowSorting="True" NoDetailRecordsText="No Items" HierarchyLoadMode="ServerBind"
NoMasterRecordsText="No Items Added" CommandItemDisplay="Bottom" EditMode="Batch">
<CommandItemSettings ShowAddNewRecordButton="false" />
<BatchEditingSettings EditType="Cell" />
<DetailTables>
<telerik:GridTableView Name="ComponentDetails" Caption="Component Details" DataKeyNames="InventoryID"
runat="server">
<ParentTableRelation>
<telerik:GridRelationFields DetailKeyField="ParentID" MasterKeyField="InventoryID" />
</ParentTableRelation>
<Columns>
<telerik:GridBoundColumn DataField="ParentID" Display="false" UniqueName="ParentID" />
<telerik:GridBoundColumn DataField="InventoryID" HeaderText="InventoryID" ReadOnly="true"
UniqueName="InventoryID" />
<telerik:GridBoundColumn DataField="FormNumber" HeaderText="Form #" UniqueName="FormNumber" />
<telerik:GridBoundColumn DataField="Description" HeaderText="Description" ReadOnly="true"
UniqueName="Description" Display="True" />
</Columns>
</telerik:GridTableView>
</DetailTables>
<Columns>
<telerik:GridTemplateColumn UniqueName="ThumbNail">
<ItemTemplate>
<asp:Image ID="imgThumbNail" runat="server" ImageUrl='<%#Eval("ImageUrl")%>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="InventoryID" UniqueName="FormNumber" Display="false" />
<telerik:GridBoundColumn DataField="Text" HeaderText="Product Code" />
<telerik:GridBoundColumn DataField="Description" Display="false" />
<telerik:GridTemplateColumn UniqueName="Description" HeaderText="Title">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%#Eval("Description") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDescription" runat="server" Text='<%#Eval("Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="Type" Display="false" />
<telerik:GridEditCommandColumn ButtonType="ImageButton" EditText="Edit" UniqueName="EditCommandColumn">
</telerik:GridEditCommandColumn>
<telerik:GridButtonColumn ButtonType="ImageButton"
CommandName="DeleteInventory" Text="Remove" UniqueName="Delete">
<ItemStyle HorizontalAlign="Center" CssClass="MyImageButton"></ItemStyle>
<HeaderStyle Width="20px"></HeaderStyle>
</telerik:GridButtonColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:XmlDataSource ID="XMLBookBuilderSource" runat="server">
</asp:XmlDataSource>
It seems that you are using a declarative DataSource control for RadGrid. In that case you should set the DataSourceID property of RadGrid. Also the NeedDataSource event would not be needed in this scenario. Check out the following article that describes how you can bind RadGrid to a declarative DataSource in more detail.
Regards,
Viktor Tachev
Telerik