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

Grid column custom filtering dynamic

1 Answer 173 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 27 Sep 2011, 02:46 AM
I am having problems when adding two columns to a grid which rely on filtering though a combobox. The majority of the examples and support for custom filtering on a RadGrid is through the markup (.aspx). This does not apply for me because I am generating the RadGrid completely dynamically in the code-behind.

I have successfully created a custom filtering on a column. Please find below the snippet which loads the AssignedTo column. The AssignedTo column has a combobox filter implemented through AssignedToCustomBoundFilteringColumn.

var assignedToCol = new AssignedToCustomBoundFilteringColumn();
assignedToCol.DataField = "AssessmentManagerUser.FullName";
assignedToCol.HeaderText = "Assigned To";               
assignedToCol.UniqueName = "AssignedTo";
assignedToCol.SortExpression = "AssessmentManagerUser.FullName";
 
PlaApplicationsGrid.MasterTableView.Columns.Add(assignedToCol);

See below for the implementation of the AssignedToCustomBoundFilteringColumn
private class AssignedToCustomBoundFilteringColumn : GridBoundColumn
{
    protected override void SetupFilterControls(TableCell cell)
    {
        RadComboBox rcBox = new RadComboBox();
        rcBox.ID = "AssignedToFilterDropDownList";
        rcBox.AutoPostBack = true;
        rcBox.DataTextField = "FullName";
        rcBox.DataValueField = "FullName";
        rcBox.SelectedIndexChanged += rcBox_SelectedIndexChanged;
        rcBox.ItemDataBound += new RadComboBoxItemEventHandler(rcBox_ItemDataBound);
 
        var plaOfficers = ApplicationUserManager.Instance.GetBySecurityRoles(new string[] { KnownRoleNames.PLAOfficer }) as List<ApplicationUser>;
 
        var unassignedUser = ApplicationUserManager.Instance.GetUnassigned();
 
        ApplicationUser tempEmptyUser = new ApplicationUser();
        tempEmptyUser.Person = new Person();
        tempEmptyUser.Person.FirstName = "empty";
        tempEmptyUser.Person.Surname = "";
 
        plaOfficers.Insert(0, unassignedUser);
        plaOfficers.Insert(0, tempEmptyUser);
 
        rcBox.DataSource = plaOfficers;
        rcBox.DataBind();
 
        //rcBox.Items[0].Text = "Unassigned";
 
        cell.Controls.Add(rcBox);
    }           
 
    private void rcBox_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
    {
        // Need this unless you want the item text to say "Unassignred Unassigned"
        if (e.Item.Text == ApplicationUserManager.Instance.GetUnassigned().FullName)
        {
            e.Item.Text = ApplicationUserManager.Instance.GetUnassigned().Username;
        }
        else if (e.Item.Text == "empty ")
        {
            e.Item.Text = " ";
        }
    }
 
    protected override void SetCurrentFilterValueToControl(TableCell cell)
    {
        if (!(this.CurrentFilterValue == ""))
        {
            ((RadComboBox)cell.Controls[0]).Items.FindItemByValue(this.CurrentFilterValue).Selected = true;
        }
    }
 
    protected override string GetCurrentFilterValueFromControl(TableCell cell)
    {
        string currentValue = ((RadComboBox)cell.Controls[0]).SelectedItem.Value;
        this.CurrentFilterFunction = (currentValue != "" && currentValue != "empty ") ? GridKnownFunction.EqualTo : GridKnownFunction.NoFilter;
        return currentValue;
    }
 
    private void rcBox_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        ((GridFilteringItem)(((RadComboBox)sender).Parent.Parent)).FireCommandEvent("Filter", new Pair());
    }
}

This is working fine. But I have since created a new column, which is basically an identical implementation
var stateCol = new StateCustomBoundFilteringColumn(); // AssignedTo column filtering breaks when this column is introduced
//var stateCol = new GridBoundColumn();
stateCol.DataField = "State.Description";
stateCol.HeaderText = "State";
stateCol.UniqueName = "State";
stateCol.SortExpression = "State.Description";
PlaApplicationsGrid.MasterTableView.Columns.Add(stateCol);

private class StateCustomBoundFilteringColumn : GridBoundColumn
{
    protected override void SetupFilterControls(TableCell cell)
    {
        RadComboBox rcBox = new RadComboBox();
        rcBox.ID = "StateFilterDropDownList";
        rcBox.AutoPostBack = true;
        rcBox.DataTextField = "Description";
        rcBox.DataValueField = "Name";
        rcBox.SelectedIndexChanged += rcBox_SelectedIndexChanged;
 
        var validStates = ApplicationStateManager.Instance.GetAllMasterBuildStates();
 
        ApplicationState tempEmptyState = new ApplicationState();
        tempEmptyState.Name = "empty";
        tempEmptyState.Description = "";
 
        validStates.Insert(0, tempEmptyState);
 
        rcBox.DataSource = validStates;
        rcBox.DataBind();
 
        //rcBox.Items[0].Text = "Unassigned";
 
        cell.Controls.Add(rcBox);
    }
  
    protected override void SetCurrentFilterValueToControl(TableCell cell)
    {
        if (!(this.CurrentFilterValue == ""))
        {
            ((RadComboBox)cell.Controls[0]).Items.FindItemByValue(this.CurrentFilterValue).Selected = true;
        }
    }
 
    protected override string GetCurrentFilterValueFromControl(TableCell cell)
    {
        string currentValue = ((RadComboBox)cell.Controls[0]).SelectedItem.Value;
        this.CurrentFilterFunction = (currentValue != "" && currentValue != "empty ") ? GridKnownFunction.EqualTo : GridKnownFunction.NoFilter;
        return currentValue;
    }
 
    private void rcBox_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        ((GridFilteringItem)(((RadComboBox)sender).Parent.Parent)).FireCommandEvent("Filter", new Pair());
    }
}

N.B. I have implemented the ColumnCreating event handler

I am at a loss why introducing the State filtering stuffs up the AssignedTo filtering.

1 Answer, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 28 Sep 2011, 11:14 AM
Hi Andy,

On which event are you creating the grid? I suggest that you create the entire grid in Page_Init following the steps here and leave its ViewState and ColumnViewSate enabled.

Greetings,
Iana Tsolova
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
Grid
Asked by
Paul
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or