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

Hierarchial Grid and Flood Down Values

5 Answers 98 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 06 Oct 2008, 05:54 PM
I have a hierarchial grid that will have 3 levels to it.  I want to keep this example as simple as possible so let's say the grid has 2 visible columns on the first 2 levels - a number column and button column.  The 3rd level has only a number column.  If the button is pushed on the 1st or 2nd level, the number column should be set to zero and all related child rows in the hierarchy below should be set to zero.  All rows updated should have the background set to a color, red for the sake of argument.

How can this be accomplished with VB.Net code?

Here's some starter code.  More columns will be visible than the 2, but just ignore them.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Grid.aspx.vb" Inherits="Grid" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server">  
    <title>Untitled Page</title> 
    <style type="text/css">  
 
        .ZeroedOut  
        {  
            background-color:pink;  
        }  
 
    </style> 
 
</head> 
<body> 
    <form id="form1" runat="server">  
    <div> 
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">  
        </telerik:RadScriptManager> 
        <telerik:RadGrid ID="grdOR" runat="server" AutoGenerateColumns="False" GridLines="None" Width="100%" Skin="WebBlue"  > 
            <MasterTableView DataKeyNames="STYL_SEQ_NUM,RVD_QTY" AllowAutomaticUpdates="True" NoDetailRecordsText="No SKU records to display." NoMasterRecordsText="No Style records to display." EditMode="InPlace">  
                <DetailTables> 
                    <telerik:GridTableView runat="server" DataKeyNames="MSC_MDSE_NUM" NoDetailRecordsText="No Store records to display." > 
                        <RowIndicatorColumn> 
                            <HeaderStyle Width="20px" /> 
                        </RowIndicatorColumn> 
                        <ExpandCollapseColumn Visible="True">  
                            <HeaderStyle Width="20px" /> 
                        </ExpandCollapseColumn> 
                        <Columns> 
                            <telerik:GridBoundColumn DataField="CLR_CDE" HeaderText="Color Code" UniqueName="CLR_CDE" > 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="CLR_DCD" HeaderText="Color Desc" UniqueName="CLR_DCD" > 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="STYL_SEQ_NUM" HeaderText="SSN" UniqueName="STYL_SEQ_NUM" > 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="MSC_MDSE_NUM" HeaderText="MSC" UniqueName="MSC_MDSE_NUM" > 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="RVD_QTY" HeaderText="RVD_QTY" UniqueName="RVD_QTY" ItemStyle-Width="50px" HeaderStyle-Width="50px">  
                            </telerik:GridBoundColumn> 
                            <telerik:GridButtonColumn ButtonType="PushButton" Text="Zero" UniqueName="ZERO" CommandName="ZEROSKU" ItemStyle-Width="50px" HeaderStyle-Width="50px">  
                            </telerik:GridButtonColumn> 
                        </Columns> 
                        <DetailTables> 
                            <telerik:GridTableView runat="server" DataKeyNames="STOR_CDE,MSC_MDSE_NUM,RVD_QTY" EditMode="InPlace" AllowAutomaticUpdates="True" Width="100%" > 
                                <RowIndicatorColumn> 
                                    <HeaderStyle Width="20px" /> 
                                </RowIndicatorColumn> 
                                <ExpandCollapseColumn> 
                                    <HeaderStyle Width="20px" /> 
                                </ExpandCollapseColumn> 
                                <Columns> 
                                    <telerik:GridEditCommandColumn /> 
                                    <telerik:GridTemplateColumn DataField="STOR_CDE" HeaderText="Store" UniqueName="STOR_CDE" > 
                                        <ItemTemplate> 
                                            <%#Eval("STOR_CDE")%> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <asp:TextBox ID="txtStore" runat="server" Text='<%# Bind("STOR_CDE") %>' ReadOnly='<%# IIf(DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "False", "True") %>'/>  
                                        </EditItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                    <telerik:GridTemplateColumn DataField="ORDR_QTY" HeaderText="Quantity" UniqueName="ORDR_QTY">  
                                        <ItemTemplate> 
                                            <%#Eval("ORDR_QTY")%> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <asp:TextBox ID="txtOrderQty" runat="server" Text='<%# Bind("ORDR_QTY") %>' ReadOnly='<%# IIf(DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "False", "True") %>' /> 
                                        </EditItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                    <telerik:GridTemplateColumn DataField="MSC_MDSE_NUM" HeaderText="MSC" UniqueName="MSC_MDSE_NUM">  
                                        <ItemTemplate> 
                                            <%#Eval("MSC_MDSE_NUM")%> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <%# Eval("MSC_MDSE_NUM") %> 
                                        </EditItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                    <telerik:GridTemplateColumn DataField="RVD_QTY" HeaderText="RVD_QTY" UniqueName="RVD_QTY" ItemStyle-Width="110px" HeaderStyle-Width="110px">  
                                        <ItemTemplate> 
                                            <%#Eval("RVD_QTY")%> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <asp:TextBox ID="txtRevisedQuantity" runat="server" Text='<%# Bind("RVD_QTY") %>' /> 
                                        </EditItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                </Columns> 
                                <EditFormSettings> 
                                    <EditColumn UniqueName="EditCommandColumn1">  
                                    </EditColumn> 
                                </EditFormSettings> 
                            </telerik:GridTableView> 
                        </DetailTables>                         
                    </telerik:GridTableView> 
                </DetailTables> 
                <RowIndicatorColumn> 
                    <HeaderStyle Width="20px" /> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn Visible="True">  
                    <HeaderStyle Width="20px" /> 
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="ITEM_DES" HeaderText="Desc" UniqueName="ITEM_DES">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="VEN_STYL_NUM" HeaderText="Style" UniqueName="VEN_STYL_NUM">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="STYL_SEQ_NUM" HeaderText="SSN" UniqueName="STYL_SEQ_NUM">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridTemplateColumn DataField="RVD_QTY" HeaderText="RVD_QTY" UniqueName="RVD_QTY" ItemStyle-Width="50px" HeaderStyle-Width="50px">  
                        <ItemTemplate> 
                            <%#Eval("RVD_QTY")%> 
                        </ItemTemplate> 
                        <EditItemTemplate> 
                            <asp:TextBox ID="txtRevisedStyle" runat="server" Text='<%# Bind("RVD_QTY") %>' /> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridButtonColumn ButtonType="PushButton" Text="Zero" UniqueName="ZERO" CommandName="ZEROSTYLE" ItemStyle-Width="50px" HeaderStyle-Width="50px">  
                    </telerik:GridButtonColumn> 
                </Columns> 
                <EditFormSettings > 
                    <PopUpSettings Modal="True" /> 
                </EditFormSettings> 
            </MasterTableView> 
            <ClientSettings> 
                <Scrolling AllowScroll="True" UseStaticHeaders="True" /> 
            </ClientSettings> 
        </telerik:RadGrid> 
      
    </div> 
    </form> 
</body> 
</html> 
 

Imports System.Data  
Imports Telerik.Web.UI  
 
