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

Hierarchical RadGrid not refreshed after ItemCommand update

2 Answers 101 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve Newbery
Top achievements
Rank 1
Steve Newbery asked on 06 Dec 2008, 10:57 AM
I have a GridButtonColumn in a hierarchical RadGrid that fires a custom update via the ItemCommand event. The update sets the value of an "OrderStatus" field to "X".

My problem is that while the update does correctly set the value in the db row, the column in the grid still shows the old value, even if I use "rebind".

The ItemCommand handler is:

    Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)  
        If e.CommandName = "CancelOrder" Then  
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  
            Dim OrderID As String = item.OwnerTableView.DataKeyValues(item.ItemIndex)("OrderID").ToString()  
            AccessDataSource2.UpdateCommand = "UPDATE [Orders] SET [OrderStatus] = 'X' WHERE [OrderID] = " & OrderID  
            AccessDataSource2.Update()  
        End If  
    End Sub  
 

And the GridButtonColumn:

    <telerik:GridButtonColumn  
        CommandName="CancelOrder" 
        Text="Cancel" 
        UniqueName="OrderCancelColumn"   
        ConfirmText="Are you sure you want to cancel this order?"   
        DataTextFormatString="Cancel" 
        DataTextField="OrderID">   
    </telerik:GridButtonColumn> 
 

Can anyone see what I'm doing wrong? I tried turning off Ajax, but that didn't change anything. I also tried using an SqlCommand, but no change. I tried explicitly rebinding with RadGrid1.MasterTableView.DetailTables(0).Rebind() but that didn't work either.

I think this problem is because I'm using a hierarchy, because when I don't use a hierarchy, it all works as expected. 

Many thanks, Steve

PS the full page is here, it just needs the Nwind.mdb Access database to run, with an extra column OrderStatus in the Orders table:

<%@ Page Language="VB"  ValidateRequest="false" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
 
<script runat="server">  
 
    Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)  
        If e.CommandName = "CancelOrder" Then  
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  
            Dim OrderID As String = item.OwnerTableView.DataKeyValues(item.ItemIndex)("OrderID").ToString()  
            AccessDataSource2.UpdateCommand = "UPDATE [Orders] SET [OrderStatus] = 'X' WHERE [OrderID] = " & OrderID  
            AccessDataSource2.Update()  
        End If  
    End Sub  
 
