Folks using VS 2010 and Rad Grid controls for Ajax 2011 V.1
I am using hierarchical Master/Child Table. I would like to know how to get the row counts of detail table into a varible (say
int childRowcount) in following scenarios:
1) When Master record is selected (Without expanding)
2) After the update/insert in Child Table.
Thanks GC_0620
_____________
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) { if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "MasterTable") { GridDataItem dataItem = (GridDataItem)e.Item; // How to Get Child Table Row Count } if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "DetailTable") { GridDataItem childItem = (GridDataItem)e.Item; // How to Get the Child Table Row Count } } 7 Answers, 1 is accepted
0
Princy
Top achievements
Rank 2
answered on 03 May 2011, 08:00 AM
Hello,
In order to get the row count when master table row is selected, better approach is to go for client side.
Javascript:
Note: Set HierarchyLoadMode in MasterTableView as Client.
For second scenario, attach ItemCommand event and try the following snippet code. Hope this helps you.
C#:
Thanks,
Princy.
In order to get the row count when master table row is selected, better approach is to go for client side.
Javascript:
<script type="text/javascript"> function OnRowSelected(sender, args) { var detailTable = args.get_gridDataItem().get_nestedViews()[0]; alert(detailTable.get_dataItems().length); }</script>Note: Set HierarchyLoadMode in MasterTableView as Client.
For second scenario, attach ItemCommand event and try the following snippet code. Hope this helps you.
C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e){ if( (e.CommandName == RadGrid.UpdateCommandName) ||(e.CommandName == RadGrid.PerformInsertCommandName)) { if (e.Item.OwnerTableView.Name == "DetailTable1") { int i= e.Item.OwnerTableView.Items.Count; Response.Write(i); } }}Thanks,
Princy.
0
Sabaratnam
Top achievements
Rank 1
answered on 27 Feb 2013, 01:27 AM
Hi Princy,
I am a Telerik newbie.
I have a Master table and a Details Table underneath it within a RadGrid (using AJAX).
The user is able to Edit rows in-line (mode) everything (including Add New Record / Delete) seems to work fine.
Now I need to prevent the users from editing a row (i.e clicking on the pencil icon) if the Details Table for the row for which Edit action is attempted has no rows.
Ideally I would like to achieve this in JavaScript within a suitable event such as "OnCommand".
Please help..
I am a Telerik newbie.
I have a Master table and a Details Table underneath it within a RadGrid (using AJAX).
The user is able to Edit rows in-line (mode) everything (including Add New Record / Delete) seems to work fine.
Now I need to prevent the users from editing a row (i.e clicking on the pencil icon) if the Details Table for the row for which Edit action is attempted has no rows.
Ideally I would like to achieve this in JavaScript within a suitable event such as "OnCommand".
Please help..
0
Princy
Top achievements
Rank 2
answered on 27 Feb 2013, 04:49 AM
Hi,
Please check the following code snippet to prevent user from editing the Parent RadGrid's row if doesn't have Child row.
Javascript:
Thanks,
Princy.
Please check the following code snippet to prevent user from editing the Parent RadGrid's row if doesn't have Child row.
Javascript:
<script type="text/javascript"> function OnCommand(sender, eventArgs) { if (eventArgs.get_commandName() == 'Edit') { var editIndex = eventArgs.get_commandArgument(); var masterTable = sender.get_masterTableView(); var dataItem = masterTable.get_dataItems()[editIndex]; if (dataItem.get_nestedViews().length == 0) { eventArgs.set_cancel(true); alert("canceled"); } } }</script>Thanks,
Princy.
0
Sabaratnam
Top achievements
Rank 1
answered on 27 Feb 2013, 06:10 PM
Thanks a lot Princy for your timely help,
For some reason dataItem.get_nestedViews().length comes out as zero irrespective of wheather the DetailTable has any rows or not.
Please find below the relevant code bolcks copied from my bulky program.
I use LINQ-to-SQL to set datasources at server for both the master and detailTables and the detailTable uses
Hope you'd have a moment to glance through the code below and point me in the right direction.
Thanks a lot again for all your posts. They have helped me tremendously.
For some reason dataItem.get_nestedViews().length comes out as zero irrespective of wheather the DetailTable has any rows or not.
Please find below the relevant code bolcks copied from my bulky program.
I use LINQ-to-SQL to set datasources at server for both the master and detailTables and the detailTable uses
HierarchyLoadMode="Client" .Hope you'd have a moment to glance through the code below and point me in the right direction.
Thanks a lot again for all your posts. They have helped me tremendously.
<P> function RowCommand(sender, eventArgs) {</P><P> var editIndex = eventArgs.get_commandArgument();<BR> var masterTable = sender.get_masterTableView();<BR> var dataItem = masterTable.get_dataItems()[editIndex];<BR> if ((eventArgs.get_commandName() == "Edit") && (dataItem.get_nestedViews().length == 0)) {<BR> eventArgs.set_cancel(true);<BR> alert("canceled ");<BR> }<BR> }</P><P> <telerik:RadAjaxManager ID="RadAjaxManager1"runat="server" ><BR> <AjaxSettings><BR> <telerik:AjaxSettingAjaxControlID="cbPICPUserActivity"><BR> <UpdatedControls><BR> <telerik:AjaxUpdatedControlControlID="rgSampleData"></telerik:AjaxUpdatedControl><BR> </UpdatedControls></P><P> <UpdatedControls><BR> <telerik:AjaxUpdatedControl ControlID="rgSampleData"LoadingPanelID="RadAjaxLoadingPanel1"><BR> </telerik:AjaxUpdatedControl><BR> <telerik:AjaxUpdatedControlControlID="cbPICPUserActivity"></telerik:AjaxUpdatedControl><BR> </UpdatedControls><BR> </telerik:AjaxSetting><BR> <telerik:AjaxSettingAjaxControlID="cbPICPProcessType"><BR> <UpdatedControls><BR> <telerik:AjaxUpdatedControl ControlID="rgSampleData"LoadingPanelID="RadAjaxLoadingPanel1"><BR> </telerik:AjaxUpdatedControl><BR> <telerik:AjaxUpdatedControlControlID="cbPICPProcessType"></telerik:AjaxUpdatedControl><BR> </UpdatedControls><BR> </telerik:AjaxSetting><BR> </AjaxSettings><BR> </telerik:RadAjaxManager></P><P><BR> <telerik:RadFilter runat="server" ID="RadFilter1" Skin="Sunset"FilterContainerID="rgSampleData" ShowApplyButton="false" /></P><P> <telerik:RadGrid<BR> runat="server"<BR> ID="rgSampleData"<BR> Skin="Sunset"<BR> ShowFooter="true" <BR> AutoGenerateColumns="false"<BR> AllowAutomaticInserts="false" <BR> AllowAutomaticUpdates="false"<BR> AllowMultiRowSelection="false"<BR> AllowPaging="true"<BR> PageSize="15"<BR> EnableViewState="true" <BR> AllowFilteringByColumn="true"<BR> OnColumnCreated="rgSampleData_ColumnCreated"<BR> OnPreRender="rgSampleData_PreRender" <BR> OnNeedDataSource="rgSampleData_NeedDataSource"<BR> OnItemEvent="rgSampleData_ItemEvent"<BR> OnItemCommand="rgSampleData_ItemCommand"<BR> OnInsertCommand="rgSampleData_InsertCommand"<BR> OnSelectedIndexChanged="rgSampleData_SelectedIndexChanged"<BR> OnItemInserted="rgSampleData_ItemInserted"<BR> OnUpdateCommand="rgSampleData_UpdateCommand"<BR> OnItemDataBound="rgSampleData_ItemDataBound"<BR> OnItemCreated="rgSampleData_ItemCreated"<BR> OnDetailTableDataBind="rgSampleData_DetailTableDataBind"<BR> OnRowEditing="rgSampleData_RowEditing"<BR> <BR> OnItemUpdated="rgSampleData_ItemUpdated"</P><P> OnDeleteCommand="rgSampleData_DeleteCommand"<BR> ><BR> <PagerStyleMode="Slider"></PagerStyle><BR> <MasterTableViewIsFilterItemExpanded="true"<BR> Name="Master"<BR> Width="98%"<BR> datakeynames="PXQSampleId"<BR> bordercolor="LightGray" <BR> CommandItemDisplay="Top"<BR> editmode="InPlace"><BR> <CommandItemTemplate><BR> <telerik:RadToolBar runat="server" ID="RadToolBar1" CssClass="customToolBar"<BR> OnButtonClick="RadToolBar1_ButtonClick" AutoPostBack="true" Skin="Sunset"><BR> <Items><BR> <telerik:RadToolBarButton Text="Apply filter" CommandName="FilterRadGrid"<BR> ImageUrl="~/Portals/_default/Skins/PXQ_MRT_Skins/images/stock_filter_data_by_criteria.ico" Visible="false"><BR> </telerik:RadToolBarButton></P><P> <telerik:RadToolBarButton<BR> Text="Add new Sample Record" <BR> CommandName="InitInsert"<BR> ImageUrl="~/Portals/_default/Skins/PXQ_MRT_Skins/images/Add Record.gif" <BR> Visible='<%# !rgSampleData.MasterTableView.IsItemInserted %>'> <BR> </telerik:RadToolBarButton></P><P> <telerik:RadToolBarButton Text="Refresh"CommandName="RebindGrid"<BR> ImageUrl="~/images/synchronize.gif"><BR> </telerik:RadToolBarButton></P><P> </Items><BR> </telerik:RadToolBar><BR> </CommandItemTemplate><BR> <EditFormSettings InsertCaption="Enter Sample details"><BR> </EditFormSettings><BR> <Columns> <BR> <telerik:GridNumericColumn DataField="PXQSampleId" UniqueName="PXQSampleId"HeaderText="PXQ Sample Id" DataType="System.Int32" Visible="false"AllowFiltering="false"><BR> <ItemStyle Width="0%" HorizontalAlign="Center" CssClass="SmallLink"/><BR> </telerik:GridNumericColumn><BR> <telerik:GridBoundColumn DataField="activity" UniqueName="activity"HeaderText="Activity" AllowFiltering="false"readonly="true"><BR> <HeaderStyle Width="22%"/><BR> <ItemStyle Width="22%" HorizontalAlign="Left" CssClass="SmallLink"/><BR> </telerik:GridBoundColumn><BR> <telerik:GridBoundColumn DataField="ProcessType" UniqueName="ProcessType"HeaderText="Process" AllowFiltering="false"readonly="true"><BR> <HeaderStyle Width="22%"/><BR> <ItemStyle Width="22%" HorizontalAlign="Left" CssClass="SmallLink"/><BR> </telerik:GridBoundColumn><BR> <telerik:GridDateTimeColumn DataField="ConductedDate"UniqueName="ConductedDate" HeaderText="Conducted On"DataFormatString="{0:MM/dd/yyyy}"><BR> <HeaderStyle Width="15%"/><BR> <ItemStyle Width="15%" HorizontalAlign="Left" CssClass="SmallLink"/><BR> </telerik:GridDateTimeColumn><BR> <telerik:GridNumericColumn DataField="PopulationSize"UniqueName="PopulationSize" HeaderText="Population"DataType="System.Int32"><BR> <HeaderStyle Width="2%" HorizontalAlign="right"/><BR> <ItemStyle Width="2%" HorizontalAlign="right" CssClass="SmallLink"/><BR> </telerik:GridNumericColumn><BR> <telerik:GridNumericColumn DataField="SampleSize" UniqueName="SampleSize"HeaderText="Sample"DataType="System.Int32"><BR> <HeaderStyle Width="2%" HorizontalAlign="right"/><BR> <ItemStyle Width="2%" HorizontalAlign="right" CssClass="SmallLink"/><BR> </telerik:GridNumericColumn> <BR> <BR> <telerik:GridNumericColumn DataField="PXQActivityId"UniqueName="PXQActivityId" HeaderText="PXQActivityId" DataType="System.Int32"Visible="false"AllowFiltering="false"><BR> <ItemStyle Width="0%" HorizontalAlign="Center" CssClass="SmallLink"/><BR> </telerik:GridNumericColumn><BR> <telerik:GridNumericColumn DataField="PXQProcessTypeId"HeaderText="PXQProcessTypeId" DataType="System.Int32" Visible="false"AllowFiltering="false"><BR> <ItemStyle Width="0%" HorizontalAlign="Center" CssClass="SmallLink"/><BR> </telerik:GridNumericColumn> <BR> </P><P> <telerik:GridEditCommandColumn<BR> ButtonType="ImageButton" <BR> UniqueName="EditCommandColumn" <BR> CancelText="Cancel"<BR> UpdateText="Post" <BR> InsertText="Post New" <BR> EditText="Edit"></P><P> <HeaderStyle Width="40px"><BR> </HeaderStyle> <BR> </telerik:GridEditCommandColumn > <BR> <telerik:GridButtonColumn CommandName="Delete" ConfirmText="Are you sure you want to permanently delete this Sample Record and its Error/Cause Details?" Text="Delete" UniqueName="Delete"HeaderStyle-Width="6%"> <BR> </telerik:GridButtonColumn> <BR> </Columns><BR> <DetailTables><BR> <telerik:GridTableView DataKeyNames="PXQSampleId"<BR> Name="SampleErrorCauseTotal" <BR> Width="60%" <BR> HierarchyLoadMode="Client" <BR> runat="server" <BR> ShowFooter="True"<BR> AutoGenerateColumns="False"<BR> AllowFilteringByColumn="false"<BR> AllowPaging="false"<BR> ><BR> <ParentTableRelation><BR> <telerik:GridRelationFields DetailKeyField="PXQSampleId"MasterKeyField="PXQSampleId"/><BR> </ParentTableRelation></P><P> <Columns><BR> <telerik:GridTemplateColumn HeaderText="PXQSampleErrorCauseId"UniqueName="PXQSampleErrorCauseId" Visible="false" ><BR> <ItemTemplate><BR> <telerik:RadNumericTextBox runat="server" ID="PXQSampleErrorCauseId"Text='<%# Bind( "PXQSampleErrorCauseId") %>'><BR> <NumberFormat GroupSeparator="" DecimalDigits="0"/><BR> </telerik:RadNumericTextBox><BR> </ItemTemplate><BR> </telerik:GridTemplateColumn><BR> <telerik:GridBoundColumn DataField="PXQSampleId" UniqueName="PXQSampleId" Visible="false"/><BR> <telerik:GridBoundColumn DataField="PXQErrorCauseId"UniqueName="PXQErrorCauseId" HeaderText="PXQErrorCauseId" HeaderStyle-Width="5%"Visible="false"><BR> <ItemStyle HorizontalAlign="Left" CssClass="GridChildRowSmall" /> <BR> </telerik:GridBoundColumn><BR> <telerik:GridBoundColumn DataField="ErrorCause" UniqueName="ErrorCause"HeaderText="ERROR CAUSES"HeaderStyle-Width="25.7%"><BR> <ItemStyle HorizontalAlign="Left" Width="120px" CssClass="GridChildRowSmall"/> <BR> </telerik:GridBoundColumn><BR> <telerik:GridTemplateColumn HeaderText="ERROR CAUSE TOTALS"HeaderStyle-Width="37%"><BR> <ItemTemplate><BR> <telerik:RadNumericTextBox runat="server" ID="ErrorCauseTotal" Text='<%# Bind( "ErrorCauseTotal") %>'<BR> MinValue="0" MaxValue="99999" MaxLength="6" Width="50"><BR> <NumberFormat GroupSeparator="" DecimalDigits="0" /> <BR> <ClientEvents OnBlur="CauseBlur" OnFocus="CauseFocus"OnLoad="CauseGridItemLoad"></ClientEvents><BR> </telerik:RadNumericTextBox><BR> </ItemTemplate><BR> <FooterTemplate><BR> <telerik:RadButton runat="server" ID="btnCauseFooter"Width="100"<BR> ButtonType="LinkButton" Text="Total Causes" Skin="Sunset" ><BR> </telerik:RadButton><BR> <telerik:RadNumericTextBox ID="txtCauseFooter" runat="server" Width="40" MaxLength="6"><BR> <ClientEventsOnLoad="CauseFooterLoad"></ClientEvents><BR> <NumberFormat GroupSeparator="" DecimalDigits="0"/><BR> </telerik:RadNumericTextBox><BR> <BR> <telerik:RadButton runat="server" ID="btnSave"OnClientClicked="showalertSave" CommandName="SaveChanges"Width="40"<BR> ButtonType="LinkButton" Text="Save" Skin="Sunset" ><BR> </telerik:RadButton><BR> <BR> <telerik:RadButton runat="server" ID="btnQuit"OnClientClicked="showalertQuit" CommandName="Quit"Width="40"<BR> ButtonType="LinkButton" Text="Quit" Skin="Sunset" ><BR> </telerik:RadButton><BR> <BR> <telerik:RadButton runat="server" ID="btnChart"OnClientClicked="showalertChart" CommandName="Chart"Width="40"<BR> ButtonType="LinkButton" Text="Chart" Skin="Sunset" ><BR> </telerik:RadButton> <BR> </FooterTemplate><BR> </telerik:GridTemplateColumn><BR> </Columns><BR> <NoRecordsTemplate><BR> <em>None to Display</em><BR> </NoRecordsTemplate> <BR> </telerik:GridTableView><BR> </DetailTables> <BR> </MasterTableView><BR> <ClientSettings EnablePostBackOnRowClick="false"><BR> <Selecting AllowRowSelect="True"/><BR> <ClientEvents OnRowSelecting="RowSelecting"/><BR> <ClientEvents OnCommand="RowCommand"/><BR> </ClientSettings><BR> </telerik:RadGrid><BR></P>C#
<P> </P><P> protected voidrgSampleData_NeedDataSource(object source, GridNeedDataSourceEventArgs e)<BR> {<BR> //selectedActivityID = int.Parse(cbPXQUserActivity.SelectedValue.ToString());<BR> //selectedProcessID = int.Parse(cbPXQProcessType.SelectedValue.ToString());<BR> if ((cbPXQUserActivity.Items.Count > 0) && (!e.IsFromDetailTable))<BR> { LoadGrid_rgSampleData();}<BR> else<BR> {<BR> rgSampleData.DataSource = null;<BR> lblErrorMessage.Text = "You are not assigned to any Activity yet";<BR> }<BR> }</P><P><BR> protected voidLoadGrid_rgSampleData()<BR> {<BR> selectedActivityID = int.Parse(cbPXQUserActivity.SelectedValue.ToString());<BR> selectedProcessID = int.Parse(cbPXQProcessType.SelectedValue.ToString());<BR> var objPXQ_SampleDetailedList = from s indb.PXQSamples<BR> where (s.PXQActivityId == selectedActivityID &&<BR> s.PXQProcessTypeId == selectedProcessID)</P><P> join pact in db.PXQActivities on s.PXQActivityId equals pact.PXQActivityId</P><P> join makeAct in db.make_data_activities on pact.make_data_activities_Id equals makeAct.make_data_activities_id<BR> join procType in db.PXQProcessTypes on s.PXQProcessTypeId equals procType.PXQProcessTypeId</P><P> select new<BR> {<BR> makeAct.activity,<BR> s.PXQActivityId,<BR> procType.PXQProcessTypeId,<BR> procType.ProcessType,<BR> s.PXQSampleId,<BR> s.ConductedDate,<BR> s.PopulationSize,<BR> s.SampleSize,<BR> s.AddUser,<BR> s.AddDate,<BR> s.UpdateUser,<BR> s.UpdateDate,<BR> </P><P> };</P><P> rgSampleData.DataSource = objPXQ_SampleDetailedList; </P><P> }</P><P><BR> protected voidrgSampleData_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)<BR> {<BR> try<BR> {<BR> // retrieve the PXQSampleId from the datagrid<BR> GridDataItem gridDataItem = e.DetailTableView.ParentItem asGridDataItem;<BR> int sampleRecordID = int.Parse(gridDataItem["PXQSampleId"].Text);<BR> switch(e.DetailTableView.Name)<BR> {<BR> case"SampleErrorTotals":<BR> {</P><P><BR> List<PXQ_RetrieveSampleErrorTypeDetailResult> objSampleErrTotalsResult = sqlcontroller.PXQ_RetrieveSampleErrorTypeTotalList(sampleRecordID);<BR> e.DetailTableView.DataSource = objSampleErrTotalsResult;<BR> e.DetailTableView.ToolTip = "Error Totals for the above Sample";<BR> break;<BR> }</P><P> case"SampleErrorCauseTotal":<BR> {</P><P><BR> List<PXQ_RetrieveSampleErrorCauseDetailResult> objSampleErrCauseTotalsResult = sqlcontroller.PXQ_RetrieveSampleErrorCauseDetailList(sampleRecordID);</P><P> e.DetailTableView.DataSource = objSampleErrCauseTotalsResult;<BR> e.DetailTableView.ToolTip = "Error Causes for the above Sample";<BR> break;<BR> }</P><P> }<BR> }<BR> catch(Exception exc)<BR> {<BR> Exceptions.ProcessModuleLoadException(this, exc);<BR> }<BR> }</P><P> </P><BR>0
Hello Sabaratnam,
I suppose you have to set the same load mode to the MasterTableView.
Hope this helps.
Regards,
Daniel
the Telerik team
I suppose you have to set the same load mode to the MasterTableView.
Hope this helps.
Regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Sabaratnam
Top achievements
Rank 1
answered on 01 Mar 2013, 07:16 PM
Hi Daniel,
Thank you for your reply.
When I set HierarchyLoadMode="Client" in the MasterTableView definition I am getting the correct value for dataItem.get_nestedViews().length.
However the InLine Edit function of the MasterTable Row now fails. When I click on the pencil Icon to edit a row it doesn't go into edit mode.
Looks like the inline Edit function of the Master Table in my case requires the HierarchyLoadMode to be ServerBind.
My client settings is as follows:-
<ClientSettings EnablePostBackOnRowClick="false" >
<Selecting AllowRowSelect="True" />
<ClientEvents OnRowSelecting="RowSelecting" />
<ClientEvents OnCommand="RowCommand" />
</ClientSettings>
I need EnablePostBackOnRowClick="false" in order to perform some processing at the client.
Not sure how to proceed from here.
Thank you for your reply.
When I set HierarchyLoadMode="Client" in the MasterTableView definition I am getting the correct value for dataItem.get_nestedViews().length.
However the InLine Edit function of the MasterTable Row now fails. When I click on the pencil Icon to edit a row it doesn't go into edit mode.
Looks like the inline Edit function of the Master Table in my case requires the HierarchyLoadMode to be ServerBind.
My client settings is as follows:-
<ClientSettings EnablePostBackOnRowClick="false" >
<Selecting AllowRowSelect="True" />
<ClientEvents OnRowSelecting="RowSelecting" />
<ClientEvents OnCommand="RowCommand" />
</ClientSettings>
I need EnablePostBackOnRowClick="false" in order to perform some processing at the client.
Not sure how to proceed from here.
0
Hello Sabaratnam,
It doesn't go in edit mode because you are handling the client-side oncommand event and you are cancelling the command there.
Regards,
Daniel
the Telerik team
It doesn't go in edit mode because you are handling the client-side oncommand event and you are cancelling the command there.
Regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.