Partial Class Grid  
 
    Inherits System.Web.UI.Page  
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
 
    End Sub  
 
    Protected Sub grdOR_DetailTableDataBind(ByVal source As Object, ByVal e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles grdOR.DetailTableDataBind  
 
        Dim parentItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)  
 
        If parentItem.Edit Then  
            Return  
        End If  
 
        Dim DataKeys() As String = parentItem.KeyValues.Split(",")  
 
        If e.DetailTableView.HierarchyIndex = "0" Then  
 
            If ViewState(e.DetailTableView.ClientID) Is Nothing Then  
 
                Dim Keys() As String = DataKeys(0).Split(":")  
                e.DetailTableView.DataSource = PopulateSKULevel(Keys(1).Replace("""", "").Replace("}", ""))  
                ee.DetailTableView.Name = e.DetailTableView.ClientID  
                ViewState(e.DetailTableView.ClientID) = e.DetailTableView.DataSource  
 
            Else  
 
                e.DetailTableView.DataSource = ViewState(e.DetailTableView.ClientID)  
 
            End If  
 
        ElseIf e.DetailTableView.HierarchyIndex.Substring(0, 2) = "0_" Then  
 
            If ViewState(e.DetailTableView.ClientID) Is Nothing Then  
 
                Dim Keys() As String = DataKeys(0).Split(":")  
                e.DetailTableView.DataSource = PopulateStoreLevel(Keys(1).Replace("""", "").Replace("}", ""))  
                ee.DetailTableView.Name = e.DetailTableView.ClientID  
                ViewState(e.DetailTableView.ClientID) = e.DetailTableView.DataSource  
 
            Else  
 
                e.DetailTableView.DataSource = ViewState(e.DetailTableView.ClientID)  
 
                If ViewState("grdOR_Event") = "STOREUPDATED" Then  
 
                    Dim parentSKU As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)  
                    Dim parentStyle As GridDataItem = CType(parentSKU.OwnerTableView.ParentItem, GridDataItem)  
 
                    parentSKU.OwnerTableView.DataSource = ViewState(ViewState("grdOR_DataSource_SKU").ToString)  
                    parentStyle.OwnerTableView.DataSource = ViewState(ViewState("grdOR_DataSource_Style").ToString)  
 
                    ViewState("grdOR_Event") = ""  
                    ViewState("grdOR_DataSource_SKU") = ""  
                    ViewState("grdOR_DataSource_Style") = ""  
 
                End If  
 
            End If  
 
        End If  
 
    End Sub  
 
    Protected Sub grdOR_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdOR.ItemCommand  
 
        Select Case e.CommandName  
 
            Case "ZEROSTYLE"  
 
                Dim StyleItem As GridDataItem = CType(e.Item, GridDataItem)  
                Dim dtStyle As DataTable = ViewState(grdOR.ClientID)  
                Dim SSN As String = StyleItem.OwnerTableView.DataKeyValues(StyleItem.ItemIndex)("STYL_SEQ_NUM").ToString  
                Dim Revised As String = StyleItem.OwnerTableView.DataKeyValues(StyleItem.ItemIndex)("RVD_QTY").ToString  
                  
                ZeroParentRow(StyleItem, dtStyle, SSN, e.Canceled)  
 
                StyleItem.CssClass = "ZeroedOut" 
 
                If StyleItem.Expanded Then  
 
                    For Each SKUItem As GridDataItem In StyleItem.ChildItem.NestedTableViews(0).Items  
 
                        Dim dtSKU As DataTable = ViewState(SKUItem.OwnerTableView.Name)  
 
                        ZeroParentRow(SKUItem, dtSKU, SSN, e.Canceled)  
 
                    Next  
 
                    StyleItem.Expanded = True 
 
                End If  
 
            Case "ZEROSKU"  
 
        End Select  
 
    End Sub  
 
    Public Sub ZeroParentRow(ByRef RowItem As GridDataItem, ByRef dt As DataTable, ByVal SSN As String, ByRef Canceled As Boolean)  
 
        Dim changedRows() As DataRow = dt.Select("STYL_SEQ_NUM = " & SSN)  
 
        If (changedRows.Length <> 1) Then  
            'MeMe.Label1.Text = Me.Label1.Text + "Unable to locate the Order for updating."  
            Canceled = True 
            Return  
        End If  
 
        changedRows(0).BeginEdit()  
 
        Try  
 
            changedRows(0)("RVD_QTY") = 0  
            changedRows(0).EndEdit()  
            changedRows(0).AcceptChanges()  
 
            grdOR.DataSource = dt 
            'grdOR.Rebind()  
            ViewState(RowItem.OwnerTableView.ClientID) = dt  
 
            'RowItem.Expanded = True 
 
        Catch ex As Exception  
 
            changedRows(0).CancelEdit()  
            'Label1Label1.Text = Label1.Text & "Unable to update Orders. Reason: " & ex.Message  
            Canceled = True 
 
        End Try  
 
    End Sub  
 
    Protected Sub grdOR_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdOR.NeedDataSource  
 
        grdOR.DataSource = PopulateStyleLevel()  
        ViewState(grdOR.ClientID) = grdOR.DataSource  
 
    End Sub  
 
    Private Function CalculateStoreTotal(ByVal dt As DataTable) As Integer  
 
        Dim newValues As New Hashtable  
        Dim Total As Integer = 0 
 
        For Each r As DataRow In dt.Rows  
 
            Total += Convert.ToInt32(r("RVD_QTY"))  
 
        Next  
 
        Return Total  
 
    End Function      
 
    Private Function PopulateStyleLevel() As DataTable  
 
        Dim dt As New DataTable  
        Dim c1 As New DataColumn  
        Dim c2 As New DataColumn  
        Dim c3 As New DataColumn  
        Dim c4 As New DataColumn  
 
        c1.ColumnName = "ITEM_DES" 
        c2.ColumnName = "VEN_STYL_NUM" 
        c3.ColumnName = "STYL_SEQ_NUM" 
        c4.ColumnName = "RVD_QTY" 
 
        dt.Columns.Add(c1)  
        dt.Columns.Add(c2)  
        dt.Columns.Add(c3)  
        dt.Columns.Add(c4)  
 
        Dim r As DataRow = dt.NewRow  
        r("ITEM_DES") = "Test Item 1"  
        r("VEN_STYL_NUM") = "TEST100"  
        r("STYL_SEQ_NUM") = "0123456789999"  
        r("RVD_QTY") = "20"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("ITEM_DES") = "Test Item 2"  
        r("VEN_STYL_NUM") = "TEST200"  
        r("STYL_SEQ_NUM") = "0123456788888"  
        r("RVD_QTY") = "10"  
        dt.Rows.Add(r)  
 
        'grdOR.DataSource = dt 
        'grdOR.DataSource = New DataView(dt)  
        'grdOR.DataBind()  
 
        Return dt  
 
    End Function  
 
    Private Function PopulateSKULevel(ByVal SSN As String) As DataTable  
 
        Dim dt As New DataTable  
        Dim c1 As New DataColumn  
        Dim c2 As New DataColumn  
        Dim c3 As New DataColumn  
        Dim c4 As New DataColumn  
        Dim c5 As New DataColumn  
 
        c1.ColumnName = "CLR_CDE" 
        c2.ColumnName = "CLR_DCD" 
        c3.ColumnName = "STYL_SEQ_NUM" 
        c4.ColumnName = "MSC_MDSE_NUM" 
        c5.ColumnName = "RVD_QTY" 
 
        dt.Columns.Add(c1)  
        dt.Columns.Add(c2)  
        dt.Columns.Add(c3)  
        dt.Columns.Add(c4)  
        dt.Columns.Add(c5)  
 
        Dim r As DataRow = dt.NewRow  
        r("CLR_CDE") = "100"  
        r("CLR_DCD") = "BLUE"  
        r("STYL_SEQ_NUM") = "0123456789999"  
        r("MSC_MDSE_NUM") = "0123456789999"  
        r("RVD_QTY") = "20"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("CLR_CDE") = "200"  
        r("CLR_DCD") = "RED"  
        r("STYL_SEQ_NUM") = "0123456788888"  
        r("MSC_MDSE_NUM") = "0123456788888"  
        r("RVD_QTY") = "10"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("CLR_CDE") = "300"  
        r("CLR_DCD") = "YELLOW"  
        r("STYL_SEQ_NUM") = "0123456788888"  
        r("MSC_MDSE_NUM") = "0123456787777"  
        r("RVD_QTY") = "40"  
        dt.Rows.Add(r)  
 
        Dim rows() As DataRow = dt.Select("STYL_SEQ_NUM = '" & SSN & "'")  
        Dim dtRet As DataTable = dt.Clone  
 
        For Each row As DataRow In rows  
 
            r = dtRet.NewRow  
            r(0) = row(0)  
            r(1) = row(1)  
            r(2) = row(2)  
            r(3) = row(3)  
            r(4) = row(4)  
            dtRet.Rows.Add(r)  
 
        Next  
 
        'dtRet.TableName = "SKU" 
 
        Return dtRet  
 
    End Function  
 
    Private Function PopulateStoreLevel(ByVal MSC As String) As DataTable  
 
        Dim dt As New DataTable  
        Dim c1 As New DataColumn  
        Dim c2 As New DataColumn  
        Dim c3 As New DataColumn  
        Dim c4 As New DataColumn  
 
        c1.ColumnName = "STOR_CDE" 
        c2.ColumnName = "ORDR_QTY" 
        c3.ColumnName = "MSC_MDSE_NUM" 
        c4.ColumnName = "RVD_QTY" 
 
        dt.Columns.Add(c1)  
        dt.Columns.Add(c2)  
        dt.Columns.Add(c3)  
        dt.Columns.Add(c4)  
 
        Dim r As DataRow = dt.NewRow  
        r("STOR_CDE") = "21"  
        r("ORDR_QTY") = "5"  
        r("MSC_MDSE_NUM") = "0123456789999"  
        r("RVD_QTY") = "20"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("STOR_CDE") = "54"  
        r("ORDR_QTY") = "7"  
        r("MSC_MDSE_NUM") = "0123456788888"  
        r("RVD_QTY") = "10"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("STOR_CDE") = "67"  
        r("ORDR_QTY") = "5"  
        r("MSC_MDSE_NUM") = "0123456787777"  
        r("RVD_QTY") = "40"  
        dt.Rows.Add(r)  
 
        Dim rows() As DataRow = dt.Select("MSC_MDSE_NUM = '" & MSC & "'")  
        Dim dtRet As DataTable = dt.Clone  
 
        For Each row As DataRow In rows  
 
            r = dtRet.NewRow  
            r(0) = row(0)  
            r(1) = row(1)  
            r(2) = row(2)  
            r(3) = row(3)  
            dtRet.Rows.Add(r)  
 
        Next  
 
        Return dtRet  
 
    End Function  
 
End Class  
 

5 Answers, 1 is accepted

Sort by
0
Rob
Top achievements
Rank 1
answered on 06 Oct 2008, 08:25 PM
OK, I got it to work with this event and sub:

Protected Sub grdOR_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdOR.ItemCommand  
 
        Select Case e.CommandName  
 
            Case "ZEROSTYLE"  
 
                Dim StyleItem As GridDataItem = CType(e.Item, GridDataItem)  
                Dim dtStyle As DataTable = ViewState(grdOR.ClientID)  
                Dim SSN As String = StyleItem.OwnerTableView.DataKeyValues(StyleItem.ItemIndex)("STYL_SEQ_NUM").ToString  
                Dim Revised As String = StyleItem.OwnerTableView.DataKeyValues(StyleItem.ItemIndex)("RVD_QTY").ToString  
                  
                If StyleItem.Expanded Then  
 
                    ViewState("grdOR_Expand") = StyleItem.ItemIndex  
 
                    For Each SKUItem As GridDataItem In StyleItem.ChildItem.NestedTableViews(0).Items  
 
                        If SKUItem.Expanded Then  
 
                            ViewState("grdOR_Expand") = StyleItem.ItemIndex & "," & SKUItem.ItemIndex  
 
                            For Each StoreItem As GridDataItem In SKUItem.ChildItem.NestedTableViews(0).Items  
 
                                Dim dtStore As DataTable = ViewState(StoreItem.OwnerTableView.Name)  
                                Dim MSC As String = SKUItem.OwnerTableView.DataKeyValues(SKUItem.ItemIndex)("MSC_MDSE_NUM").ToString  
                                ZeroOutRow(StoreItem, dtStore, "MSC", MSC, e.Canceled)  
 
                            Next  
 
                        End If  
 
                        Dim dtSKU As DataTable = ViewState(SKUItem.OwnerTableView.Name)  
 
                        ZeroOutRow(SKUItem, dtSKU, "SSN", SSN, e.Canceled)  
 
                    Next  
 
                Else  
 
                    ViewState("grdOR_Expand") = Nothing  
 
                End If  
 
                ZeroOutRow(StyleItem, dtStyle, "SSN", SSN, e.Canceled)  
 
            Case "ZEROSKU"  
 
        End Select  
 
    End Sub  
 
 
 
    Public Sub ZeroOutRow(ByRef RowItem As GridDataItem, ByRef dt As DataTable, ByVal Field As String, ByVal FieldValue As String, ByRef Canceled As Boolean)  
 
        Dim strField As String = "" 
 
        Select Case Field  
 
            Case "SSN"  
 
                strField = "STYL_SEQ_NUM" 
 
            Case "MSC"  
 
                strField = "MSC_MDSE_NUM" 
 
        End Select  
 
 
        Dim changedRows() As DataRow = dt.Select(strField & " = " & FieldValue)  
 
        If (changedRows.Length < 0) Then  
            'MeMe.Label1.Text = Me.Label1.Text + "Unable to locate the Order for updating."  
            Canceled = True 
            Return  
        End If  
 
        Try  
 
            For i As Integer = 0 To changedRows.Length - 1  
 
                changedRows(i).BeginEdit()  
                changedRows(i)("RVD_QTY") = 0  
                changedRows(i).EndEdit()  
                changedRows(i).AcceptChanges()  
 
            Next  
 
            grdOR.DataSource = dt 
            grdOR.Rebind()  
            ViewState(RowItem.OwnerTableView.ClientID) = dt  
 
        Catch ex As Exception  
 
            changedRows(0).CancelEdit()  
            'Label1Label1.Text = Label1.Text & "Unable to update Orders. Reason: " & ex.Message  
            Canceled = True 
 
        End Try  
 
    End Sub 

