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

RadComboBox in RadGrid Filter Template

2 Answers 639 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric Klein
Top achievements
Rank 1
Eric Klein asked on 15 Sep 2010, 09:10 PM
I have a grid that is being populated OnNeeddDataSource
<telerik:RadGrid runat="server" ID="RadGrid1" AllowPaging="True" AllowSorting="True"
 PageSize="20" AllowFilteringByColumn="True" EnableViewState="False"  AutoGenerateColumns="False" Skin="Office2007" GridLines="None" OnItemCreated="RadGrid1_ItemCreated" OnPreRender="RadGrid1_PreRender"
OnNeedDataSource="PC_NeedDataSource">

In this grid the client column has a Filter Template that is a RadComboBox

<telerik:GridBoundColumn SortExpression="ClientName" DataField="ClientName" HeaderText="Client Name"                                               HeaderStyle-Width="300px">
     <FilterTemplate>
           <telerik:RadComboBox ID="RadComboBoxClientName" DataTextField="ClientName" DataValueField="ClientName"                                                      AppendDataBoundItems="true" SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("ClientName").CurrentFilterValue %>'    runat="server" OnClientSelectedIndexChanged="ClientNameIndexChanged" Skin="Office2007">
     </telerik:RadComboBox>
     <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
        <script type="text/javascript">
             function ClientNameIndexChanged(sender, args) {
               var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
               if (args.get_item().get_value() == "0") {             tableView.filter("ClientName", args.get_item().get_value(), "NoFilter");
         }
    else {
           tableView.filter("ClientName", args.get_item().get_value(), "EqualTo");
         }
   }
 </script> </telerik:RadScriptBlock>
</FilterTemplate>
</telerik:GridBoundColumn>

This all works fine except when I try to sort on the grid when I have set a filter.  The data in the grid is correct but the DropDown gets changed to *** ALL ***.

I have tried the following code to select the by value
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
       {
            
               if (e.Item is GridFilteringItem)
               {
                   GridFilteringItem filterItem = (GridFilteringItem)e.Item;
                   RadComboBox combo = (RadComboBox)filterItem["ClientName"].FindControl("RadComboBoxClientName");
                   if (Session["AdvisorClientID"] == null)
                   {
                       ClientDataContext db = new ClientDataContext();
                       var clients = (from c in db.Clients
                                      select new
                                      {
                                          c.ClientID,
                                          c.ClientName,
                                      })
                                     .OrderBy(c => c.ClientName);
                       combo.DataSource = clients;
                       RadComboBoxItem newItem = new RadComboBoxItem();
                       newItem.Text = "*** ALL ***";
                       newItem.Value = "0";
                       combo.Items.Insert(0, newItem);
                         
                       int test = RadGrid1.MasterTableView.FilterExpression.IndexOf("ClientName = \"");
                       if (test > 0)
                       {
                           string clientName = RadGrid1.MasterTableView.FilterExpression.Replace("(ClientName = \"", "");
                           clientName = clientName.Replace("\")", "");
                           combo.SelectedValue = clientName;// combo.FindItemByValue(clientName);
                       }
                         
                   }
                   else
                   {
                       combo.Visible = false;
                   }
               }
           }

Is there a way to keep the selected value in the combobox when the grid gets reloaded on sort?

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 18 Sep 2010, 11:18 AM
Hello Eric,


I hope resetting the SelectedValue in PreRender event is better approach. Check the following forum link which discussed similar scenario.
How to persist DropDownList selected index and value on postback from RadGrid FilterTemplate


-Shinu.
0
Brad H
Top achievements
Rank 2
answered on 23 Jan 2011, 11:38 PM
I have read all of the replies to this topic and none really seem worthwhile.  Pre_Render may work for some but it did not for me.  I tried ViewState and Session on both the RadComboBox inside my FilterTemplate and the RadGrid itself.  The SelectedIndex/Value never persisted.  I chose instead to simply embrace the lack of functionality in this case and use a blank entry as my top selection.  This way, every time the user makes a selection, the index is changed and your events are fired.  It seems this is the way the RadComboBox was intended because it always resets to the top value and when you click the top value, no postback is fired.  Anyway, here's my code in case anyone wants to try something similar.

ASPX:  I used an "All" item in the RadComboBox DataSource and an Empty RadComboBoxItem.  I use the "All" to display all results in code behind.
<telerik:RadGrid ID="rgCallTagHistory" runat="server" AutoGenerateColumns="false"
            AllowFilteringByColumn="true" AllowSorting="true" Skin="Office2007" AllowMultiRowSelection="false"
            Width="750px" ShowFooter="false" PageSize="10" AllowPaging="true" Height="400px">
            <ClientSettings>
                <Scrolling UseStaticHeaders="true" AllowScroll="true" />
            </ClientSettings>
            <GroupingSettings CaseSensitive="false" />
                <Columns>
                    <telerik:GridBoundColumn ItemStyle-Wrap="false" ItemStyle-Width="75px" HeaderStyle-Width="75px"
                        AllowFiltering="true" DataType="System.String" ItemStyle-HorizontalAlign="Center"
                        HeaderStyle-HorizontalAlign="Center" DataField="AccessType" HeaderText="Type of Access"
                        UniqueName="AccessType">
                        <FilterTemplate>
                            <telerik:RadComboBox ID="comboAccessType" runat="server" DataSourceID="XmlDataSource1"
                                DataTextField="Text" DataValueField="Value" AutoPostBack="true" OnSelectedIndexChanged="comboAccessType_SelectedIndexChanged"
                                Width="75px" AppendDataBoundItems="true">
                                <Items>
                                    <telerik:RadComboBoxItem Text="" Value="" />
                                </Items>
                            </telerik:RadComboBox>
                        </FilterTemplate>
                    </telerik:GridBoundColumn>
               </Columns>
        </telerik:RadGrid>
      <asp:XmlDataSource ID="XmlDataSource1" runat="server">
            <Data>
                <Items>
                    <Item Value="All" Text="All"></Item>
                    <Item Value="Archive" Text="Archive"></Item>
                    <Item Value="Edit" Text="Edit"></Item>
                    <Item Value="Play" Text="Play"></Item>
                </Items>
            </Data>
        </asp:XmlDataSource>

ASPX.VB
Public Sub comboAccessType_SelectedIndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)
        Dim filterExpression As String
        If e.Value = "All" Then
            filterExpression = ""
        Else
            filterExpression = "([AccessType] LIKE '" + e.Value + "')"
        End If
        rgCallTagHistory.MasterTableView.FilterExpression = filterExpression
        rgCallTagHistory.Rebind()
    End Sub
Tags
Grid
Asked by
Eric Klein
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Brad H
Top achievements
Rank 2
Share this question
or