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

Show a column in detail table depending on a sesiion variable

3 Answers 46 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rui
Top achievements
Rank 1
Rui asked on 18 Aug 2014, 09:06 PM
Hi,
I want to show a column in the radgrid detail table depending on a session flag.
I can show the column when I show the detail table, but once I show another detail table for another data item, the column is  invisible again. 
How do I show the column for all expanded detail table?
Here is my code:

       <telerik:RadGrid 
            ID="radgrid1" 
            Runat="server"  
            AutoGenerateColumns="false"
            AllowPaging="true" 
            PageSize="10"
            AllowFilteringByColumn="False"
            AllowSorting="True" 
            AllowMultiRowSelection="false"     
            OnSelectedIndexChanged="radgrid1_SelectedIndexChanged"
            OnNeedDataSource="radgrid1_NeedDataSource"
            OnUpdateCommand="radgrid1_UpdateCommand" 
            OnItemCreated="radgrid1_GridItemCreated"
            OnItemDataBound ="radgrid1_ItemDataBound"
            OnCreateColumnEditor="radgrid1_CreateColumnEditor" 
            OnItemCommand="radgrid1_ItemCommand"
            OnDetailTableDataBind="radgrid1_DetailTableDataBind"
            AutoGenerateHierarchy="true"
            CellSpacing="0" 
            GridLines="None"
            DataMember="PortalUserTable">
        <MasterTableView DataKeyNames= "Id,UserName" EditMode="PopUp" CommandItemDisplay="Top">
            <CommandItemSettings ShowExportToExcelButton="true" ShowExportToWordButton="true" ShowExportToPdfButton="true" ShowAddNewRecordButton="false" />
            <DetailTables>
                <telerik:GridTableView Name="detailtable1" 
                    Width="100%" NoDetailRecordsText="No Location is set up for this user."
                    AllowFilteringByColumn="false" AllowPaging="false">
                    <Columns>
                        <telerik:GridBoundColumn UniqueName="column1" HeaderText="Location" DataField="column1" HeaderStyle-Width="200px" Display="false"/>
                       
                            DataField="LocationId" Display="false" />
                    </Columns>
                </telerik:GridTableView>
            </DetailTables>
            <Columns>
            .................
             </Columns>
            <EditFormSettings>
               <FormTemplate>
.............................................
                </FormTemplate>
             </EditFormSettings>
</telerik:RadGrid>

protected void radgrid1_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
{
      if (Convert.ToBoolean(Session["flag"]))
      {
          e.DetailTableView.GetColumn("column1").Display = true;
      }
}

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 19 Aug 2014, 04:49 AM
Hi Rui,

I guess you want to hide a detail table column depending on a condition. Please use the PreRender event to obtain this, try the following code snippet.

C#:
protected void radgrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridDataItem dataItems in radgrid1.MasterTableView.Items)
    {
        if (dataItems.Expanded)
        {
         foreach (GridTableView innerDetailView in dataItems.ChildItem.NestedTableViews)
            {
                if (Convert.ToBoolean(Session["flag"]))
                {
                  innerDetailView.GetColumn("column1").Visible = true;
                }
                else
                {
                  innerDetailView.GetColumn("column1").Visible = false;
                  innerDetailView.Rebind();
                }
            }
        }
    }
}

Thanks,
Princy
0
Rui
Top achievements
Rank 1
answered on 19 Aug 2014, 03:16 PM
Thanks, that works.  The only thing I missed is that rebind.
When do I need to trigger a rebind?
0
Princy
Top achievements
Rank 2
answered on 20 Aug 2014, 04:47 AM
Hi Rui,

The Rebind method forces the grid to bind again and if any changes made to the current structure will be visible. Since by setting visible as false, you are changing the Grid structure, hence in order to see the changes, we have to rebind it. When you want to change the current structure to a new one you can call Rebind to affect the changes.

Thanks,
Princy
Tags
Grid
Asked by
Rui
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Rui
Top achievements
Rank 1
Share this question
or