Update Total field in parent grid when row added to detail grid

11 posts, 0 answers
  1. Simon Channell
    Simon Channell avatar
    25 posts
    Member since:
    Mar 2004

    Posted 31 Jul 2017 Link to this post

    I have a hierarchical grid with two levels.

    The Parent grid rows contain a student's ID (student_reference) , Name, course id (Expr1) , class code (Group) and the total number of notes created for that student.

    The Detail rows contain the note details ( Student ref ,Note Type,Subject, Note content, Date created and ID of the member of staff who created the note)

    When a new note is created it is added to the detail grid and the notes database table.

    I now need to know how to update the parent row with the total number of notes when a new note is added. I need to also update the associated dataset which is a property of the User control containing the grid..

    Rows can only be added to the notes database table. They cannot be deleted or updated.

    Below is the definition of the grid from the ascx file.

    <telerik:radgrid runat="server" id="RadGridClassOverview" showfooter="false" width="100%" enableajax="true" showstatusbar="true"
        allowaddingrecords="false" autogeneratecolumns="false" allowsorting="false" allowpagesizeselection="False"
        onitemdatabound="OnItemDataBoundHandler" ondetailtabledatabind="RadGridOverview_DetailTableDataBind"
        allowpaging="True" skin="Bootstrap" pagesize="30" onneeddatasource="RadGridClassOverview_NeedDataSource"
        OnItemCommand="RadGridClassOverview_ItemCommand" onprerender="RadGridClassOverview_PreRender" cssclass="table table-striped">
        <PagerStyle Mode="NextPrev" Position="Bottom" PageSizeControlType="RadComboBox"></PagerStyle>
        <MasterTableView AutoGenerateColumns="false" DataKeyNames="Student_reference, Expr1, Group" TableLayout="Auto" HierarchyLoadMode="ServerOnDemand" EnableHierarchyExpandAll="true" HierarchyDefaultExpanded="false" EditMode="EditForms" RetainExpandStateOnRebind="False">
                        <DetailTables>
                <telerik:GridTableView AutoGenerateColumns="false" DataKeyNames="note_ref" Width="100%"   EnableHierarchyExpandAll="true" CommandItemDisplay="Top" EditMode="EditForms" name="GridNotes">
                    <CommandItemSettings AddNewRecordText="Create New Note" />
                         <NoRecordsTemplate>
          <div>
            There are no notes to display</div>
        </NoRecordsTemplate>
                     <Columns>
                        <telerik:GridButtonColumn CommandName="Edit" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" Text="View" UniqueName="View">
                        </telerik:GridButtonColumn>
                         <telerik:GridBoundColumn DataField="student_id"  HeaderText="note_ref" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" UniqueName="Student_ref" Display="false" runat="server" ReadOnly="true">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="note_ref" DataType="System.Int32" HeaderText="note_ref" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" UniqueName="note_ref" Display="false" runat="server" ReadOnly="true">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="course" HeaderText="code" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" UniqueName="course" Display="false" runat="server" ItemStyle-Width="10px" HeaderStyle-Width="10px">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="course_name" HeaderText="Subject" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" UniqueName="qualname" HeaderStyle-Width="200px" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Left" ItemStyle-Wrap="true">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="noteType" HeaderText="Type" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" UniqueName="noteType" Display="false">
                        </telerik:GridBoundColumn>
                          <telerik:GridBoundColumn DataField="typeDesc" HeaderText="Note Type" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" UniqueName="typeDesc" ItemStyle-HorizontalAlign="Left" ItemStyle-Wrap="true">
                        </telerik:GridBoundColumn>
                        <telerik:GridDateTimeColumn DataField="created" HeaderText="Created" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" UniqueName="created" DataFormatString="{0:dd/MM/yyyy}">
                        </telerik:GridDateTimeColumn>
                        <telerik:GridBoundColumn DataField="created_by" HeaderText="Created" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" UniqueName="created_by" Display="false" ItemStyle-HorizontalAlign="Left">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="note_content" HeaderText="Created" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" UniqueName="note_content" Display="false" ItemStyle-HorizontalAlign="Center">
                        </telerik:GridBoundColumn>
                    </Columns>
                      <EditFormSettings EditFormType="Template">
                <EditColumn UniqueName="View">
                </EditColumn>
                    <FormTemplate>
                    <table style="width: 85%" border="1" >
                         <asp:Panel ID="pnlAdd" runat="server" Visible="false">
                        <tr style="border:solid">
                                    <td colspan="2"><b>Create New Note</b><br /></td>
                      </tr>
                                  <tr>
                                    <td colspan="2">
                                        <asp:Label ID="lblqualcode" runat="server"  Text='' ></asp:Label>
                                        <br />
                                        Select Note Type<br />
                                            <telerik:RadComboBox ID="RadComboBoxNoteType" runat="server" EmptyMessage="Select NoteType"  CausesValidation="False"
                                                       HighlightTemplatedItems="true" Height="100px" Width="450px" DropDownWidth="450px"  EnableAutomaticLoadOnDemand="False" AutoPostBack="true"
                                                      AppendDataBoundItems="true" Visible="true"   CssClass="ComboBox" OnSelectedIndexChanged="RadComboBoxNoteType_SelectedIndexChanged">
                       </telerik:RadComboBox>
                                    </td>
                                </tr><tr><td colspan="2">Mindset habits<br />Please select the mindset habits to link to this note</td></tr>
                            <tr><td colspan="2">
                                <telerik:RadListBox ID="ListBoxhabits" runat="server" RenderMode="Lightweight" CheckBoxes="true">
                                <Items>
                               <telerik:RadListBoxItem Text=" Time" />
                              <telerik:RadListBoxItem Text=" Engagement" />
                              <telerik:RadListBoxItem Text=" Response" />
                              <telerik:RadListBoxItem Text=" Resources" />
                             <telerik:RadListBoxItem Text=" Organisation" />
                           </Items>
                         </telerik:RadListBox>
                                </td></tr>
                                    
                                       <tr>
                                    <td colspan="2">
                                        Enter Note content
                    <br />
                                      <asp:TextBox ID="txtNoteContent" runat="server" Text=""  CssClass="notecontent"  TextMode="MultiLine" BorderStyle="Solid" style="width:450px">
                                     </asp:TextBox><br />
                                       <br />
                         <br /><br />
                                      <asp:Label ID="lblMessage" runat="server"  Text=''></asp:Label>
                                        </td>
                                       </tr>
                                     <tr>
                                    <td  colspan="2">
                                             <asp:Button ID="btnSave" CausesValidation="False" Text="Save"
                                            runat="server"  AutoPostBack="True" CommandName="PerformInsert"></asp:Button>&nbsp;
                                        <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                            CommandName="Cancel"></asp:Button>
                                    </td>
                                </tr>
                                </asp:Panel>
                          <asp:Panel ID="Paneldetail" runat="server" Visible="false">
                        <tr>
                                    <td colspan="2">
                                        <b>Note Details</b><br />
                                    </td>
                        </tr>
                         <tr>
                                    <td colspan="2">
                                   <asp:Label ID="LabelDetails" runat="server"  Text='<%# Bind("note_content") %>'></asp:Label>
                            </td>
                        </tr>
                           </asp:Panel>
                    </table>
                </FormTemplate>
            </EditFormSettings>
                </telerik:GridTableView>
            </DetailTables>
            <Columns>
                 <telerik:GridBoundColumn DataField="Student_reference" HeaderText="Student ID" UniqueName="Student_reference" runat="server" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true">
                </telerik:GridBoundColumn>
                 <telerik:GridBoundColumn DataField="Expr1" HeaderText="class" UniqueName="Expr1" Display="false" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Left">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Group" HeaderText="class" UniqueName="Group" Display="false" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Left">
                </telerik:GridBoundColumn>
                <telerik:GridCalculatedColumn DataFields="p_forenames,p_surname" Expression='{0}+"&nbsp;" +{1}' HeaderText="Name" UniqueName="student_name" Display="True" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="250px">
                </telerik:GridCalculatedColumn>
                <telerik:GridBoundColumn DataField="p_surname" HeaderText="Name" UniqueName="p_surname" Display="false" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Left">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="p_forenames" HeaderText="Name" UniqueName="p_forenames" Display="False" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Left">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="attendance" HeaderText="Attendance" UniqueName="attendance" DataFormatString="{0:N0}%"  Display="True"  HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"  ItemStyle-Width="100px">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="spms_level" HeaderText="SPMS Level" UniqueName="spms_level" Display="True" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="150px">
                </telerik:GridBoundColumn>
                <telerik:GridButtonColumn DataTextField="total_notes" HeaderText="Total Notes" UniqueName="total_notes" runat="server" HeaderStyle-BackColor="#428bca" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" CommandName="ViewNotes" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"  ItemStyle-Width="100px"  HeaderStyle-Wrap="true">
                </telerik:GridButtonColumn>
            </Columns>
             <PagerStyle Mode="NextPrev" Position="Bottom" PageSizeControlType="RadComboBox"></PagerStyle>
               </MasterTableView>
              <ClientSettings EnablePostBackOnRowClick="true">
               <Selecting AllowRowSelect="true"></Selecting>
            </ClientSettings>
         <SelectedItemStyle BackColor="Gray" BorderColor="Blue" BorderStyle="Dashed" BorderWidth="1px" />
    </telerik:radgrid>

     

     

  2. Simon Channell
    Simon Channell avatar
    25 posts
    Member since:
    Mar 2004

    Posted 03 Aug 2017 Link to this post

    The attached screenshot shows the layout of my grid

    The total notes field in the Parent Grid now updates when a new note is created, but the dataset and grid is not being refreshed correctly as the total notes field goes blank when this Parent row is collapsed and a new Parent row is expanded

  3. Eyup
    Admin
    Eyup avatar
    3824 posts

    Posted 03 Aug 2017 Link to this post

    Hi Simon,

    To achieve this requirement you should Rebind() the entire grid when a note is added. And in order to preserve the collapsed state of the master rows, you can enable the following property, which is currently set to false:
    <MasterTableView ... RetainExpandStateOnRebind="true">

    I hope this will prove helpful.

    Regards,
    Eyup
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  4. Simon Channell
    Simon Channell avatar
    25 posts
    Member since:
    Mar 2004

    Posted 07 Aug 2017 in reply to Eyup Link to this post

    Please can you clarify when I should Rebind the grid

    A row is added to the detail grid when the PerformInsert Command is activated in the ItemCommand event handler 

    I am updating the row in the dataset for the parent grid after I have added the row to the student notes database table and the detail grid. I only need to rebind the parent grid.

  5. Eyup
    Admin
    Eyup avatar
    3824 posts

    Posted 10 Aug 2017 Link to this post

    Hello Simon,

    As you've stated: 
    "I am updating the row in the dataset for the parent grid after I have added the row to the student notes database table and the detail grid."

    I suppose you are doing this logic in the code-behind. After executing the database queries, you can call RadGridClassOverview.Rebind() method so that the Notes count will have the new updated values in the master rows.

    Regards,
    Eyup
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  6. Simon Channell
    Simon Channell avatar
    25 posts
    Member since:
    Mar 2004

    Posted 10 Aug 2017 in reply to Eyup Link to this post

    Is there a way to just update the parent grid?

    A new row is already being successfully added to the detail grid so I just need to rebind the parent after the row is added to the detail grid and its associated database table.

    Below is an outline of what happens when row is added (from Item Command subroutine)

    --------

    If e.CommandName.Contains("PerformInsert") Then

    Get values for new row from Edit form

    Add new row to database table

    -- next line updates total in Parent row

     e.Item.OwnerTableView.ParentItem.Item("total_notes").Text = totalnotes.ToString()

    --loop through rows in dataset for parent grid and update total  in dataset row for this student
    numrows = Me.currentclass.Tables(0).Rows.Count
                        'update dataset with correct number of notes for this student
                        For x = 0 To numrows - 1
                            If (Me.currentclass.Tables(0).Rows(x).item("Expr1") = qualcode) And Me.currentclass.Tables(0).Rows(x).item("Group") = classcode And (Me.currentclass.Tables(0).Rows(x).item("Student_reference") = studentcode) Then
                                Me.currentclass.Tables(0).Rows(x).item("total_notes") = totalnotes
                            End If
                        Next

    -- Rebind grid - this rebinds whole grid and currently gives error message which is due to the unnecessary rebind of detail grid
                        RadGridClassOverview.Rebind()

     

     

  7. Eyup
    Admin
    Eyup avatar
    3824 posts

    Posted 15 Aug 2017 Link to this post

    Hello Simon,

    You can visually set the cell text as you've done - ("total_notes").Text. But rebinding the parent grid also resets and rebinds the inner tables, too. 

    Regards,
    Eyup
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  8. Simon Channell
    Simon Channell avatar
    25 posts
    Member since:
    Mar 2004

    Posted 16 Aug 2017 in reply to Eyup Link to this post

    I get the following error message on Rebind

    Error Message: note_ref is neither a DataColumn nor a DataRelation for table Table.

    Note ref is a column in the detail table, but is not in the Parent grid

    Is this the reason behind the above error?

     

  9. Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 21 Aug 2017 Link to this post

    Hello,

    The error you are seeing is usually observed when a DataField set in RadGrid cannot be found in the data source. Therefore make sure that note_ref data field is present in the data source. Note that the same applies to the fields that are added to the DataKeyNames collection.

    Regards,
    Pavlina
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  10. Simon Channell
    Simon Channell avatar
    25 posts
    Member since:
    Mar 2004

    Posted 22 Aug 2017 Link to this post

    I have just made the note ref column visible to check it is being populated

    There is a value set when a new row is added, but I still get the error message on Rebind

    note_ref is neither a DataColumn nor a DataRelation for table Table.

  11. Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 24 Aug 2017 Link to this post

    Hi,

    Could you please isolate the problem you encounter in a sample runnable project and send it to us via support ticket? We will examine it right away and will get back to you with our findings.

    Thank you in advance! 

    Regards,
    Pavlina
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top