Now the problem is that on the postback after rebinding() the changed datasources, any rows that were expanded are now collapsed.  I tried the following code:

Protected Sub grdOR_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdOR.PreRender  
 
        If Not ViewState("grdOR_Expand") Is Nothing Then  
 
            Dim dgiStyle As GridDataItem = Nothing 
            Dim dgiSKU As GridDataItem = Nothing 
            Dim Keys() As String = ViewState("grdOR_Expand").ToString.Split(",")  
 
            For i As Integer = 0 To Keys.Length - 1  
 
                Select Case i  
 
                    Case 0  
 
                        dgiStyle = grdOR.MasterTableView.Items(Convert.ToInt32(Keys(i)))  
                        dgiStyle.Expanded = True 
 
                    Case 1  
 
                        dgiSKU = dgiStyle.ChildItem.OwnerTableView.Items(Convert.ToInt32(Keys(i)))  
                        dgiSKU.Expanded = True 
 
                End Select  
 
 
 
            Next  
 
            ViewState("grdOR_Expand") = Nothing  
 
        End If 

It works well if only the masterview is expanded, but once I expand the detail table, it blows up on this line in the Select Case statment in the above code:

dgiStyle = grdOR.MasterTableView.Items(Convert.ToInt32(Keys(i)))  

Is there a better approach for persisting the state of expanded rows?

How do I keep the grid from flashing between postbacks?
0
Rob
Top achievements
Rank 1
answered on 07 Oct 2008, 02:35 PM
Hi all,

I have a page with a grid on it that has 3 levels in the hierarchy.  Please do the following to reproduce the problem.

Working:

Start the debugger.  2 rows are visible in the grid.  Expand 1 master row.  Click the button on the expanded Master row.  The master & detail rows bgcolor is changed to pink and the RVD_QTY column is zeroed out for all related rows.  Stop the debugger.

Problem:

Start the debugger again.  2 rows are visible in the grid.  Expand 1 master row.  Expand 1 detail row.  Click the button on the expanded Master row.  An error is thrown on a line in RadGrid_PreRender() event that executed successfully in the first example.  Here is the line:

dgiStyle = grdOR.MasterTableView.Items(Convert.ToInt32(Keys(i)))  

The value being passed in is either 0 or 1 in both cases.

I really don't understand why this line loses reference to the Items collection of the MasterTableView.  Help Please!

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Grid.aspx.vb" Inherits="Grid" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server">  
    <title>Untitled Page</title> 
    <style type="text/css">  
 
        .ZeroedOut  
        {  
            background-color:pink;  
        }  
          
        .ButtonN  
        {  
            height: 20px;  
            width: 50px;  
        }  
 
    </style> 
 
</head> 
<body> 
    <form id="form1" runat="server">  
    <div> 
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">  
        </telerik:RadScriptManager> 
        <telerik:RadGrid ID="grdOR" runat="server" AutoGenerateColumns="False" GridLines="None" Width="100%" Skin="WebBlue"  > 
            <MasterTableView DataKeyNames="STYL_SEQ_NUM,RVD_QTY" AllowAutomaticUpdates="True" NoDetailRecordsText="No SKU records to display." NoMasterRecordsText="No Style records to display." EditMode="InPlace">  
                <DetailTables> 
                    <telerik:GridTableView runat="server" DataKeyNames="MSC_MDSE_NUM" NoDetailRecordsText="No Store records to display." > 
                        <RowIndicatorColumn> 
                            <HeaderStyle Width="20px" /> 
                        </RowIndicatorColumn> 
                        <ExpandCollapseColumn Visible="True">  
                            <HeaderStyle Width="20px" /> 
                        </ExpandCollapseColumn> 
                        <Columns> 
                            <telerik:GridBoundColumn DataField="CLR_CDE" HeaderText="Color Code" UniqueName="CLR_CDE" > 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="CLR_DCD" HeaderText="Color Desc" UniqueName="CLR_DCD" > 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="STYL_SEQ_NUM" HeaderText="SSN" UniqueName="STYL_SEQ_NUM" > 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="MSC_MDSE_NUM" HeaderText="MSC" UniqueName="MSC_MDSE_NUM" > 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="RVD_QTY" HeaderText="RVD_QTY" UniqueName="RVD_QTY" ItemStyle-Width="50px" HeaderStyle-Width="50px">  
                            </telerik:GridBoundColumn> 
                            <telerik:GridButtonColumn ButtonType="PushButton" Text="Zero" UniqueName="ZERO" CommandName="ZEROSKU" ItemStyle-Width="50px" HeaderStyle-Width="50px">  
                            </telerik:GridButtonColumn> 
                        </Columns> 
                        <DetailTables> 
                            <telerik:GridTableView runat="server" DataKeyNames="STOR_CDE,MSC_MDSE_NUM,RVD_QTY" EditMode="InPlace" AllowAutomaticUpdates="True" Width="100%" > 
                                <RowIndicatorColumn> 
                                    <HeaderStyle Width="20px" /> 
                                </RowIndicatorColumn> 
                                <ExpandCollapseColumn> 
                                    <HeaderStyle Width="20px" /> 
                                </ExpandCollapseColumn> 
                                <Columns> 
                                    <telerik:GridEditCommandColumn /> 
                                    <telerik:GridTemplateColumn DataField="STOR_CDE" HeaderText="Store" UniqueName="STOR_CDE" > 
                                        <ItemTemplate> 
                                            <%#Eval("STOR_CDE")%> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <asp:TextBox ID="txtStore" runat="server" Text='<%# Bind("STOR_CDE") %>' ReadOnly='<%# IIf(DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "False", "True") %>'/>  
                                        </EditItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                    <telerik:GridTemplateColumn DataField="ORDR_QTY" HeaderText="Quantity" UniqueName="ORDR_QTY">  
                                        <ItemTemplate> 
                                            <%#Eval("ORDR_QTY")%> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <asp:TextBox ID="txtOrderQty" runat="server" Text='<%# Bind("ORDR_QTY") %>' ReadOnly='<%# IIf(DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "False", "True") %>' /> 
                                        </EditItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                    <telerik:GridTemplateColumn DataField="MSC_MDSE_NUM" HeaderText="MSC" UniqueName="MSC_MDSE_NUM">  
                                        <ItemTemplate> 
                                            <%#Eval("MSC_MDSE_NUM")%> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <%# Eval("MSC_MDSE_NUM") %> 
                                        </EditItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                    <telerik:GridTemplateColumn DataField="RVD_QTY" HeaderText="RVD_QTY" UniqueName="RVD_QTY" ItemStyle-Width="110px" HeaderStyle-Width="110px">  
                                        <ItemTemplate> 
                                            <%#Eval("RVD_QTY")%> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <asp:TextBox ID="txtRevisedQuantity" runat="server" Text='<%# Bind("RVD_QTY") %>' /> 
                                        </EditItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                </Columns> 
                                <EditFormSettings> 
                                    <EditColumn UniqueName="EditCommandColumn1">  
                                    </EditColumn> 
                                </EditFormSettings> 
                            </telerik:GridTableView> 
                        </DetailTables>                         
                    </telerik:GridTableView> 
                </DetailTables> 
                <RowIndicatorColumn> 
                    <HeaderStyle Width="20px" /> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn Visible="True">  
                    <HeaderStyle Width="20px" /> 
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="ITEM_DES" HeaderText="Desc" UniqueName="ITEM_DES">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="VEN_STYL_NUM" HeaderText="Style" UniqueName="VEN_STYL_NUM">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="STYL_SEQ_NUM" HeaderText="SSN" UniqueName="STYL_SEQ_NUM">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridTemplateColumn DataField="RVD_QTY" HeaderText="RVD_QTY" UniqueName="RVD_QTY" ItemStyle-Width="50px" HeaderStyle-Width="50px">  
                        <ItemTemplate> 
                            <%#Eval("RVD_QTY")%> 
                        </ItemTemplate> 
                        <EditItemTemplate> 
                            <asp:TextBox ID="txtRevisedStyle" runat="server" Text='<%# Bind("RVD_QTY") %>' /> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridButtonColumn ButtonType="PushButton" Text="Zero" UniqueName="ZERO" CommandName="ZEROSTYLE" ItemStyle-Width="50px" HeaderStyle-Width="50px">  
                    </telerik:GridButtonColumn> 
                </Columns> 
                <EditFormSettings > 
                    <PopUpSettings Modal="True" /> 
                </EditFormSettings> 
            </MasterTableView> 
            <ClientSettings> 
                <Scrolling AllowScroll="True" UseStaticHeaders="True" /> 
            </ClientSettings> 
        </telerik:RadGrid> 
      
    </div> 
    </form> 
</body> 
</html> 
 

Imports System.Data  
Imports Telerik.Web.UI  
 
