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

Retaining Filter values in the radcombobox

1 Answer 145 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rachelle
Top achievements
Rank 1
Rachelle asked on 14 Jan 2013, 05:37 PM
Hi all,

This is my first posting here and I could use your help.  I am trying to figure out how to keep my values when using a radcombo box when filtering my grid.  Each time I select an option, the selected value always go back to the first in the list.  So I added some code in the cs file to retain the value, but I get an error.  I could use your expertise!  I have cut/pasted the code and the error message below.

Here is the Aspx:

 

<telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" DataSourceID="FastTrackDataSource" GridLines="None" Skin="Outlook" AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" OnItemDataBound="RadGrid1_ItemDataBound" EnableLinqExpressions="false">
        <ClientSettings>
            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
        </ClientSettings>
        <MasterTableView AutoGenerateColumns="False" DataKeyNames="ID,PersonId,LastName,FirstName,PositionId,BduId,bureau_name,division_name,unit_name,Status" DataSourceID="FastTrackDataSource">
            <CommandItemSettings ExportToPdfText="Export to PDF">
            </CommandItemSettings>
            <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                <HeaderStyle Width="20px"></HeaderStyle>
            </RowIndicatorColumn>
            <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                <HeaderStyle Width="20px"></HeaderStyle>
            </ExpandCollapseColumn>
            <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn DataField="EmployeeId" FilterControlAltText="Filter EmployeeId column" HeaderText="Employee ID" SortExpression="EmployeeId" UniqueName="EmployeeId" DataType="System.Int32" ReadOnly="true">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="LastName" FilterControlAltText="Filter LastName column" HeaderText="Last Name" SortExpression="LastName" UniqueName="LastName" ReadOnly="True">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="FirstName" FilterControlAltText="Filter FirstName column" HeaderText="First Name" ReadOnly="True" SortExpression="FirstName" UniqueName="FirstName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="JobTitle" FilterControlAltText="Filter JobTitle column" HeaderText="Job Title" SortExpression="JobTitle" UniqueName="JobTitle">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="division_name" FilterControlAltText="Filter division_name column" HeaderText="Division" ReadOnly="True" SortExpression="division_name" UniqueName="division_name">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="unit_name" FilterControlAltText="Filter unit_name column" HeaderText="Unit" ReadOnly="True" SortExpression="unit_name" UniqueName="unit_name">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Status" DataType="System.Int32" FilterControlAltText="Filter Status column" HeaderText="Status" SortExpression="Status" UniqueName="Status">
                 <FilterTemplate>
                        <telerik:RadComboBox ID="StatusCombo" runat="server" OnSelectedIndexChanged="StatusCombo_SelectedIndexChanged" AutoPostBack="true">
                            <Items>
                                <telerik:RadComboBoxItem Text="All" Value="ALL" />
                                <telerik:RadComboBoxItem Text="Active" Value="1"></telerik:RadComboBoxItem>
                                <telerik:RadComboBoxItem Text="Inactive" Value="0"></telerik:RadComboBoxItem>
                            </Items>
                        </telerik:RadComboBox>
                    </FilterTemplate>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Start_date" DataType="System.DateTime" FilterControlAltText="Filter Start_date column" HeaderText="Start Date" SortExpression="Start_date" UniqueName="Start_date">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="End_date" DataType="System.DateTime" FilterControlAltText="Filter End_date column" HeaderText="End Date" SortExpression="End_date" UniqueName="End_date">
                </telerik:GridBoundColumn>
            </Columns>
            <EditFormSettings>
                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                </EditColumn>
            </EditFormSettings>
        </MasterTableView>
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
    </telerik:RadGrid>
    <asp:EntityDataSource ID="FastTrackDataSource" runat="server" ConnectionString="name=FastTrackEntities" DefaultContainerName="FastTrackEntities" EnableFlattening="False" EntitySetName="CPS_SocialWorkers" EnableUpdate="True">
    </asp:EntityDataSource>

Here is the code-behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CPSTrainingLog.DAL;
using Telerik.Web.UI;
using System.Drawing;
  
namespace CPSTrainingLog
{
    public partial class CPSSocialWorkersListing : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
              
        }
         
        protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                if (item["Status"].Text == "1")
                {
                    item["Status"].Text = "Active";
                    item["Status"].ForeColor = Color.Green;
                }
                else if (item["Status"].Text == "0")
                {
                    item["Status"].Text = "Inactive";
                    item["Status"].ForeColor = Color.Red;
                }
            }
  
        }
  
        protected void StatusCombo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            GridFilteringItem item = RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem;
            RadComboBox radComboBoxFilterStatus = item["Status"].Controls[0] as RadComboBox;
            string filterStatus = Convert.ToString(radComboBoxFilterStatus.SelectedValue);
            RadGrid1.MasterTableView.FilterExpression = string.Empty;
            if (e.Value.Equals("1") || e.Value.Equals("0"))
            {
                RadGrid1.MasterTableView.FilterExpression = RadGrid1.MasterTableView.FilterExpression + string.Format("it.[Status] = {0}", Convert.ToInt32(e.Value));
            }
            GridColumn columnID = RadGrid1.MasterTableView.GetColumnSafe("Status");
            columnID.CurrentFilterValue = filterStatus;
            RadGrid1.Rebind();
             
        }
  
          
  
         
    }
}

and here is the error i get after filtering on the Status column:

Server Error in '/' Application.
--------------------------------------------------------------------------------
  
Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
  
Source Error: 
  
  
Line 41:             GridFilteringItem item = RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem;
Line 42:             RadComboBox radComboBoxFilterStatus = item["Status"].Controls[0] as RadComboBox;
Line 43:             string filterStatus = Convert.ToString(radComboBoxFilterStatus.SelectedValue);
Line 44:             RadGrid1.MasterTableView.FilterExpression = string.Empty;
Line 45:             if (e.Value.Equals("1") || e.Value.Equals("0")) 
  
Source File: c:\Users\huntra\Documents\Visual Studio 2012\Projects\CPSTrainingLog\CPSTrainingLog\CPSSocialWorkersListing.aspx.cs    Line: 43 
  
Stack Trace: 
  
  
[NullReferenceException: Object reference not set to an instance of an object.]
   CPSTrainingLog.CPSSocialWorkersListing.StatusCombo_SelectedIndexChanged(Object sender, RadComboBoxSelectedIndexChangedEventArgs e) in c:\Users\huntra\Documents\Visual Studio 2012\Projects\CPSTrainingLog\CPSTrainingLog\CPSSocialWorkersListing.aspx.cs:43
   Telerik.Web.UI.RadComboBox.OnSelectedIndexChanged() +191
   Telerik.Web.UI.RadComboBox.RaisePostDataChangedEvent() +37
   Telerik.Web.UI.RadDataBoundControl.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +13
   System.Web.UI.Page.RaiseChangedEvents() +132
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1644
   
  
  
--------------------------------------------------------------------------------


1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 15 Jan 2013, 06:05 AM
Hi,

Please try the following code snippet.

C#:
protected void StatusCombo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    RadComboBox combo = (RadComboBox)sender;
    GridFilteringItem item = (GridFilteringItem)combo.NamingContainer;
    string filterStatus = (combo.SelectedValue).ToString();
    //your code           
}

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