Hide columns in radgrid based on querystring

4 posts, 0 answers
  1. Julie
    Julie avatar
    2 posts
    Member since:
    Jun 2012

    Posted 21 Jun 2012 Link to this post

    I am trying to hide a column based on a value in the querystring and it continues to show: I have tried it 2 different ways and cannot figure out what I am doing wrong....

    We are passing the country and if it is Canada we want to show the other column and if it is US we want to show the state drop downlist.



    asp.net:
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="dsLocation" GridLines="None"
                 AllowAutomaticUpdates="True" AllowAutomaticInserts="True"  ShowStatusBar="True"
                AllowSorting="True" OnDataBound="RadGrid1_DataBound" Skin="Default"
                OnItemCommand="RadGrid1_ItemCommand">

                <PagerStyle Mode="NextPrevAndNumeric" />
                <MasterTableView DataKeyNames="testid" DataSourceID="dsLocation" AutoGenerateColumns="false"
                    CommandItemDisplay="Top" AllowMultiColumnSorting="True">
                    <CommandItemSettings AddNewRecordText="Add new record" />
     
                    <ExpandCollapseColumn Visible="True">
                    </ExpandCollapseColumn>
                    <Columns>
                    <telerik:GridEditCommandColumn ButtonType="ImageButton">
                            <HeaderStyle Width="20px" />
                            <ItemStyle CssClass="MyImageButton" />
                        </telerik:GridEditCommandColumn>    

                        <telerik:GridTemplateColumn HeaderText="State" SortExpression="StateID" UniqueName="TemplateColumn2" Display="True"
                            EditFormColumnIndex="0">
                            <EditItemTemplate>
                                <br />
                                <asp:dropdownlist DataField="StateID" id="ddlStateID" runat="server" DataSourceID="dsStates" DataTextField="StateCode" DataValueField="StateID" AppendDataBoundItems="true"
                                SelectedValue='<%# Bind("StateID") %>' AutoPostBack="True" >
                                <asp:ListItem Selected="True" Text="Please select a state" Value=""/>
                                </asp:dropdownlist>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>                      
                        
                                    
                        <telerik:GridBoundColumn DataField="Other" HeaderText="Other"
                            SortExpression="Other" UniqueName="Other"  Display="true">
                        </telerik:GridBoundColumn>
                        
                    </Columns>
                   <EditFormSettings>
                        <EditColumn ButtonType="ImageButton" />
                    </EditFormSettings>
                </MasterTableView>
            </telerik:RadGrid>


    code:
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (Request.QueryString["Canada"] == "Y")
                {
                    RadGrid1.MasterTableView.GetColumn("Other").Visible = true;
                    RadGrid1.MasterTableView.GetColumn("TemplateColumn2").Visible = false;
                    RadGrid1.MasterTableView.Rebind();


                }
                else
                {
                    RadGrid1.MasterTableView.GetColumn("Other").Visible = false;
                    RadGrid1.MasterTableView.GetColumn("TemplateColumn2").Visible = true;
                    RadGrid1.MasterTableView.Rebind();
                }
            }



        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            foreach (GridColumn col in RadGrid1.Columns)
            {
                if (col.ColumnType == "GridBoundColumn" && col.UniqueName == "Other")
                {
                    if (e.CommandName == RadGrid.EditCommandName)
                    {
                        (col as GridTemplateColumn).ReadOnly = true;
                        col.Visible = false;
                    }
                    else
                    {
                        (col as GridTemplateColumn).ReadOnly = false;
                        col.Visible = true;
                    }
                }
            }
        }




    Any insight would be greatly appreciated....
    Julie
  2. Eyup
    Admin
    Eyup avatar
    3694 posts

    Posted 22 Jun 2012 Link to this post

    Hi Julie,

    Please try the following approach:
    protected void RadGrid1_PreRender(object sender, EventArgs e)
       {
           string text = "Canada";
     
           if (text == "Canada")
           {
               RadGrid1.MasterTableView.GetColumn("ShipCountry").Visible = true;
               RadGrid1.MasterTableView.GetColumn("ShipName").Visible = false;
           }
           else
           {
               RadGrid1.MasterTableView.GetColumn("ShipCountry").Visible = false;
               RadGrid1.MasterTableView.GetColumn("ShipName").Visible = true;
           }
       }

    That should do the trick.

    Greetings,
    Eyup
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. Julie
    Julie avatar
    2 posts
    Member since:
    Jun 2012

    Posted 23 Jun 2012 Link to this post

    I need to hide the column when editing as well - the visible appears to only hide it from the grid but now when editing...
  4. Eyup
    Admin
    Eyup avatar
    3694 posts

    Posted 25 Jun 2012 Link to this post

    Hello Julie,

    Please try using the following property:
    (RadGrid1.MasterTableView.GetColumn("ShipName") as GridBoundColumn).ReadOnly = true;

    An alternative approach would be:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridEditableItem && e.Item.IsInEditMode)
           {
               GridEditableItem editedItem = e.Item as GridEditableItem;
               editedItem["ShipName"].Parent.Visible = false;
           }
       }

    That should do the trick.

    Greetings,
    Eyup
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top