Partial Class Grid  
 
    Inherits System.Web.UI.Page  
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
 
    End Sub  
 
    Protected Sub grdOR_DetailTableDataBind(ByVal source As Object, ByVal e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles grdOR.DetailTableDataBind  
 
        Dim parentItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)  
 
        If parentItem.Edit Then  
            Return  
        End If  
 
        Dim DataKeys() As String = parentItem.KeyValues.Split(",")  
 
        If e.DetailTableView.HierarchyIndex = "0" Then  
 
            If ViewState(e.DetailTableView.ClientID) Is Nothing Then  
 
                Dim Keys() As String = DataKeys(0).Split(":")  
                e.DetailTableView.DataSource = PopulateSKULevel(Keys(1).Replace("""", "").Replace("}", ""))  
                ee.DetailTableView.Name = e.DetailTableView.ClientID  
                ViewState(e.DetailTableView.ClientID) = e.DetailTableView.DataSource  
 
            Else  
 
                e.DetailTableView.DataSource = ViewState(e.DetailTableView.ClientID)  
 
            End If  
 
        ElseIf e.DetailTableView.HierarchyIndex.Substring(0, 2) = "0_" Then  
 
            If ViewState(e.DetailTableView.ClientID) Is Nothing Then  
 
                Dim Keys() As String = DataKeys(0).Split(":")  
                e.DetailTableView.DataSource = PopulateStoreLevel(Keys(1).Replace("""", "").Replace("}", ""))  
                ee.DetailTableView.Name = e.DetailTableView.ClientID  
                ViewState(e.DetailTableView.ClientID) = e.DetailTableView.DataSource  
 
            Else  
 
                e.DetailTableView.DataSource = ViewState(e.DetailTableView.ClientID)  
 
                If ViewState("grdOR_Event") = "STOREUPDATED" Then  
 
                    Dim parentSKU As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)  
                    Dim parentStyle As GridDataItem = CType(parentSKU.OwnerTableView.ParentItem, GridDataItem)  
 
                    parentSKU.OwnerTableView.DataSource = ViewState(ViewState("grdOR_DataSource_SKU").ToString)  
                    parentStyle.OwnerTableView.DataSource = ViewState(ViewState("grdOR_DataSource_Style").ToString)  
 
                    ViewState("grdOR_Event") = ""  
                    ViewState("grdOR_DataSource_SKU") = ""  
                    ViewState("grdOR_DataSource_Style") = ""  
 
                End If  
 
            End If  
 
        End If  
 
    End Sub  
 
    Protected Sub grdOR_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdOR.ItemCommand  
 
        Select Case e.CommandName  
 
            Case "ZEROSTYLE"  
 
                Dim StyleItem As GridDataItem = CType(e.Item, GridDataItem)  
                Dim dtStyle As DataTable = ViewState(grdOR.ClientID)  
                Dim SSN As String = StyleItem.OwnerTableView.DataKeyValues(StyleItem.ItemIndex)("STYL_SEQ_NUM").ToString  
                Dim Revised As String = StyleItem.OwnerTableView.DataKeyValues(StyleItem.ItemIndex)("RVD_QTY").ToString  
                  
                If StyleItem.Expanded Then  
 
                    ViewState("grdOR_Expand") = StyleItem.ItemIndex  
 
                    For Each SKUItem As GridDataItem In StyleItem.ChildItem.NestedTableViews(0).Items  
 
                        If SKUItem.Expanded Then  
 
                            ViewState("grdOR_Expand") = StyleItem.ItemIndex & "," & SKUItem.ItemIndex  
 
                            For Each StoreItem As GridDataItem In SKUItem.ChildItem.NestedTableViews(0).Items  
 
                                Dim dtStore As DataTable = ViewState(StoreItem.OwnerTableView.Name)  
                                Dim MSC As String = SKUItem.OwnerTableView.DataKeyValues(SKUItem.ItemIndex)("MSC_MDSE_NUM").ToString  
                                ZeroOutRow(StoreItem, dtStore, "MSC", MSC, e.Canceled)  
 
                            Next  
 
                        End If  
 
                        Dim dtSKU As DataTable = ViewState(SKUItem.OwnerTableView.Name)  
 
                        ZeroOutRow(SKUItem, dtSKU, "SSN", SSN, e.Canceled)  
 
                    Next  
 
                Else  
 
                    ViewState("grdOR_Expand") = Nothing  
                    ViewState("grdOR_MasterRow") = StyleItem.ItemIndex  
 
                End If  
 
                ZeroOutRow(StyleItem, dtStyle, "SSN", SSN, e.Canceled)  
 
            Case "ZEROSKU"  
 
        End Select  
 
    End Sub  
 
    Public Sub ZeroOutRow(ByRef RowItem As GridDataItem, ByRef dt As DataTable, ByVal Field As String, ByVal FieldValue As String, ByRef Canceled As Boolean)  
 
        Dim strField As String = "" 
 
        Select Case Field  
 
            Case "SSN"  
 
                strField = "STYL_SEQ_NUM" 
 
            Case "MSC"  
 
                strField = "MSC_MDSE_NUM" 
 
        End Select  
 
 
        Dim changedRows() As DataRow = dt.Select(strField & " = " & FieldValue)  
 
        If (changedRows.Length < 0) Then  
            'MeMe.Label1.Text = Me.Label1.Text + "Unable to locate the Order for updating."  
            Canceled = True 
            Return  
        End If  
 
        Try  
 
            For i As Integer = 0 To changedRows.Length - 1  
 
                changedRows(i).BeginEdit()  
                changedRows(i)("RVD_QTY") = 0  
                changedRows(i).EndEdit()  
                changedRows(i).AcceptChanges()  
 
            Next  
 
            grdOR.DataSource = dt 
            grdOR.Rebind()  
            ViewState(RowItem.OwnerTableView.ClientID) = dt  
 
        Catch ex As Exception  
 
            changedRows(0).CancelEdit()  
            'Label1Label1.Text = Label1.Text & "Unable to update Orders. Reason: " & ex.Message  
            Canceled = True 
 
        End Try  
 
    End Sub  
 
    Protected Sub grdOR_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grdOR.ItemDataBound  
 
        If TypeOf e.Item Is GridDataItem Then  
 
            Dim dataItem As GridDataItem = DirectCast(e.Item, GridDataItem)  
            Dim Keys() As String = dataItem.ItemIndexHierarchical.Split(":")  
 
            If Keys.Length < 3 Then  
 
                TryCast(dataItem("Zero").Controls(0), Button).CssClass = "ButtonN" 
 
            End If  
 
        End If  
 
    End Sub  
 
    Protected Sub grdOR_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdOR.NeedDataSource  
 
        grdOR.DataSource = PopulateStyleLevel()  
        ViewState(grdOR.ClientID) = grdOR.DataSource  
 
    End Sub  
 
    Protected Sub grdOR_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdOR.PreRender  
 
        Dim dgiStyle As GridDataItem = Nothing 
 
        If Not ViewState("grdOR_Expand") Is Nothing Then  
 
            Dim dgiSKU As GridDataItem = Nothing 
            Dim Keys() As String = ViewState("grdOR_Expand").ToString.Split(",")  
 
            For i As Integer = 0 To Keys.Length - 1  
 
                Select Case i  
 
                    Case 0  
 
                        dgiStyle = grdOR.MasterTableView.Items(Convert.ToInt32(Keys(i)))  
                        dgiStyle.Expanded = True 
                        dgiStyle.CssClass = "ZeroedOut" 
 
                        For Each SKUItem As GridDataItem In dgiStyle.ChildItem.NestedTableViews(0).Items  
 
                            SKUItem.CssClass = "ZeroedOut" 
 
                        Next  
 
                    Case 1  
 
                        dgiSKU = dgiStyle.ChildItem.OwnerTableView.Items(Convert.ToInt32(Keys(i)))  
                        dgiSKU.Expanded = True 
                        'dgiSKU.CssClass = "ZeroedOut" 
 
                        For Each StoreItem As GridDataItem In dgiSKU.ChildItem.NestedTableViews(0).Items  
 
                            StoreItem.CssClass = "ZeroedOut" 
 
                        Next  
 
                End Select  
 
            Next  
 
            ViewState("grdOR_Expand") = Nothing  
 
        ElseIf Not ViewState("grdOR_MasterRow") Is Nothing Then  
 
            dgiStyle = grdOR.MasterTableView.Items(Convert.ToInt32(ViewState("grdOR_MasterRow").ToString))  
            dgiStyle.CssClass = "ZeroedOut" 
            ViewState("grdOR_MasterRow") = Nothing  
 
        End If  
 
 
    End Sub  
 
    Protected Sub grdOR_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdOR.UpdateCommand  
 
        Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)  
        Dim dtStore As DataTable = ViewState(editedItem.OwnerTableView.ClientID)  
        Dim store As String = editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("STOR_CDE").ToString  
        'Dim store As String = editedItem("STOR_CDE").Text  
        Dim MSC As String = editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("MSC_MDSE_NUM").ToString  
        'Dim MSC As String = editedItem("MSC_MDSE_NUM").Text  
        Dim changedRows() As DataRow = dtStore.Select("STOR_CDE = " & store)  
 
        If (changedRows.Length <> 1) Then  
            'MeMe.Label1.Text = Me.Label1.Text + "Unable to locate the Order for updating."  
            e.Canceled = True 
            Return  
        End If  
 
        'Update new values  
        Dim newValues As Hashtable = New Hashtable  
 
        'The GridTableView will fill the values from all editable columns in the hash  
        editedItem.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)  
        changedRows(0).BeginEdit()  
 
        Try  
 
            For Each entry As DictionaryEntry In newValues  
                changedRows(0)(CType(entry.Key, String)) = entry.Value  
            Next  
 
            changedRows(0).EndEdit()  
 
            ViewState(editedItem.OwnerTableView.ClientID) = dtStore  
 
        Catch ex As Exception  
 
            changedRows(0).CancelEdit()  
            'Label1Label1.Text = Label1.Text & "Unable to update Orders. Reason: " & ex.Message  
            e.Canceled = True 
 
        End Try  
 
        'Get Revised Quantity Total for Stores  
        Dim StoreTotal As Integer = CalculateStoreTotal(dtStore) 'CalculateStoreTotal(e.Item.OwnerTableView.Items)  
        Dim parentSKU As GridDataItem = CType(e.Item.OwnerTableView.ParentItem, GridDataItem)  
 
        UpdateSKULevel(parentSKU, MSC, StoreTotal, e.Canceled)  
        UpdateStyleLevel(parentSKU, e.Canceled)  
 
    End Sub  
 
    Private Function CalculateStoreTotal(ByVal dt As DataTable) As Integer  
 
        Dim newValues As New Hashtable  
        Dim Total As Integer = 0 
 
        For Each r As DataRow In dt.Rows  
 
            Total += Convert.ToInt32(r("RVD_QTY"))  
 
        Next  
 
        Return Total  
 
    End Function  
 
    Private Function CalculateStoreTotal(ByVal StoreItems As GridDataItemCollection) As Integer  
 
        Dim newValues As New Hashtable  
        Dim Total As Integer = 0 
 
        For Each gdi As GridDataItem In StoreItems  
 
            newValues = New Hashtable  
            gdi.OwnerTableView.ExtractValuesFromItem(newValues, gdi)  
 
            If newValues.Count > 0 Then  
 
                Total += Convert.ToInt32(newValues("RVD_QTY").ToString)  
 
            End If  
 
        Next  
 
        Return Total  
 
    End Function  
 
    Private Sub UpdateStyleLevel(ByVal parentSKU As GridDataItem, ByRef Canceled As Boolean)  
 
        'Get Revised Quantity Total for Stores  
        Dim StyleTotal As Integer = 0 
        Dim newValues As Hashtable = New Hashtable  
        Dim SSN As String = parentSKU("STYL_SEQ_NUM").Text  
 
        For Each gdi As GridDataItem In parentSKU.OwnerTableView.Items  
 
            newValues = New Hashtable  
            gdi.OwnerTableView.ExtractValuesFromItem(newValues, gdi)  
            StyleTotal += Convert.ToInt32(newValues("RVD_QTY").ToString)  
 
        Next  
 
        'Update Style Revised Quantity  
        Dim parentStyle As GridDataItem = CType(parentSKU.OwnerTableView.ParentItem, GridDataItem)  
        Dim dtStyle As DataTable = ViewState(grdOR.ClientID)  
 
        Dim changedRows() As DataRow = dtStyle.Select("STYL_SEQ_NUM = " & SSN)  
 
        changedRows(0).BeginEdit()  
 
        Try  
 
            changedRows(0)("RVD_QTY") = StyleTotal  
            changedRows(0).EndEdit()  
 
            ViewState(parentStyle.OwnerTableView.ClientID) = dtStyle  
            ViewState("grdOR_DataSource_Style") = parentStyle.OwnerTableView.ClientID  
 
            parentStyle("RVD_QTY").Text = StyleTotal 
 
        Catch ex As Exception  
 
            changedRows(0).CancelEdit()  
            'Label1Label1.Text = Label1.Text & "Unable to update Orders. Reason: " & ex.Message  
            Canceled = True 
 
        End Try  
 
    End Sub  
 
    Private Sub UpdateSKULevel(ByRef parentSKU As GridDataItem, ByVal MSC As String, ByVal StoreTotal As Integer, ByRef Canceled As Boolean)  
 
        'Update SKU Revised Quantity  
        Dim dtSKU As DataTable = ViewState(parentSKU.OwnerTableView.ClientID)  
        Dim changedRows() As DataRow = dtSKU.Select("MSC_MDSE_NUM = " & MSC)  
 
        Dim newValues As Hashtable = New Hashtable  
 
        parentSKU.OwnerTableView.ExtractValuesFromItem(newValues, parentSKU)  
        changedRows(0).BeginEdit()  
 
        Try  
 
            'For Each entry As DictionaryEntry In newValues  
            changedRows(0)("RVD_QTY") = StoreTotal  
            'Next  
 
            changedRows(0).EndEdit()  
 
            ViewState(parentSKU.OwnerTableView.ClientID) = dtSKU  
            ViewState("grdOR_Event") = "STOREUPDATED"  
            ViewState("grdOR_DataSource_SKU") = parentSKU.OwnerTableView.ClientID  
 
            parentSKU("RVD_QTY").Text = StoreTotal 
 
        Catch ex As Exception  
 
            changedRows(0).CancelEdit()  
            'Label1Label1.Text = Label1.Text & "Unable to update Orders. Reason: " & ex.Message  
            Canceled = True 
 
        End Try  
 
    End Sub  
 
    Private Function PopulateStyleLevel() As DataTable  
 
        Dim dt As New DataTable  
        Dim c1 As New DataColumn  
        Dim c2 As New DataColumn  
        Dim c3 As New DataColumn  
        Dim c4 As New DataColumn  
 
        c1.ColumnName = "ITEM_DES" 
        c2.ColumnName = "VEN_STYL_NUM" 
        c3.ColumnName = "STYL_SEQ_NUM" 
        c4.ColumnName = "RVD_QTY" 
 
        dt.Columns.Add(c1)  
        dt.Columns.Add(c2)  
        dt.Columns.Add(c3)  
        dt.Columns.Add(c4)  
 
        Dim r As DataRow = dt.NewRow  
        r("ITEM_DES") = "Test Item 1"  
        r("VEN_STYL_NUM") = "TEST100"  
        r("STYL_SEQ_NUM") = "0123456789999"  
        r("RVD_QTY") = "20"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("ITEM_DES") = "Test Item 2"  
        r("VEN_STYL_NUM") = "TEST200"  
        r("STYL_SEQ_NUM") = "0123456788888"  
        r("RVD_QTY") = "10"  
        dt.Rows.Add(r)  
 
        'grdOR.DataSource = dt 
        'grdOR.DataSource = New DataView(dt)  
        'grdOR.DataBind()  
 
        Return dt  
 
    End Function  
 
    Private Function PopulateSKULevel(ByVal SSN As String) As DataTable  
 
        Dim dt As New DataTable  
        Dim c1 As New DataColumn  
        Dim c2 As New DataColumn  
        Dim c3 As New DataColumn  
        Dim c4 As New DataColumn  
        Dim c5 As New DataColumn  
 
        c1.ColumnName = "CLR_CDE" 
        c2.ColumnName = "CLR_DCD" 
        c3.ColumnName = "STYL_SEQ_NUM" 
        c4.ColumnName = "MSC_MDSE_NUM" 
        c5.ColumnName = "RVD_QTY" 
 
        dt.Columns.Add(c1)  
        dt.Columns.Add(c2)  
        dt.Columns.Add(c3)  
        dt.Columns.Add(c4)  
        dt.Columns.Add(c5)  
 
        Dim r As DataRow = dt.NewRow  
        r("CLR_CDE") = "100"  
        r("CLR_DCD") = "BLUE"  
        r("STYL_SEQ_NUM") = "0123456789999"  
        r("MSC_MDSE_NUM") = "0123456789999"  
        r("RVD_QTY") = "20"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("CLR_CDE") = "200"  
        r("CLR_DCD") = "RED"  
        r("STYL_SEQ_NUM") = "0123456788888"  
        r("MSC_MDSE_NUM") = "0123456788888"  
        r("RVD_QTY") = "10"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("CLR_CDE") = "300"  
        r("CLR_DCD") = "YELLOW"  
        r("STYL_SEQ_NUM") = "0123456788888"  
        r("MSC_MDSE_NUM") = "0123456787777"  
        r("RVD_QTY") = "40"  
        dt.Rows.Add(r)  
 
        Dim rows() As DataRow = dt.Select("STYL_SEQ_NUM = '" & SSN & "'")  
        Dim dtRet As DataTable = dt.Clone  
 
        For Each row As DataRow In rows  
 
            r = dtRet.NewRow  
            r(0) = row(0)  
            r(1) = row(1)  
            r(2) = row(2)  
            r(3) = row(3)  
            r(4) = row(4)  
            dtRet.Rows.Add(r)  
 
        Next  
 
        'dtRet.TableName = "SKU" 
 
        Return dtRet  
 
    End Function  
 
    Private Function PopulateStoreLevel(ByVal MSC As String) As DataTable  
 
        Dim dt As New DataTable  
        Dim c1 As New DataColumn  
        Dim c2 As New DataColumn  
        Dim c3 As New DataColumn  
        Dim c4 As New DataColumn  
 
        c1.ColumnName = "STOR_CDE" 
        c2.ColumnName = "ORDR_QTY" 
        c3.ColumnName = "MSC_MDSE_NUM" 
        c4.ColumnName = "RVD_QTY" 
 
        dt.Columns.Add(c1)  
        dt.Columns.Add(c2)  
        dt.Columns.Add(c3)  
        dt.Columns.Add(c4)  
 
        Dim r As DataRow = dt.NewRow  
        r("STOR_CDE") = "21"  
        r("ORDR_QTY") = "5"  
        r("MSC_MDSE_NUM") = "0123456789999"  
        r("RVD_QTY") = "20"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("STOR_CDE") = "54"  
        r("ORDR_QTY") = "7"  
        r("MSC_MDSE_NUM") = "0123456788888"  
        r("RVD_QTY") = "10"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("STOR_CDE") = "67"  
        r("ORDR_QTY") = "5"  
        r("MSC_MDSE_NUM") = "0123456787777"  
        r("RVD_QTY") = "40"  
        dt.Rows.Add(r)  
 
        Dim rows() As DataRow = dt.Select("MSC_MDSE_NUM = '" & MSC & "'")  
        Dim dtRet As DataTable = dt.Clone  
 
        For Each row As DataRow In rows  
 
            r = dtRet.NewRow  
            r(0) = row(0)  
            r(1) = row(1)  
            r(2) = row(2)  
            r(3) = row(3)  
            dtRet.Rows.Add(r)  
 
        Next  
 
        Return dtRet  
 
    End Function  
 
