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

Access DataKeyValues and DataItem values in ItemCreated event when using NestedViewTemplate

1 Answer 71 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jignesh
Top achievements
Rank 1
Jignesh asked on 09 Nov 2013, 06:38 AM
I need to bind a UserControl in the ItemCreated Event of Telerik RadGrid.
In order to bind it,I need the primary key value that is DataKeyValue and also in DataItem.
But,when using NestedViewTemplate,I am not able to access the value.
Also,when the Hierarchy Button(left side of Grid) is clicked,the ItemCreated Event is fired first and after that NeedDataSource event is fired,sue to which the DataItem and DataKeyValue are not avaiable.

I have provide the code.Kindly let me know if the scenario which I am trying to achieve is possbile or what is the probelm with the code.?

or do I need to use FormTemplate-EditMode to achieve it?

<telerik:RadGrid ID="radGridQuestionnaire" runat="server" <br>
                    Width="100%"   <br>
                    PageSize="20"    <br>
<font class="Apple-tab-span" style="white-space: pre;"> </font>                AutoGenerateColumns="False" <br>
                    GridLines="None" <br>
                    AllowSorting="True" <br>
<font class="Apple-tab-span" style="white-space: pre;"> </font>                AllowPaging="True" <br>
                    OnNeedDataSource="radGridQuestionnaire_NeedDataSource"<br>
                    OnItemCreated="radGridQuestionnaire_ItemCreated"<br>
                    OnItemDataBound="radGridQuestionnaire_ItemDataBound"<br>
                    OnItemCommand="radGridQuestionnaire_ItemCommand"><br>
        <PagerStyle Mode="NextPrevNumericAndAdvanced"/><br>
<font class="Apple-tab-span" style="white-space: pre;"> </font>    <MasterTableView EnableViewState="true" DataKeyNames="Form.DisplayFormName,Question.QuestionNumber,Question.QuestionId,ReviewId" Width="100%"><br>
            <RowIndicatorColumn><br>
                <HeaderStyle Width="20px"></HeaderStyle><br>
            </RowIndicatorColumn><br>
<font class="Apple-tab-span" style="white-space: pre;">     </font>    <ExpandCollapseColumn><br>
<font class="Apple-tab-span" style="white-space: pre;">         </font>    <HeaderStyle Width="20px"></HeaderStyle><br>
<font class="Apple-tab-span" style="white-space: pre;">     </font>    </ExpandCollapseColumn><br>
            <Columns><br>
                <telerik:GridBoundColumn HeaderText="Form" UniqueName="FormName" DataField="Form.DisplayFormName" HeaderStyle-Width="40%" /><br>
                <telerik:GridBoundColumn HeaderText="Question #" UniqueName="QuestionNumber" DataField="Question.QuestionNumber" HeaderStyle-Width="15%" /><br>
                <telerik:GridBoundColumn HeaderText="Status" UniqueName="QuestionnaireStatusName" DataField="QuestionnaireStatus.QuestionnaireStatusName" HeaderStyle-Width="15%" /><br>
                <telerik:GridBoundColumn HeaderText="Submitted By" UniqueName="SubmittedBy" DataField="Questionnaire.SubmittedBy" HeaderStyle-Width="15%" /><br>
                <telerik:GridBoundColumn HeaderText="Submitted On" UniqueName="SubmittedOn" DataField="Questionnaire.SubmittedOn" HeaderStyle-Width="15%" /><br>
            </Columns><br>
            <NestedViewTemplate><br>
                <div id="divChoiceMain" class="pad10"><br>
                    <div class="divChoiceTextSection"><br>
                        <div class="divChoiceTextLeftSection">Form:</div><br>
                        <div class="divFloatLeft"><br>
                            <asp:Label ID="lblFormName" runat="server" Text='<%# Eval("Form.DisplayFormName")%>'/><br>
                        </div><br>
                    </div><br>
                    <div class="divChoiceTextSection"><br>
                        <div class="divChoiceTextLeftSection">Question #:</div><br>
                        <div class="divFloatLeft"><br>
                            <div><br>
                                <asp:Label ID="lblQuestionNumber" runat="server" Text='<%#Eval("Question.QuestionNumber")%>'/><br>
                            </div><br>
                        </div><br>
                    </div><br>
                    <div class="divChoiceTextCorrectiveActionSection"><br>
                        <div class="divFloatLeft padtop5" style="margin-left:100px;"><br>
                            <ar:ReviewConfigurationToolTipUC ID="reviewConfigurationToolTipUC" runat="server" /><br>
                        </div><br>
                    </div><br>
                </div><br>
                <div class="clearfix pad10"><br>
                    <div class="divChoiceTextSection"><br>
                        <div class="divFloatLeft padRight10"><br>
                            <asp:LinkButton ID="lnkBtnSave" runat="server" Text="Save" CommandName="Save"></asp:LinkButton><br>
                        </div><br>
                        <div class="divFloatLeft padRight10"><br>
                            <asp:LinkButton ID="lnkSaveAndSubmit" runat="server" Text="Save & Submit" CommandName="Submit"></asp:LinkButton><br>
                        </div><br>
                        <div class="divFloatLeft"><br>
                            <asp:LinkButton ID="lnkBtnCancel" runat="server" Text="Cancel" CommandName="Cancel"></asp:LinkButton><br>
                        </div><br>
                    </div><br>
                </div><br>
            </NestedViewTemplate><br>
        </MasterTableView><br>
    </telerik:RadGrid>


