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

turning ShowHeadersWhenNoRecords on/off in code-behind

6 Answers 56 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Helen
Top achievements
Rank 1
Helen asked on 19 Feb 2015, 12:16 AM
Hi,

I have a grid with detail table which has a button that can show/hide filter item. 
I also set ShowHeadersWhenNoRecords="false" declaratively in detail table by default.
However, when a user puts in filter criteria that's not in the detail table, the whole table disappears.
I tried various ways to set ShowHeadersWhenNoRecords="true" in the code-behind but it only works if I don't have validation above that line of code.
Could you tell me how to make the detail table not disappearing when there's no record during filtering?

Thanks,

Helen

Here's my ascx page:

<telerik:RadGrid ID="RadLeadsBySalesGrid" runat="server" AllowPaging="True" OnNeedDataSource="RadLeadsBySalesGrid_NeedDataSource" OnDataBinding="RadLeadsBySalesGrid_DataBinding" OnItemDataBound="RadLeadsBySalesGrid_ItemDataBound"
        OnDetailTableDataBind="RadLeadsBySalesGrid_DetailTableDataBind" AutoGenerateColumns="False" OnItemCommand="RadLeadsBySalesGrid_ItemCommand" ShowFooter="true"
        Width="1900" Height="830" Skin="Telerik" EnableEmbeddedSkins="false" PageSize="100" BorderStyle="None" >
        <GroupingSettings CaseSensitive="false" />
        <ClientSettings EnablePostBackOnRowClick="true" Selecting-AllowRowSelect="true" EnableRowHoverStyle="true" AllowColumnsReorder="true" ReorderColumnsOnClient="true" ColumnsReorderMethod="Reorder" >
            <%--<Scrolling AllowScroll="true" />--%>
            <ClientEvents OnRowClick="closePane" />
            <Resizing ClipCellContentOnResize="true" AllowColumnResize="true" EnableRealTimeResize="true" ResizeGridOnColumnResize="true" />             
        </ClientSettings>
    <MasterTableView DataKeyNames="office_id, sales_id" HierarchyDefaultExpanded="true" Width="99%" TableLayout="Auto"  >
        <DetailTables>            
            <telerik:GridTableView Name="LeadsTableView" runat="server" DataKeyNames="sales_id" AllowSorting="true" ShowFooter="true" Width="100%"
                        AllowFilteringByColumn="true" IsFilterItemExpanded="false" EnableHeaderContextMenu="true" PagerStyle-Mode="NextPrevNumericAndAdvanced" ShowHeadersWhenNoRecords="false"  >
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="sales_id" MasterKeyField="sales_id" />
                    </ParentTableRelation>
                 
                    <Columns>
                        <telerik:GridBoundColumn  DataField="lead_id" ReadOnly="True" Display="false" HeaderText="lead_id" SortExpression="lead_id"  UniqueName="lead_id" DataType="System.Int32" FilterControlAltText="Filter lead_id column"
                            HeaderStyle-Font-Size="Smaller"  />
                        <telerik:GridBoundColumn DataField="sales_id" ReadOnly="True" Display="false" FilterControlAltText="Filter sales_id column" HeaderText="SalesId" SortExpression="sales_id" UniqueName="sales_id"
                            HeaderStyle-Font-Size="12px" HeaderStyle-ForeColor="Black" HeaderStyle-Font-Bold="true" ItemStyle-Font-Size="Smaller" ItemStyle-Font-Bold="true" ItemStyle-Width="20%" />
 
                        <telerik:GridBoundColumn DataField="last_action_dt" FilterControlAltText="Filter last_action_dt column" HeaderText="Last Action Dt" SortExpression="last_action_dt" UniqueName="last_action_dt"
                            HeaderStyle-Font-Size="12px" HeaderStyle-ForeColor="Black" HeaderStyle-Font-Bold="true" ItemStyle-Font-Size="Smaller" ItemStyle-Font-Bold="false"  FilterControlWidth="70%" />
                    </Columns>                     
 
            </telerik:GridTableView>
        </DetailTables>
        <Columns>
                <telerik:GridBoundColumn DataField="office_id" Display="false" ReadOnly="true" Visible="false" HeaderText="Office ID" SortExpression="office_id" UniqueName="office_id" FilterControlAltText="Filter office_id column" />
                <telerik:GridBoundColumn DataField="sales_id" Display="false" ReadOnly="true" Visible="false" HeaderText="Sales ID" SortExpression="sales_id" UniqueName="sales_id" FilterControlAltText="Filter sales_id column" />                   
                <telerik:GridTemplateColumn UniqueName="sales_name" DataField="sales_name"
                    ItemStyle-Font-Bold="true" ItemStyle-Font-Size="Smaller"  SortExpression="sales_name" FilterControlAltText="Filter sales_name column" >
                    <HeaderTemplate>
                        <asp:ImageButton runat="server" ID="ExpandPaneButton" ToolTip="Click to expand/collpase the results window" Width="32px" Height="32px"  CommandName="ExpandResultPane" ImageUrl="~/images/expandHorizontal.png" OnClientClick="expandCollapseResultPane();return false;" />
                        <asp:ImageButton runat="server" ID="RefreshButton" ToolTip="Click to refresh list" Width="32px" Height="32px"  CommandName="Refresh" ImageUrl="~/images/refresh24.jpg" />
 
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="SalesNameLabel" runat="server" ForeColor="Blue" Text='<%# DataBinder.Eval(Container.DataItem, "sales_name") %>' />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>    
</telerik:RadGrid>