End Class  
 
0
Daniel
Telerik team
answered on 10 Oct 2008, 08:48 AM
Hello Rob,

Thank you for the provided example

If you comment out your try-catch block and start the website in normal (preferred) mode you will encounter an error concerning a missing column. You catch this exception which is thrown by RadGrid on Grid_PreRender event before the items are created.

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rob
Top achievements
Rank 1
answered on 13 Oct 2008, 07:15 PM
Daniel,

Thanks for pointing this out.  I believe I have corrected this issue and it was a datasource binding issue with master vs. detail tables.  I have made the fix and added some enhancements, but now I am running into a new issue. 

To reproduce the new issue, I load the page, expand a master level, expand 1 child so that the grandchildren are visible, then click the Zero button on the expanded master row.  When the page posts back, I try to make sure the rows that are already expanded stay expanded by setting the expanded property for the GridDataItem to true in the PreRender() event.  This works well for the child, but the grandchild does not stay expanded.  Is there a better approach or did I miss something here?

Here is the latest code for the page & code behind which is not perfect or the finished product:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Grid.aspx.vb" Inherits="Grid" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server">  
    <title>Untitled Page</title> 
    <style type="text/css">  
 
        .ZeroedOut  
        {  
            background-color:pink;  
        }  
          
        .ButtonN  
        {  
            height: 20px;  
            width: 50px;  
        }  
 
    </style> 
 