</script> 
 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server">  
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
        <div> 
            <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" runat="server"   
                AutoGenerateColumns="False"   
                Skin="Telerik" 
                PageSize="3"   
                AllowSorting="True"   
                AllowPaging="True"   
                DataSourceID="AccessDataSource1" 
                OnItemCommand="RadGrid1_ItemCommand">  
                <MasterTableView   
                    DataKeyNames="CustomerID" Width="100%"   
                    HierarchyLoadMode="Client" 
                    Name="Customers"   
                    DataSourceID="AccessDataSource1">  
                    <DetailTables> 
                        <telerik:GridTableView   
                            DataKeyNames="OrderID"   
                            Name="Orders" Width="100%"   
                            HierarchyLoadMode="Client" 
                            DataSourceID="AccessDataSource2">  
                            <ParentTableRelation> 
                                <telerik:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" /> 
                            </ParentTableRelation> 
                            <DetailTables> 
                                <telerik:GridTableView   
                                    DataKeyNames="OrderID"   
                                    Name="OrderDetails" Width="100%"   
                                    DataSourceID="AccessDataSource3">  
                                    <ParentTableRelation> 
                                        <telerik:GridRelationFields DetailKeyField="OrderID" MasterKeyField="OrderID" /> 
                                    </ParentTableRelation> 
                                    <Columns> 
                                        <telerik:GridBoundColumn SortExpression="UnitPrice" HeaderText="Unit Price" HeaderButtonType="TextButton" 
                                            DataField="UnitPrice">  
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn SortExpression="Quantity" HeaderText="Quantity" HeaderButtonType="TextButton" 
                                            DataField="Quantity">  
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn SortExpression="Discount" HeaderText="Discount" HeaderButtonType="TextButton" 
                                            DataField="Discount">  
                                        </telerik:GridBoundColumn> 
                                    </Columns> 
                                </telerik:GridTableView> 
                            </DetailTables> 
                            <Columns> 
                                <telerik:GridBoundColumn SortExpression="OrderID" HeaderText="OrderID" HeaderButtonType="TextButton" 
                                    DataField="OrderID">  
                                </telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn SortExpression="OrderDate" HeaderText="Date Ordered" HeaderButtonType="TextButton" 
                                    DataField="OrderDate">  
                                </telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn SortExpression="Freight" HeaderText="Freight" HeaderButtonType="TextButton" 
                                    DataField="Freight">  
                                </telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn SortExpression="OrderStatus" HeaderText="Status" HeaderButtonType="TextButton" 
                                    DataField="OrderStatus">  
                                </telerik:GridBoundColumn> 
                                <telerik:GridButtonColumn  
                                    CommandName="CancelOrder" 
                                    Text="Cancel" 
                                    UniqueName="OrderCancelColumn"   
                                    ConfirmText="Are you sure you want to cancel this order?"   
                                    DataTextFormatString="Cancel" 
                                    DataTextField="OrderID">   
                                </telerik:GridButtonColumn> 
 
                            </Columns> 
                        </telerik:GridTableView> 
                    </DetailTables> 
                    <Columns> 
                        <telerik:GridBoundColumn SortExpression="CustomerID" HeaderText="CustomerID" HeaderButtonType="TextButton" 
                            DataField="CustomerID">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="ContactName" HeaderText="Contact Name" HeaderButtonType="TextButton" 
                            DataField="ContactName">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="CompanyName" HeaderText="Company" HeaderButtonType="TextButton" 
                            DataField="CompanyName">  
                        </telerik:GridBoundColumn> 
                    </Columns> 
                </MasterTableView> 
                <ClientSettings AllowExpandCollapse="true">  
                </ClientSettings> 
            </telerik:RadGrid> 
              
             <asp:AccessDataSource ID="AccessDataSource1" DataFile="~/App_Data/Nwind.mdb" 
                SelectCommand="SELECT * FROM Customers" runat="server">  
            </asp:AccessDataSource> 
              
            <asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~/App_Data/Nwind.mdb" 
                SelectCommand="SELECT * FROM Orders Where CustomerID = ?">  
                <SelectParameters> 
                    <asp:SessionParameter Name="CustomerID" SessionField="CustomerID" Type="string" /> 
                </SelectParameters> 
            </asp:AccessDataSource> 
              
            <asp:AccessDataSource ID="AccessDataSource3" DataFile="~/App_Data/Nwind.mdb" 
                SelectCommand="SELECT * FROM [Order Details] where OrderID = ?" runat="server">  
                <SelectParameters> 
                    <asp:SessionParameter Name="OrderID" SessionField="OrderID" Type="Int32" /> 
                </SelectParameters> 
            </asp:AccessDataSource> 
              
        </div> 
    </form> 
</body> 
</html> 
 

2 Answers, 1 is accepted

Sort by
0
Steve Newbery
Top achievements
Rank 1
answered on 06 Dec 2008, 12:32 PM
Ok - I found a workaround - just by putting the exact same code in a  DeleteCommand handler, it works:

    Protected Sub RadGrid1_DeleteCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)  
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  
        Dim OrderID As String = item.OwnerTableView.DataKeyValues(item.ItemIndex)("OrderID").ToString()  
        AccessDataSource2.UpdateCommand = "UPDATE [Orders] SET [OrderStatus] = 'X' WHERE [OrderID] = " & OrderID  
        AccessDataSource2.Update()  
    End Sub  
 

and
    <telerik:GridButtonColumn  
        CommandName="Delete" 
        Text="Cancel the order" 
        UniqueName="OrderCancelColumn"   
        ConfirmText="Are you sure you want to cancel this order?">   
    </telerik:GridButtonColumn> 
 

Cool - not sure if this is entirely "correct" but hey it works!
0
Iana Tsolova
Telerik team
answered on 08 Dec 2008, 11:49 AM
Hello Steve,

Try modifying your code as below and let me know if it makes any difference:

Protected Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As GridCommandEventArgs)     
        If e.CommandName = "CancelOrder" Then     
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)     
            Dim OrderID As String = item.OwnerTableView.DataKeyValues(item.ItemIndex)("OrderID").ToString()     
            AccessDataSource2.UpdateCommand = "UPDATE [Orders] SET [OrderStatus] = 'X' WHERE [OrderID] = " & OrderID     
            AccessDataSource2.Update()    
            e.Item.OwnerTableView.DataBind()   
        End If     
    End Sub     
 
    <telerik:GridButtonColumn     
        CommandName="CancelOrder"    
        Text="Cancel"    
        UniqueName="OrderCancelColumn"      
        ConfirmText="Are you sure you want to cancel this order?"      
        DataTextFormatString="Cancel"    
        DataTextField="OrderID">      
    </telerik:GridButtonColumn>    
 

I hope this helps.

Greetings,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Steve Newbery
Top achievements
Rank 1
Answers by
Steve Newbery
Top achievements
Rank 1
Iana Tsolova
Telerik team
Share this question
or