My code-behind:

protected void RadLeadsBySalesGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
 
    if (e.Item is GridFilteringItem && e.Item.OwnerTableView.Name == "LeadsTableView")
    {
        GridFilteringItem item = e.Item as GridFilteringItem;
        if (item.Expanded) // code works when I don't have if statement
        {
            RadLeadsBySalesGrid.MasterTableView.DetailTables[0].ShowHeadersWhenNoRecords = true;
        }
        else
        {
            RadLeadsBySalesGrid.MasterTableView.DetailTables[0].ShowHeadersWhenNoRecords = false;
        }
    }
}
 
protected void RadLeadsBySalesGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    SALTDALTableAdapters.OfficeTableAdapter officeTA = new SALTDALTableAdapters.OfficeTableAdapter();
    SALTDAL.OfficeDataTable officeDT = new SALTDAL.OfficeDataTable();
    if (SalesJobCode == "DSM")
        officeDT = saltBLL.GetDataByOfficeAndSalesId(OfficeId, SalesIdSelected.Replace('|',','));
    else
        officeDT = officeTA.GetDataBySalesId(SalesId);
 
    int count = officeDT.Count;
 
    RadLeadsBySalesGrid.DataSource = officeDT;
 
}  
 
protected void RadLeadsBySalesGrid_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
 
    GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
    string SalesIdSelected = dataItem.GetDataKeyValue("sales_id").ToString().Trim();
 
    if (e.DetailTableView.Name == "LeadsTableView")
    {
        _dt = (SALTSearchCriteriaDAL.SALTLeadDataTable)Session["SaltLeadsDT"];
        int oricount = _dt.Rows.Count;
        e.DetailTableView.DataSource = _dt;
 
        DataRow[] filterRows;
        filterRows = _dt.Select("sales_id=" + SalesIdSelected);
 
        SALTSearchCriteriaDAL.SALTLeadDataTable newDT = _dt.Clone() as SALTSearchCriteriaDAL.SALTLeadDataTable;
        foreach (DataRow row in filterRows)
        {
            try
            {
                newDT.ImportRow(row);
            }
            catch (Exception ex)
            {
                string errMsg = ex.Message;
                ShowErrorMessage(errMsg);
            }
        }
 
        e.DetailTableView.DataSource = newDT;
        _total = newDT.Rows.Count;
    }       
}  


6 Answers, 1 is accepted

Sort by
0
Helen
Top achievements
Rank 1
answered on 20 Feb 2015, 11:32 PM
Can anyone help?
0
Jaya
Top achievements
Rank 1
answered on 21 Feb 2015, 05:54 AM
Hi
can you Provide Table Strures and Db connections
0
Eyup
Telerik team
answered on 23 Feb 2015, 12:27 PM
Hi Helen,

You can use the following approach to achieve this requirement:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.FilterCommandName)
    {
        string function = (e.CommandArgument as Pair).Second.ToString();
        if (function != "NoFilter")
        {
            e.Item.OwnerTableView.ShowHeadersWhenNoRecords = true;
        }
    }
}

A more generic approach would be the following:
protected void RadGrid1_DataBound(object sender, EventArgs e)
{
    if (RadGrid1.Items.Count == 0 && string.IsNullOrEmpty(RadGrid1.MasterTableView.FilterExpression))
    {
        GridItemType[] typesToHide = new GridItemType[] {
            GridItemType.Footer, GridItemType.CommandItem,
            GridItemType.Header, GridItemType.FilteringItem };
 
        foreach (GridItemType itemType in typesToHide)
        {
            foreach (GridItem item in RadGrid1.MasterTableView.GetItems(itemType))
            {
                item.Display = false;
            }
        }
    }
}

Hope this helps.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Helen
Top achievements
Rank 1
answered on 23 Feb 2015, 01:29 PM
Eyup,

Thanks for your reply. I tried the first approach, the detail table didn't go away, but the filter didn't work either.  The records I tried to filter out remained in the table.  The second approach hide away the whole detail table including the header.  Any idea what might have caused this problem?

Thanks,

Helen
0
Helen
Top achievements
Rank 1
answered on 24 Feb 2015, 07:43 PM
Eyup,

The first approach is working for me now.  I found out that I had some uncommented out test codes that interfered with the result.

Thank you for your help!
0
Eyup
Telerik team
answered on 26 Feb 2015, 10:13 AM
Hello Helen,

I'm glad the suggested approach has proven helpful.
Thank you for sharing your experience with this case with our community.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Helen
Top achievements
Rank 1
Answers by
Helen
Top achievements
Rank 1
Jaya
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or