</head> 
<body> 
    <form id="form1" runat="server">  
    <div> 
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">  
        </telerik:RadScriptManager> 
        <telerik:RadGrid ID="grdOR" runat="server" AutoGenerateColumns="False" GridLines="None" Width="100%" Skin="WebBlue"  > 
            <MasterTableView DataKeyNames="STYL_SEQ_NUM,RVD_QTY" AllowAutomaticUpdates="True" NoDetailRecordsText="No SKU records to display." NoMasterRecordsText="No Style records to display." EditMode="InPlace">  
                <DetailTables> 
                    <telerik:GridTableView runat="server" DataKeyNames="STYL_SEQ_NUM,MSC_MDSE_NUM" NoDetailRecordsText="No Store records to display." > 
                        <RowIndicatorColumn> 
                            <HeaderStyle Width="20px" /> 
                        </RowIndicatorColumn> 
                        <ExpandCollapseColumn Visible="True">  
                            <HeaderStyle Width="20px" /> 
                        </ExpandCollapseColumn> 
                        <Columns> 
                            <telerik:GridBoundColumn DataField="CLR_CDE" HeaderText="Color Code" UniqueName="CLR_CDE" > 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="CLR_DCD" HeaderText="Color Desc" UniqueName="CLR_DCD" > 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="STYL_SEQ_NUM" HeaderText="SSN" UniqueName="STYL_SEQ_NUM" > 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="MSC_MDSE_NUM" HeaderText="MSC" UniqueName="MSC_MDSE_NUM" > 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="RVD_QTY" HeaderText="RVD_QTY" UniqueName="RVD_QTY" ItemStyle-Width="50px" HeaderStyle-Width="50px">  
                            </telerik:GridBoundColumn> 
                            <telerik:GridButtonColumn ButtonType="PushButton" Text="Zero" UniqueName="ZERO" CommandName="ZEROSKU" ItemStyle-Width="50px" HeaderStyle-Width="50px">  
                            </telerik:GridButtonColumn> 
                        </Columns> 
                        <DetailTables> 
                            <telerik:GridTableView runat="server" DataKeyNames="STOR_CDE,MSC_MDSE_NUM,RVD_QTY" EditMode="InPlace" AllowAutomaticUpdates="True" Width="100%" > 
                                <RowIndicatorColumn> 
                                    <HeaderStyle Width="20px" /> 
                                </RowIndicatorColumn> 
                                <ExpandCollapseColumn> 
                                    <HeaderStyle Width="20px" /> 
                                </ExpandCollapseColumn> 
                                <Columns> 
                                    <telerik:GridEditCommandColumn /> 
                                    <telerik:GridTemplateColumn DataField="STOR_CDE" HeaderText="Store" UniqueName="STOR_CDE" > 
                                        <ItemTemplate> 
                                            <%#Eval("STOR_CDE")%> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <asp:TextBox ID="txtStore" runat="server" Text='<%# Bind("STOR_CDE") %>' ReadOnly='<%# IIf(DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "False", "True") %>'/>  
                                        </EditItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                    <telerik:GridTemplateColumn DataField="ORDR_QTY" HeaderText="Quantity" UniqueName="ORDR_QTY">  
                                        <ItemTemplate> 
                                            <%#Eval("ORDR_QTY")%> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <asp:TextBox ID="txtOrderQty" runat="server" Text='<%# Bind("ORDR_QTY") %>' ReadOnly='<%# IIf(DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "False", "True") %>' /> 
                                        </EditItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                    <telerik:GridTemplateColumn DataField="MSC_MDSE_NUM" HeaderText="MSC" UniqueName="MSC_MDSE_NUM">  
                                        <ItemTemplate> 
                                            <%#Eval("MSC_MDSE_NUM")%> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <%# Eval("MSC_MDSE_NUM") %> 
                                        </EditItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                    <telerik:GridTemplateColumn DataField="RVD_QTY" HeaderText="RVD_QTY" UniqueName="RVD_QTY" ItemStyle-Width="110px" HeaderStyle-Width="110px">  
                                        <ItemTemplate> 
                                            <%#Eval("RVD_QTY")%> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <asp:TextBox ID="txtRevisedQuantity" runat="server" Text='<%# Bind("RVD_QTY") %>' /> 
                                        </EditItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                </Columns> 
                                <EditFormSettings> 
                                    <EditColumn UniqueName="EditCommandColumn1">  
                                    </EditColumn> 
                                </EditFormSettings> 
                            </telerik:GridTableView> 
                        </DetailTables>                         
                    </telerik:GridTableView> 
                </DetailTables> 
                <RowIndicatorColumn> 
                    <HeaderStyle Width="20px" /> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn Visible="True">  
                    <HeaderStyle Width="20px" /> 
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="ITEM_DES" HeaderText="Desc" UniqueName="ITEM_DES">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="VEN_STYL_NUM" HeaderText="Style" UniqueName="VEN_STYL_NUM">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="STYL_SEQ_NUM" HeaderText="SSN" UniqueName="STYL_SEQ_NUM">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridTemplateColumn DataField="RVD_QTY" HeaderText="RVD_QTY" UniqueName="RVD_QTY" ItemStyle-Width="50px" HeaderStyle-Width="50px">  
                        <ItemTemplate> 
                            <%#Eval("RVD_QTY")%> 
                        </ItemTemplate> 
                        <EditItemTemplate> 
                            <asp:TextBox ID="txtRevisedStyle" runat="server" Text='<%# Bind("RVD_QTY") %>' /> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridButtonColumn ButtonType="PushButton" Text="Zero" UniqueName="ZERO" CommandName="ZEROSTYLE" ItemStyle-Width="50px" HeaderStyle-Width="50px">  
                    </telerik:GridButtonColumn> 
                </Columns> 
                <EditFormSettings > 
                    <PopUpSettings Modal="True" /> 
                </EditFormSettings> 
            </MasterTableView> 
            <ClientSettings> 
                <Scrolling AllowScroll="True" UseStaticHeaders="True" /> 
            </ClientSettings> 
        </telerik:RadGrid> 
      
    </div> 
    </form> 
</body> 
</html> 
 

Imports System.Data  
Imports Telerik.Web.UI  
 
