Search code examples
c#telerik-grid

Unable to find a control inside Grid cell


I'm using Hierarchical Telerik Grid View.I want to hide the expand/hide Button If the Question is of certain type. I'm trying to achieve this on Grid_ItemDatatbound.

                  <CW:GridView ID="rGVEvaluationQuestions" runat="server" Width="99%" OnItemDataBound="rGVEvaluationQuestions_ItemDataBound"
                        OnDetailTableDataBind="rGVEvaluationQuestions_DetailTableDataBind" OnNeedDataSource="rGVEvaluationQuestions_NeedDataSource"

                        AllowPaging="false" ShowHeader="false" GridLines="Both">
                        <MasterTableView DataKeyNames="ID" AllowMultiColumnSorting="True">
                            <DetailTables>
                                <Telerik:GridTableView DataKeyNames="ID" Name="LabelQuestion" Width="100%" Height="100px">
                                    <Columns>
                                        <Telerik:GridTemplateColumn HeaderText="<%$Resources:LanguageResource, Edit%>" ItemStyle-Width="50px"
                                            DataField="ID">
                                            <ItemTemplate>
                                                <asp:HiddenField ID="hdnEvalID" runat="server" />
                                                <asp:HiddenField ID="hdnEvaluationQuestionID" runat="server" />
                                                <asp:HiddenField ID="hdnEvaluationQuestionTypeID" runat="server" />
                                                <asp:Label ID="lblQuestionNumber" runat="server"></asp:Label>
                                            </ItemTemplate>
                                        </Telerik:GridTemplateColumn>
                               <Telerik:GridTemplateColumn ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="lblQuestion" runat="server"></asp:Label><br />
                                        <br />
                                       Display="Dynamic"></asp:CustomValidator></td>
                                    </ItemTemplate>
                                </Telerik:GridTemplateColumn>
                                        //Some Hidden fields and textboxex here
                                    </Columns>
                                </Telerik:GridTableView>
                            </DetailTables>
                            <Columns>
                                <Telerik:GridTemplateColumn HeaderText="<%$Resources:LanguageResource, Edit%>" ItemStyle-Width="50px"
                                    DataField="ID">
                                    <ItemTemplate>
                                        <asp:HiddenField ID="hdnEvalID" runat="server" />
                                        <asp:HiddenField ID="hdnEvaluationQuestionID" runat="server" />
                                        <asp:HiddenField ID="hdnEvaluationQuestionTypeID" runat="server" />
                                        <asp:Label ID="lblQuestionNumber" runat="server"></asp:Label>
                                    </ItemTemplate>
                                </Telerik:GridTemplateColumn>
                                <Telerik:GridTemplateColumn ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="lblQuestion" runat="server"></asp:Label><br />
                                        <br />
                                       //Some Hidden fields and textboxex here
                                            Display="Dynamic"></asp:CustomValidator></td>
                                    </ItemTemplate>
                                </Telerik:GridTemplateColumn>
                            </Columns>
                        </MasterTableView>
                    </CW:GridView>

On code behind this is what im doing.

if (evalQuestionType == Coursewhere.BLL.Enums.EvaluationQuestionType.Label)
{
    if (item.Cells.Count > 0)
    {
        item.Cells[0].Text = " ";
        item.Cells[0].Enabled = true;

    }

}
else
{
    item.Cells[0].Text = " ";

    if (item.Cells[0].Controls.Count>0)
    {  
        item.Cells[0].Controls[0].Visible = false;
    }
    item.Cells[0].Enabled = false;
    item.Cells[0].Style.Add("background", "#8ea3b9 none");
}

item.cell[0] is a td that has <input type Button> .However im unable to find it here in code behind. Once found i want to set its style. What am i doing wrong? Please Not that im able to find item.control[0] which is a td. But unable to fin the control which is inside it.Is it because its not running @server?

EDIT: I found following code that i call on page_prerender. This makes the expand/hide button invisible for all rows. But i want this to happen only for specific Rows.

  protected void rGVEvaluationQuestions_PreRender(object sender, EventArgs e)
        {
            HideExpandColumnRecursive(rGVEvaluationQuestions.MasterTableView);
        }
        public void HideExpandColumnRecursive(GridTableView tableView)
        {
            GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView);
            foreach (GridNestedViewItem nestedViewItem in nestedViewItems)
            {
                foreach (GridTableView nestedView in nestedViewItem.NestedTableViews)
                {
                    if (nestedView.Items.Count == 0)
                    {
                        TableCell cell = nestedView.ParentItem["ExpandColumn"];
                        cell.Controls[0].Visible = false;
                        cell.Text = " ";
                        nestedViewItem.Visible = false;
                    }
                    if (nestedView.HasDetailTables)
                    {
                        TableCell cell = nestedView.ParentItem["ExpandColumn"];
                        if (cell.Controls.Count > 0)
                        {
                            cell.Controls[0].Visible = true;
                            cell.Text = " ";
                            nestedViewItem.Visible = true;
                        }
                    }
                }
            }
        }

can someone please help me in using this function's functionality on databound function so that i could disable/invisible the appropriate Row's expand button?


Solution

  • I Dont know why this didn't work.

       if (item.Cells[0].Controls.Count>0)
        {  
            item.Cells[0].Controls[0].Visible = false;
        }    
    

    Finally this worked.

    TableCell tccell = item.Cells[0];
                   if (evalQuestionType == Coursewhere.BLL.Enums.EvaluationQuestionType.Label)
                        {
                            tccell.Controls[0].Visible = true;
    
                        }
                        else
                        {
                            tccell.Controls[0].Visible = false;
    
                        }