protected void radGridQuestionnaire_ItemCreated(object sender, GridItemEventArgs e)<br>
        {<br>
            if (e.Item is GridNestedViewItem)<br>
            {<br>
                GridDataItem gridDataItemObj = (e.Item as GridNestedViewItem).ParentItem;<br>
                GridNestedViewItem gridNestedViewItemObj = e.Item as GridNestedViewItem;<br>
<br>
                if (gridDataItemObj == null || gridNestedViewItemObj == null)<br>
                    return;<br>
<br>
                //Form.DisplayFormName,Question.QuestionNumber,ReviewId<br>
                //string formName = gridDataItemObj.GetDataKeyValue("Form.DisplayFormName") as string;<br>
                //string questionNumaber = gridDataItemObj.GetDataKeyValue("Question.QuestionNumber") as string;<br>
                //string reviewId = gridDataItemObj.GetDataKeyValue("ReviewId") as string;<br>
                //string questionIdStr = gridDataItemObj.GetDataKeyValue("Question.QuestionId") as string;<br>
                if (e.Item.ItemIndex >= 0)<br>
                {<br>
                    string formName = radGridQuestionnaire.MasterTableView.DataKeyValues[e.Item.ItemIndex]["Form.DisplayFormName"] as string;<br>
                    string questionNumaber = radGridQuestionnaire.MasterTableView.DataKeyValues[e.Item.ItemIndex]["Question.QuestionNumber"] as string;<br>
                    string reviewId = radGridQuestionnaire.MasterTableView.DataKeyValues[e.Item.ItemIndex]["ReviewId"] as string;<br>
                    string questionIdStr = radGridQuestionnaire.MasterTableView.DataKeyValues[e.Item.ItemIndex]["Question.QuestionId"] as string;<br>
                }<br>
<br>
                //object dataKeyValue = ((sender as RadGrid).NamingContainer as DataItem).GetDataKeyValue("ID");<br>
                //use the dataKeyValue to fetch the correct Employee object <br>
                Questionnaire questionnaireObj = gridDataItemObj.DataItem as Questionnaire;<br>
                ReviewConfigurationToolTipUC reviewConfigurationToolTipUCObj = gridNestedViewItemObj.FindControl("reviewConfigurationToolTipUC") as ReviewConfigurationToolTipUC;<br>
<br>
                if (questionnaireObj!=null && questionnaireObj.Question!=null && questionnaireObj.Question.QuestionId.HasValue && reviewConfigurationToolTipUCObj != null)<br>
                {<br>
                    //Create FormQuestion Obj.<br>
                    FormQuestion formQuestionObj = new FormQuestion { QuestionId = questionnaireObj.Question.QuestionId };<br>
                    reviewConfigurationToolTipUCObj.BuildToolTip(questionnaireObj.Question.QuestionName, formQuestionObj, null);<br>
                    <br>
                }<br>
            }<br>
        }



1 Answer, 1 is accepted

Sort by
0
Jignesh
Top achievements
Rank 1
answered on 11 Nov 2013, 09:00 PM
I came up with this approach,let me know if this is optimal?

if (e.Item is GridNestedViewItem)
            {
                GridNestedViewItem gridNestedViewItemObj = e.Item as GridNestedViewItem;
                GridDataItem parentItemObj = (e.Item as GridNestedViewItem).ParentItem;

                if (gridNestedViewItemObj == null || parentItemObj == null)
                     return;

                    if (gridNestedViewItemObj.OwnerTableView.DataKeyValues != null)
                    {
                        int? questionId = gridNestedViewItemObj.OwnerTableView.DataKeyValues[parentItemObj.ItemIndex]["Question.QuestionId"].ToNullableInt32();

                        if (questionId.HasValue)
                        {
                            ReviewConfigurationToolTipUC reviewConfigurationToolTipUCObj = gridNestedViewItemObj.FindControl("reviewConfigurationToolTipUC") as ReviewConfigurationToolTipUC;

                            if (reviewConfigurationToolTipUCObj != null)
                            {
                                //Create FormQuestion Obj.
                                FormQuestion formQuestionObj = new FormQuestion { QuestionId = questionId };
                                reviewConfigurationToolTipUCObj.BuildToolTip(string.Empty, formQuestionObj, null, true);
                            }
                        }
                    }
               }
Tags
Grid
Asked by
Jignesh
Top achievements
Rank 1
Answers by
Jignesh
Top achievements
Rank 1
Share this question
or