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

Hide columns in radgrid based on querystring

3 Answers 99 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Julie
Top achievements
Rank 1
Julie asked on 21 Jun 2012, 06:37 PM
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

3 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 22 Jun 2012, 01:45 PM
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.
0
Julie
Top achievements
Rank 1
answered on 23 Jun 2012, 12:10 PM
I need to hide the column when editing as well - the visible appears to only hide it from the grid but now when editing...
0
Eyup
Telerik team
answered on 25 Jun 2012, 02:13 PM
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.
Tags
General Discussions
Asked by
Julie
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Julie
Top achievements
Rank 1
Share this question
or