Partial Class Grid  
 
    Inherits System.Web.UI.Page  
 
    Protected Sub grdOR_DetailTableDataBind(ByVal source As Object, ByVal e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles grdOR.DetailTableDataBind  
 
        Dim parentItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)  
 
        If parentItem.Edit Then  
            Return  
        End If  
 
        Dim DataKeys() As String = parentItem.KeyValues.Split(",")  
 
        If e.DetailTableView.HierarchyIndex = "0" Then  
 
            If ViewState(e.DetailTableView.ClientID) Is Nothing Then  
 
                Dim Keys() As String = DataKeys(0).Split(":")  
                e.DetailTableView.DataSource = PopulateSKULevel(Keys(1).Replace("""", "").Replace("}", ""))  
                ee.DetailTableView.Name = e.DetailTableView.ClientID  
                ViewState(e.DetailTableView.ClientID) = e.DetailTableView.DataSource  
 
            Else  
 
                e.DetailTableView.DataSource = ViewState(e.DetailTableView.ClientID)  
 
            End If  
 
        ElseIf e.DetailTableView.HierarchyIndex.Substring(0, 2) = "0_" Then  
 
            If ViewState(e.DetailTableView.ClientID) Is Nothing Then  
 
                Dim Keys() As String = DataKeys(1).Split(":")  
                e.DetailTableView.DataSource = PopulateStoreLevel(Keys(1).Replace("""", "").Replace("}", ""))  
                ee.DetailTableView.Name = e.DetailTableView.ClientID  
                ViewState(e.DetailTableView.ClientID) = e.DetailTableView.DataSource  
 
            Else  
 
                e.DetailTableView.DataSource = ViewState(e.DetailTableView.ClientID)  
 
                If ViewState("grdOR_Event") = "STOREUPDATED" Then  
 
                    Dim parentSKU As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)  
                    Dim parentStyle As GridDataItem = CType(parentSKU.OwnerTableView.ParentItem, GridDataItem)  
 
                    parentSKU.OwnerTableView.DataSource = ViewState(ViewState("grdOR_DataSource_SKU").ToString)  
                    parentStyle.OwnerTableView.DataSource = ViewState(ViewState("grdOR_DataSource_Style").ToString)  
 
                    ViewState("grdOR_Event") = ""  
                    ViewState("grdOR_DataSource_SKU") = ""  
                    ViewState("grdOR_DataSource_Style") = ""  
 
                End If  
 
            End If  
 
        End If  
 
    End Sub  
 
    Protected Sub grdOR_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdOR.ItemCommand  
 
        Select Case e.CommandName  
 
            Case "ZEROSTYLE"  
 
                'Button at Style level clicked  
                Dim StyleItem As GridDataItem = CType(e.Item, GridDataItem)  
                Dim dtStyle As DataTable = ViewState(grdOR.ClientID)  
                Dim SSN As String = StyleItem.OwnerTableView.DataKeyValues(StyleItem.ItemIndex)("STYL_SEQ_NUM").ToString  
                Dim Revised As String = StyleItem.OwnerTableView.DataKeyValues(StyleItem.ItemIndex)("RVD_QTY").ToString  
 
                ViewState("grdOR_BindLevel") = "STYLE"  
 
                If StyleItem.Expanded Then  
 
                    ViewState("grdOR_Expand") = StyleItem.ItemIndex  
 
                    For Each SKUItem As GridDataItem In StyleItem.ChildItem.NestedTableViews(0).Items  
 
                        If SKUItem.Expanded Then  
 
                            ViewState("grdOR_Expand") = StyleItem.ItemIndex & "," & SKUItem.ItemIndex  
 
                            For Each StoreItem As GridDataItem In SKUItem.ChildItem.NestedTableViews(0).Items  
 
                                Dim dtStore As DataTable = ViewState(StoreItem.OwnerTableView.Name)  
                                Dim MSC As String = SKUItem.OwnerTableView.DataKeyValues(SKUItem.ItemIndex)("MSC_MDSE_NUM").ToString  
                                ZeroOutRow(StoreItem, dtStore, "MSC", MSC, False, e.Canceled)  
 
                            Next  
 
                        End If  
 
                        Dim dtSKU As DataTable = ViewState(SKUItem.OwnerTableView.Name)  
 
                        ZeroOutRow(SKUItem, dtSKU, "SSN", SSN, False, e.Canceled)  
 
                    Next  
 
                Else  
 
                    ViewState("grdOR_Expand") = Nothing  
                    ViewState("grdOR_MasterRow") = StyleItem.ItemIndex  
 
                End If  
 
                ZeroOutRow(StyleItem, dtStyle, "SSN", SSN, True, e.Canceled)  
 
            Case "ZEROSKU"  
 
                'Button at SKU level clicked  
                Dim SKUItem As GridDataItem = CType(e.Item, GridDataItem)  
                Dim StyleItem As GridDataItem = CType(e.Item.OwnerTableView.ParentItem, GridDataItem)  
                Dim dtSKU As DataTable = ViewState(SKUItem.OwnerTableView.Name)  
                Dim dtStyle As DataTable = ViewState(grdOR.ClientID)  
                Dim SSN As String = SKUItem.OwnerTableView.DataKeyValues(SKUItem.ItemIndex)("STYL_SEQ_NUM").ToString  
                Dim MSC As String = SKUItem.OwnerTableView.DataKeyValues(SKUItem.ItemIndex)("MSC_MDSE_NUM").ToString  
 
                ViewState("grdOR_BindLevel") = "SKU"  
 
                If SKUItem.Expanded Then  
 
                    For i As Integer = 0 To SKUItem.ChildItem.NestedTableViews(0).Items.Count - 1  
 
                        Dim StoreItem As GridDataItem = SKUItem.ChildItem.NestedTableViews(0).Items(i)  
                        Dim dtStore As DataTable = ViewState(StoreItem.OwnerTableView.Name)  
                        ZeroOutSKURow(StoreItem, dtStore, "MSC", MSC, e.Canceled)  
 
                    Next  
 
                    ViewState("grdOR_Expand") = StyleItem.ItemIndex & "," & SKUItem.ItemIndex  
 
                    'Get Revised Quantity Total for Stores  
 
                    '*** Get Store Total working!!!  ***  
                    Dim StoreTotal As Integer = CalculateStoreTotal(CType(ViewState(SKUItem.ChildItem.NestedTableViews(0).Name), DataTable))  
                    Dim parentSKU As GridDataItem = CType(e.Item.OwnerTableView.ParentItem, GridDataItem)  
 
                    Dim SKUTotal As Integer = CalculateStoreTotal(CType(ViewState(SKUItem.OwnerTableView.Name), DataTable))  
 
                    UpdateSKULevel(parentSKU, MSC, StoreTotal, e.Canceled)  
                    UpdateStyleLevel(parentSKU, e.Canceled)  
 
                Else  
 
                    ZeroOutSKURow(SKUItem, dtSKU, "MSC", MSC, e.Canceled)  
                    'ZeroOutSKURow(StyleItem, dtStyle, "SSN", SSN, e.Canceled)  
 
 
                    ViewState("grdOR_Expand") = StyleItem.ItemIndex  
                    ViewState("grdOR_MasterRow") = SKUItem.ItemIndex  
 
                End If  
 
                'ZeroOutRow(StyleItem, dtStyle, "SSN", SSN, e.Canceled)  
 
 
        End Select  
 
    End Sub  
 
    Public Sub ZeroOutRow(ByRef RowItem As GridDataItem, ByRef dt As DataTable, ByVal Field As String, ByVal FieldValue As String, ByVal IsMaster As Boolean, ByRef Canceled As Boolean)  
 
        Dim strField As String = "" 
 
        Select Case Field  
 
            Case "SSN"  
 
                strField = "STYL_SEQ_NUM" 
 
            Case "MSC"  
 
                strField = "MSC_MDSE_NUM" 
 
        End Select  
 
 
        Dim changedRows() As DataRow = dt.Select(strField & " = " & FieldValue)  
 
        If (changedRows.Length < 0) Then  
            'MeMe.Label1.Text = Me.Label1.Text + "Unable to locate the Order for updating."  
            Canceled = True 
            Return  
        End If  
 
        Try  
 
            For i As Integer = 0 To changedRows.Length - 1  
 
                changedRows(i).BeginEdit()  
                changedRows(i)("RVD_QTY") = 0  
                changedRows(i).EndEdit()  
                changedRows(i).AcceptChanges()  
 
            Next  
 
            If IsMaster Then  
 
                grdOR.DataSource = dt 
                grdOR.Rebind()  
 
                'Else  
 
                '    RowItem.OwnerTableView.DataSource = dt 
                '    RowItem.OwnerTableView.Rebind()  
 
            End If  
 
            ViewState(RowItem.OwnerTableView.Name) = dt  
 
        Catch ex As Exception  
 
            changedRows(0).CancelEdit()  
            'Label1Label1.Text = Label1.Text & "Unable to update Orders. Reason: " & ex.Message  
            Canceled = True 
 
        End Try  
 
    End Sub  
 
    Public Sub ZeroOutSKURow(ByRef RowItem As GridDataItem, ByRef dt As DataTable, ByVal Field As String, ByVal FieldValue As String, ByRef Canceled As Boolean)  
 
        Dim strField As String = "" 
 
        Select Case Field  
 
            Case "SSN"  
 
                strField = "STYL_SEQ_NUM" 
 
            Case "MSC"  
 
                strField = "MSC_MDSE_NUM" 
 
        End Select  
 
 
        Dim changedRows() As DataRow = dt.Select(strField & " = " & FieldValue)  
 
        If (changedRows.Length < 0) Then  
            'MeMe.Label1.Text = Me.Label1.Text + "Unable to locate the Order for updating."  
            Canceled = True 
            Return  
        End If  
 
        Try  
 
            For i As Integer = 0 To changedRows.Length - 1  
 
                changedRows(i).BeginEdit()  
                changedRows(i)("RVD_QTY") = 0  
                changedRows(i).EndEdit()  
                changedRows(i).AcceptChanges()  
 
            Next  
 
            RowItem.OwnerTableView.DataSource = dt 
            RowItem.OwnerTableView.Rebind()  
 
            ViewState(RowItem.OwnerTableView.Name) = dt  
 
        Catch ex As Exception  
 
            changedRows(0).CancelEdit()  
            'Label1Label1.Text = Label1.Text & "Unable to update Orders. Reason: " & ex.Message  
            Canceled = True 
 
        End Try  
 
    End Sub  
 
    Protected Sub grdOR_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grdOR.ItemDataBound  
 
        If TypeOf e.Item Is GridDataItem Then  
 
            Dim dataItem As GridDataItem = DirectCast(e.Item, GridDataItem)  
            Dim Keys() As String = dataItem.ItemIndexHierarchical.Split(":")  
 
            If Keys.Length < 3 Then  
 
                TryCast(dataItem("Zero").Controls(0), Button).CssClass = "ButtonN" 
 
            End If  
 
        End If  
 
    End Sub  
 
    Protected Sub grdOR_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdOR.NeedDataSource  
 
        grdOR.DataSource = PopulateStyleLevel()  
        grdORgrdOR.MasterTableView.Name = grdOR.ClientID  
        ViewState(grdOR.ClientID) = grdOR.DataSource  
 
    End Sub  
 
    Protected Sub grdOR_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdOR.PreRender  
 
        Dim dgiStyle As GridDataItem = Nothing 
 
        If Not ViewState("grdOR_BindLevel") Is Nothing Then  
 
            Select Case ViewState("grdOR_BindLevel").ToString  
 
                Case "STYLE"  
 
                    If Not ViewState("grdOR_Expand") Is Nothing Then  
 
                        Dim dgiSKU As GridDataItem = Nothing 
                        Dim Keys() As String = ViewState("grdOR_Expand").ToString.Split(",")  
 
                        For i As Integer = 0 To Keys.Length - 1  
 
                            Select Case i  
 
                                Case 0  
 
                                    dgiStyle = grdOR.MasterTableView.Items(Convert.ToInt32(Keys(i)))  
                                    dgiStyle.Expanded = True 
                                    dgiStyle.CssClass = "ZeroedOut" 
 
                                    For Each SKUItem As GridDataItem In dgiStyle.ChildItem.NestedTableViews(0).Items  
 
                                        SKUItem.CssClass = "ZeroedOut" 
 
                                    Next  
 
                                Case 1  
 
                                    dgiSKU = dgiStyle.ChildItem.OwnerTableView.Items(Convert.ToInt32(Keys(i)))  
                                    dgiSKU.Expanded = True 
 
                                    For Each StoreItem As GridDataItem In dgiSKU.ChildItem.NestedTableViews(0).Items  
 
                                        StoreItem.CssClass = "ZeroedOut" 
 
                                    Next  
 
                            End Select  
 
                        Next  
 
                        ViewState("grdOR_Expand") = Nothing  
 
                    ElseIf Not ViewState("grdOR_MasterRow") Is Nothing Then  
 
                        dgiStyle = grdOR.MasterTableView.Items(Convert.ToInt32(ViewState("grdOR_MasterRow").ToString))  
                        dgiStyle.CssClass = "ZeroedOut" 
                        ViewState("grdOR_MasterRow") = Nothing  
 
                    End If  
 
                Case "SKU"  
 
                    If Not ViewState("grdOR_Expand") Is Nothing Then  
 
                        Dim dgiSKU As GridDataItem = Nothing 
                        Dim Keys() As String = ViewState("grdOR_Expand").ToString.Split(",")  
 
                        For i As Integer = 0 To Keys.Length - 1  
 
                            Select Case i  
 
                                Case 0  
 
                                    dgiStyle = grdOR.MasterTableView.Items(Convert.ToInt32(Keys(i)))  
                                    dgiStyle.Expanded = True 
                                    dgiStyle.CssClass = "ZeroedOut" 
 
                                    If Keys.Length > 1 Then  
 
                                        dgiSKU = dgiStyle.ChildItem.NestedTableViews(0).Items(Convert.ToInt32(Keys(1)))  
                                        dgiSKU.CssClass = "ZeroedOut" 
 
                                    End If  
 
                                Case 1  
 
                                    dgiSKU = dgiStyle.ChildItem.NestedTableViews(0).Items(Convert.ToInt32(Keys(1)))  
                                    dgiSKU.Expanded = True 
 
                                    For Each StoreItem As GridDataItem In dgiSKU.ChildItem.NestedTableViews(0).Items  
 
                                        StoreItem.CssClass = "ZeroedOut" 
 
                                    Next  
 
                            End Select  
 
                        Next  
 
                        ViewState("grdOR_Expand") = Nothing  
                        ViewState("grdOR_MasterRow") = Nothing  
 
                    ElseIf Not ViewState("grdOR_MasterRow") Is Nothing Then  
 
                        dgiStyle = grdOR.MasterTableView.Items(Convert.ToInt32(ViewState("grdOR_MasterRow").ToString))  
                        dgiStyle.CssClass = "ZeroedOut" 
                        ViewState("grdOR_MasterRow") = Nothing  
 
                    End If  
 
            End Select  
 
        End If  
 
    End Sub  
 
    Protected Sub grdOR_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdOR.UpdateCommand  
 
        Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)  
        Dim dtStore As DataTable = ViewState(editedItem.OwnerTableView.Name)  
        Dim store As String = editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("STOR_CDE").ToString  
        'Dim store As String = editedItem("STOR_CDE").Text  
        Dim MSC As String = editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("MSC_MDSE_NUM").ToString  
        'Dim MSC As String = editedItem("MSC_MDSE_NUM").Text  
        Dim changedRows() As DataRow = dtStore.Select("STOR_CDE = " & store)  
 
        If (changedRows.Length <> 1) Then  
            'MeMe.Label1.Text = Me.Label1.Text + "Unable to locate the Order for updating."  
            e.Canceled = True 
            Return  
        End If  
 
        'Update new values  
        Dim newValues As Hashtable = New Hashtable  
 
        'The GridTableView will fill the values from all editable columns in the hash  
        editedItem.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)  
        changedRows(0).BeginEdit()  
 
        Try  
 
            For Each entry As DictionaryEntry In newValues  
                changedRows(0)(CType(entry.Key, String)) = entry.Value  
            Next  
 
            changedRows(0).EndEdit()  
 
            ViewState(editedItem.OwnerTableView.Name) = dtStore  
 
        Catch ex As Exception  
 
            changedRows(0).CancelEdit()  
            'Label1Label1.Text = Label1.Text & "Unable to update Orders. Reason: " & ex.Message  
            e.Canceled = True 
 
        End Try  
 
        'Get Revised Quantity Total for Stores  
        Dim StoreTotal As Integer = CalculateStoreTotal(dtStore) 'CalculateStoreTotal(e.Item.OwnerTableView.Items)  
        Dim parentSKU As GridDataItem = CType(e.Item.OwnerTableView.ParentItem, GridDataItem)  
 
        UpdateSKULevel(parentSKU, MSC, StoreTotal, e.Canceled)  
        UpdateStyleLevel(parentSKU, e.Canceled)  
 
    End Sub  
 
    Private Function CalculateStoreTotal(ByVal dt As DataTable) As Integer  
 
        Dim newValues As New Hashtable  
        Dim Total As Integer = 0 
 
        For Each r As DataRow In dt.Rows  
 
            Total += Convert.ToInt32(r("RVD_QTY"))  
 
        Next  
 
        Return Total  
 
    End Function  
 
    Private Function CalculateStoreTotal(ByVal StoreItems As GridDataItemCollection) As Integer  
 
        Dim newValues As New Hashtable  
        Dim Total As Integer = 0 
 
        For Each gdi As GridDataItem In StoreItems  
 
            newValues = New Hashtable  
            gdi.OwnerTableView.ExtractValuesFromItem(newValues, gdi)  
 
            If newValues.Count > 0 Then  
 
                Total += Convert.ToInt32(newValues("RVD_QTY").ToString)  
 
            End If  
 
        Next  
 
        Return Total  
 
    End Function  
 
    Private Sub UpdateStyleLevel(ByVal parentSKU As GridDataItem, ByRef Canceled As Boolean)  
 
        'Get Revised Quantity Total for Stores  
        Dim StyleTotal As Integer = 0 
        Dim newValues As Hashtable = New Hashtable  
        Dim SSN As String = parentSKU("STYL_SEQ_NUM").Text  
 
        For Each gdi As GridDataItem In parentSKU.OwnerTableView.Items  
 
            newValues = New Hashtable  
            gdi.OwnerTableView.ExtractValuesFromItem(newValues, gdi)  
            StyleTotal += Convert.ToInt32(newValues("RVD_QTY").ToString)  
 
        Next  
 
        'Update Style Revised Quantity  
        Dim parentStyle As GridDataItem = CType(parentSKU.OwnerTableView.ParentItem, GridDataItem)  
        Dim dtStyle As DataTable = ViewState(grdOR.ClientID)  
 
        Dim changedRows() As DataRow = dtStyle.Select("STYL_SEQ_NUM = " & SSN)  
 
        changedRows(0).BeginEdit()  
 
        Try  
 
            changedRows(0)("RVD_QTY") = StyleTotal  
            changedRows(0).EndEdit()  
 
            ViewState(parentStyle.OwnerTableView.Name) = dtStyle  
            ViewState("grdOR_DataSource_Style") = parentStyle.OwnerTableView.Name  
 
            parentStyle("RVD_QTY").Text = StyleTotal 
 
        Catch ex As Exception  
 
            changedRows(0).CancelEdit()  
            'Label1Label1.Text = Label1.Text & "Unable to update Orders. Reason: " & ex.Message  
            Canceled = True 
 
        End Try  
 
    End Sub  
 
    Private Sub UpdateSKULevel(ByRef parentSKU As GridDataItem, ByVal MSC As String, ByVal StoreTotal As Integer, ByRef Canceled As Boolean)  
 
        'Update SKU Revised Quantity  
        Dim dtSKU As DataTable = ViewState(parentSKU.OwnerTableView.Name)  
        Dim changedRows() As DataRow = dtSKU.Select("MSC_MDSE_NUM = " & MSC)  
 
        Dim newValues As Hashtable = New Hashtable  
 
        parentSKU.OwnerTableView.ExtractValuesFromItem(newValues, parentSKU)  
        changedRows(0).BeginEdit()  
 
        Try  
 
            'For Each entry As DictionaryEntry In newValues  
            changedRows(0)("RVD_QTY") = StoreTotal  
            'Next  
 
            changedRows(0).EndEdit()  
 
            ViewState(parentSKU.OwnerTableView.Name) = dtSKU  
            ViewState("grdOR_Event") = "STOREUPDATED"  
            ViewState("grdOR_DataSource_SKU") = parentSKU.OwnerTableView.Name  
 
            parentSKU("RVD_QTY").Text = StoreTotal 
 
        Catch ex As Exception  
 
            changedRows(0).CancelEdit()  
            'Label1Label1.Text = Label1.Text & "Unable to update Orders. Reason: " & ex.Message  
            Canceled = True 
 
        End Try  
 
    End Sub  
 
    Private Function PopulateStyleLevel() As DataTable  
 
        Dim dt As New DataTable  
        Dim c1 As New DataColumn  
        Dim c2 As New DataColumn  
        Dim c3 As New DataColumn  
        Dim c4 As New DataColumn  
 
        c1.ColumnName = "ITEM_DES" 
        c2.ColumnName = "VEN_STYL_NUM" 
        c3.ColumnName = "STYL_SEQ_NUM" 
        c4.ColumnName = "RVD_QTY" 
 
        dt.Columns.Add(c1)  
        dt.Columns.Add(c2)  
        dt.Columns.Add(c3)  
        dt.Columns.Add(c4)  
 
        Dim r As DataRow = dt.NewRow  
        r("ITEM_DES") = "Test Item 1"  
        r("VEN_STYL_NUM") = "TEST100"  
        r("STYL_SEQ_NUM") = "0123456789999"  
        r("RVD_QTY") = "20"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("ITEM_DES") = "Test Item 2"  
        r("VEN_STYL_NUM") = "TEST200"  
        r("STYL_SEQ_NUM") = "0123456788888"  
        r("RVD_QTY") = "10"  
        dt.Rows.Add(r)  
 
        'grdOR.DataSource = dt 
        'grdOR.DataSource = New DataView(dt)  
        'grdOR.DataBind()  
 
        Return dt  
 
    End Function  
 
    Private Function PopulateSKULevel(ByVal SSN As String) As DataTable  
 
        Dim dt As New DataTable  
        Dim c1 As New DataColumn  
        Dim c2 As New DataColumn  
        Dim c3 As New DataColumn  
        Dim c4 As New DataColumn  
        Dim c5 As New DataColumn  
 
        c1.ColumnName = "CLR_CDE" 
        c2.ColumnName = "CLR_DCD" 
        c3.ColumnName = "STYL_SEQ_NUM" 
        c4.ColumnName = "MSC_MDSE_NUM" 
        c5.ColumnName = "RVD_QTY" 
 
        dt.Columns.Add(c1)  
        dt.Columns.Add(c2)  
        dt.Columns.Add(c3)  
        dt.Columns.Add(c4)  
        dt.Columns.Add(c5)  
 
        Dim r As DataRow = dt.NewRow  
        r("CLR_CDE") = "100"  
        r("CLR_DCD") = "BLUE"  
        r("STYL_SEQ_NUM") = "0123456789999"  
        r("MSC_MDSE_NUM") = "0123456789999"  
        r("RVD_QTY") = "20"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("CLR_CDE") = "200"  
        r("CLR_DCD") = "RED"  
        r("STYL_SEQ_NUM") = "0123456788888"  
        r("MSC_MDSE_NUM") = "0123456788888"  
        r("RVD_QTY") = "10"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("CLR_CDE") = "300"  
        r("CLR_DCD") = "YELLOW"  
        r("STYL_SEQ_NUM") = "0123456788888"  
        r("MSC_MDSE_NUM") = "0123456787777"  
        r("RVD_QTY") = "40"  
        dt.Rows.Add(r)  
 
        Dim rows() As DataRow = dt.Select("STYL_SEQ_NUM = '" & SSN & "'")  
        Dim dtRet As DataTable = dt.Clone  
 
        For Each row As DataRow In rows  
 
            r = dtRet.NewRow  
            r(0) = row(0)  
            r(1) = row(1)  
            r(2) = row(2)  
            r(3) = row(3)  
            r(4) = row(4)  
            dtRet.Rows.Add(r)  
 
        Next  
 
        'dtRet.TableName = "SKU" 
 
        Return dtRet  
 
    End Function  
 
    Private Function PopulateStoreLevel(ByVal MSC As String) As DataTable  
 
        Dim dt As New DataTable  
        Dim c1 As New DataColumn  
        Dim c2 As New DataColumn  
        Dim c3 As New DataColumn  
        Dim c4 As New DataColumn  
 
        c1.ColumnName = "STOR_CDE" 
        c2.ColumnName = "ORDR_QTY" 
        c3.ColumnName = "MSC_MDSE_NUM" 
        c4.ColumnName = "RVD_QTY" 
 
        dt.Columns.Add(c1)  
        dt.Columns.Add(c2)  
        dt.Columns.Add(c3)  
        dt.Columns.Add(c4)  
 
        Dim r As DataRow = dt.NewRow  
        r("STOR_CDE") = "21"  
        r("ORDR_QTY") = "5"  
        r("MSC_MDSE_NUM") = "0123456789999"  
        r("RVD_QTY") = "20"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("STOR_CDE") = "54"  
        r("ORDR_QTY") = "7"  
        r("MSC_MDSE_NUM") = "0123456788888"  
        r("RVD_QTY") = "10"  
        dt.Rows.Add(r)  
 
        r = dt.NewRow  
        r("STOR_CDE") = "67"  
        r("ORDR_QTY") = "5"  
        r("MSC_MDSE_NUM") = "0123456787777"  
        r("RVD_QTY") = "40"  
        dt.Rows.Add(r)  
 
        Dim rows() As DataRow = dt.Select("MSC_MDSE_NUM = '" & MSC & "'")  
        Dim dtRet As DataTable = dt.Clone  
 
        For Each row As DataRow In rows  
 
            r = dtRet.NewRow  
            r(0) = row(0)  
            r(1) = row(1)  
            r(2) = row(2)  
            r(3) = row(3)  
            dtRet.Rows.Add(r)  
 
        Next  
 
        Return dtRet  
 
    End Function 
0
Daniel
Telerik team
answered on 16 Oct 2008, 05:02 PM
Hello Rob,

I'm afraid it's hard to determine the concrete reason for this behavior. Please attach a working project in order to be tested locally. You can submit a regular support ticket which will allow us to give you more to-the-point answer.
 
Best regards,
Daniel
the Telerik team

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