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

Dynamically hide GridButtonColumn in RadGrid

3 Answers 2630 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tom
Top achievements
Rank 1
Tom asked on 14 Mar 2012, 10:37 PM

Hi,

I have a RadGrid that contains two GridButtonColumns of HeaderButtonType LinkButton.  I would like to control the behavior to dynamically hide one column and show the other and vice versa.

In addition, in one of the circumstances there is an additional GridBoundColumn that I would like to hide.

How do I do this?  Also, can I do it in the ItemDataBound method or does it need to be done in a PreRender method?

In the Grid below, I want to show the RequestorName and RequestorPd columns and hide the InstitutionName column in one case and in the other I want to show the InstitutionName column and hide the RequestorName and RequestorPd columns.

<telerik:RadGrid ID="RadGridPrescriber" runat="server" 
    AutoGenerateColumns="false" AllowSorting="True" AllowPaging="false" Skin="Simple" ClientSettings-Resizing-AllowColumnResize="true"
    ItemStyle-Wrap="false" Width="95%" HeaderStyle-Wrap="false" PageSize="25"
    OnNeedDataSource="RadGridPrescriber_NeedDataSource" OnItemDataBound="RadGridPrescriber_ItemDataBound" OnItemCommand="RadGridPrescriber_ItemCommand">
    <ClientSettings>
        <Scrolling AllowScroll="true" UseStaticHeaders="true"  />
    </ClientSettings>
    <ExportSettings ExportOnlyData="false" IgnorePaging="true" OpenInNewWindow="true" />
    <MasterTableView TableLayout="Fixed" AllowMultiColumnSorting="true" DataKeyNames="RequestorSln" ShowFooter="false" PagerStyle-AlwaysVisible="false" CommandItemDisplay="Top" >
        <NoRecordsTemplate>
            <asp:Label ID="lblMsg" runat="server" Text="No Records found"></asp:Label>
        </NoRecordsTemplate>
        <CommandItemSettings ShowExportToPdfButton="true" ShowExportToCsvButton="true" ShowExportToExcelButton="true" ShowRefreshButton="false" ShowAddNewRecordButton="false" />
        <Columns>
            <telerik:GridButtonColumn HeaderText="Name" HeaderButtonType="LinkButton" UniqueName="RequestorName" CommandName="GetByRequestorSLN" DataTextField="RequestorName" ItemStyle-HorizontalAlign="Left" />
            <telerik:GridButtonColumn HeaderText="Name" HeaderButtonType="LinkButton" UniqueName="InstitutionName" CommandName="GetByRequestorSLN" DataTextField="InstitutionName" ItemStyle-HorizontalAlign="Left" />
            <telerik:GridBoundColumn HeaderText="PD" HeaderButtonType="TextButton" DataField="RequestorPd" ItemStyle-HorizontalAlign="Left" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>




Thanks,

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 15 Mar 2012, 05:55 AM
Hello Tom,

I guess your requirement is to hide ButtonColumn/BoundColumn (by making invisible) in certain rows based on a given condition.
Quite like how you specified a UniqueName for ButtonColumn, you need to give a UniqueName for the BoundColumn, without which you will not be able to access the GridBoundColumn in ItemDataBound event.
Please take a look into the following code.
C#:
protected void RadGridPrescriber_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
   GridDataItem ditem = (GridDataItem)e.Item;
   LinkButton linkbutton1 = (LinkButton)ditem["RequestorName"].Controls[0];
   LinkButton linkbutton2 = (LinkButton)ditem["InstitutionName"].Controls[0];
   TableCell cell = (TableCell)ditem["ColumnUniqueName"];    
   //your condition
   {
     linkbutton1.Visible = false;
     linkbutton2.Visible = false;
   }
             
  }
}

Let me know if you have any questions.
Thanks,
-Shinu.
0
Tom
Top achievements
Rank 1
answered on 15 Mar 2012, 02:19 PM
Hi Shinu,

My requirement will apply to the entire grid (not by row).

If a search was performed under condition A, then I wish to show the columns (on all rows) for RequestorName (GridButtonColumn as HeaderButtonType LinkButton) and RequestorPd (GridBoundColumn) and hide the column for InstitutionName (GridButtonColumn as HeaderButtonType LinkButton).

If a search was performed under condition B, then I wish to show the column for InstitutionName and hide the RequestorName and RequestorPd columns.

The first problem is that I actually did the code suggested for the LinkButtons in the ItemDataBound and it failed to hide either column.  I was able to hide the GridBoundColumn using a slightly different approach though.

protected void RadGridPrescriber_ItemDataBound(object sender, GridItemEventArgs e)
{
    try
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            LinkButton btnRequestor = (LinkButton)item["RequestorName"].Controls[0];
            LinkButton btnInstitution = (LinkButton)item["InstitutionName"].Controls[0];
            GridColumn pd = RadGridPrescriber.MasterTableView.GetColumn("RequestorPd");
            if (ClientBrandId == (int)EnumClientBrand.SAMPLE_FULFILLMENT)
            {
                btnRequestor.Visible = true;
                btnInstitution.Visible = false;
                pd.Visible = true;
            }
            else if (ClientBrandId==(int)EnumClientBrand.PRODUCT_REPLACEMENT)
            {
                btnRequestor.Visible = false;
                btnInstitution.Visible = true;
                pd.Visible = false;
            }
        }
        if (this._isExport && e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            foreach (GridColumn col in RadGridPrescriber.MasterTableView.RenderColumns)
            {
                if (col is GridButtonColumn)
                {
                    item[col].Text = (item[col].Controls[0] as IButtonControl).Text;
                }
            }
        }
    }
    catch (Exception ex)
    {
        ... handle exception
    }
}
0
Shinu
Top achievements
Rank 2
answered on 16 Mar 2012, 06:03 AM
Hello Tom,

If you want to hide the ButtonColumn for the entire grid, you can check for the condition in Prerender event as shown below.
C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
  if(condition)
  {
    RadGrid1.MasterTableView.GetColumn("InstitutionName").Display = false;
  }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Tom
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Tom
Top achievements
Rank 1
Share this question
or