Persisting selected value of radcombobox inside radgrid on sorting

4 posts, 0 answers
  1. Kavita
    Kavita avatar
    39 posts
    Member since:
    Feb 2013

    Posted 20 Feb 2013 Link to this post

    Hi,
    I have a grid in which there is template column with radcombobox in it.
    Radcombobox has 4 items it in.
    When user selects any item from list and then sort any column of grid, that selected value disappears.
    I want to persist that combobox value and show it again as it was before sorting.
    Please help me with this.
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Feb 2013 Link to this post

    Hi Kavita,

    The following code snippet shows how to achieve this.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" AllowMultiRowSelection="true" DataSourceID="SqlDataSource1"
               AllowSorting="true" runat="server" OnPreRender="RadGrid1_PreRender">
               <MasterTableView ClientDataKeyNames="EmployeeID">
                     <Columns>
                       <telerik:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" UniqueName="EmployeeID">
                       </telerik:GridBoundColumn>
                       <telerik:GridTemplateColumn UniqueName="CheckRows" HeaderStyle-Width="42px">
                           <ItemTemplate>
                               <telerik:RadComboBox runat="server" ID="RadComboBox1" DataSourceID="SqlDataSource1"
                                   AutoPostBack="true" DataTextField="FirstName" DataValueField="FirstName" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged">
                               </telerik:RadComboBox>
                           </ItemTemplate>
                         </telerik:GridTemplateColumn>
                   </Columns>
               </MasterTableView>
           </telerik:RadGrid>

    C#:
    protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
       {
           ArrayList selectedItems;
           if (Session["selectedItems"] == null)
           {
               selectedItems = new ArrayList();
           }
           else
           {
               selectedItems = (ArrayList)Session["selectedItems"];
           }
           RadComboBox comboBox = (RadComboBox)sender;
           GridDataItem item = (GridDataItem)comboBox.NamingContainer;
           string customerID = item.OwnerTableView.DataKeyValues[item.ItemIndex]["EmployeeID"].ToString();
           string selectedValue = comboBox.SelectedValue;
           selectedItems.Add(customerID + ',' + selectedValue);
           Session["selectedItems"] = selectedItems;
       }
     
       protected void RadGrid1_PreRender(object sender, EventArgs e)
       {
           if (Session["selectedItems"] != null)
           {
               ArrayList selectedItems = (ArrayList)Session["selectedItems"];
               Int16 stackIndex;
               for (stackIndex = 0; stackIndex <= selectedItems.Count - 1; stackIndex++)
               {
                   string curItem = selectedItems[stackIndex].ToString().Split(',')[0];
                   foreach (GridItem item in RadGrid1.MasterTableView.Items)
                   {
                       if (item is GridDataItem)
                       {
                           GridDataItem dataItem = (GridDataItem)item;
                           if (curItem.Equals(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["EmployeeID"].ToString()))
                           {
                               RadComboBox comboBox = (RadComboBox)dataItem.FindControl("RadComboBox1");
                               comboBox.SelectedValue = selectedItems[stackIndex].ToString().Split(',')[1];
                               break;
                           }
                       }
                   }
               }
           }
       }

    Note:- I have used the logic in this help article to achieve this.

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kavita
    Kavita avatar
    39 posts
    Member since:
    Feb 2013

    Posted 21 Feb 2013 Link to this post

    hi shinu.
    My radcombobox does not have datasource assigned to it instead it is like this

    <telerik:GridTemplateColumn  UniqueName ="ServiceRequestOptions"  AllowFiltering ="false" HeaderText ="Service Request" Visible="true" ItemStyle-Wrap ="false"   >
     <ItemTemplate>
             <telerik:RadComboBox ID="cbServiceRequestOptions" runat="server" MarkFirstMatch="True"  Width ="180px" EnableLoadOnDemand ="true"  HighlightTemplatedItems="True"  Skin="Default" SortCaseSensitive="False"  Height="60px" DropDownWidth ="180px"  EmptyMessage ="-Select-"   OnClientSelectedIndexChanged="OnClientSelectedIndexChanged" ChangeTextOnKeyBoardNavigation ="true" OnClientDropDownClosing ="OnClientDropDownClosing" Font-Names="Arial" Font-Size="Small">
                          <Items >
                            <telerik:RadComboBoxItem  Text ="Startup" Value ="Startup" Font-Names="Arial" Font-Size="Small"/>
                            <telerik:RadComboBoxItem  Text ="Preventative Maintenance" Value ="Preventative Maintenance" Font-Names="Arial" Font-Size="Small"/>
                             <telerik:RadComboBoxItem  Text ="Repair" Value ="Repair" Font-Names="Arial" Font-Size="Small"/>
                             <telerik:RadComboBoxItem  Text ="Upgrade" Value ="Upgrade" Font-Names="Arial" Font-Size="Small"/>
                             <telerik:RadComboBoxItem  Text ="Other" Value ="Other" Font-Names="Arial" Font-Size="Small"/>
                            </Items>
          </telerik:RadComboBox>
      </ItemTemplate>
       <HeaderStyle Wrap="False" Width ="180px" HorizontalAlign="Center"/>
       <ItemStyle Wrap="False" Width ="180px" HorizontalAlign="Center" VerticalAlign="Middle" />
     </telerik:GridTemplateColumn>

    And i want to avoid use of session variable.
    The link you provided is showing a blank page.
    My only problem is when user selects any item from radcombobox and then do any sorting,the value should be shown.
    Please help
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Feb 2013 Link to this post

    Hi Kavitha,

    The code that I provided will work with your RadComboBox as well. [ It doesnt matter the way we bind the RadComboBox here]. But please try to attach the serverside SelectedIndexChanged event and AutoPostBack="true" for the RadComboBox.

    I am not sure why the provided link is not working for you. Can you please try the following url.
    http://www.telerik.com/help/aspnet-ajax/grid-persist-selected-rows-on-sorting.html

    And instead of session variable , you can use ViewState variable also. Please let me know if you need further help.

    Thanks,
    Shinu.
Back to Top