CustomFilter with different DataValueField and DataTextField values

2 posts, 0 answers
  1. Timo
    Timo avatar
    5 posts
    Member since:
    Jan 2011

    Posted 07 Dec 2011 Link to this post

    I am having a problem with a custom filter editor when DataKeyValue and DataTextValue differe.

    Most of the code I am using was taken from the custom field editors demo.
    Filtering the related grid works fine but the text field is showing the wrong text after a click on the "Apply" button.
    If I click on "Add Expression" I get a filled combo box where I can select the order type by name.
    Clicking on "Apply" filters the grids contents as expected. But the reacently added expression shows the integer value in it's text field.
    I would like to show the localized text out of the combo box.

    This is the filter extracted from the page.
       <telerik:RadFilter ID="Filter" runat="server" FilterContainerID="OrderListGrid"
            OnFieldEditorCreating="Filter_FieldEditorCreating"
           CssClass="RadFilter RadFilter_Default " >
          <FieldEditors>
          <csc:RadFilterDropDownEditor FieldName="Type" DataValueField="Type" DataTextField="Name" DataType="System.Int32" />
          <telerik:RadFilterTextFieldEditor FieldName="Order" DataType="System.String" />
          <telerik:RadFilterDateFieldEditor FieldName="Start" DataType="System.DateTime" />
          <telerik:RadFilterDateFieldEditor FieldName="End" DataType="System.DateTime" />
           </FieldEditors>
          </telerik:RadFilter>
    OrderListGrid is a RadGrid using a sql data source to a simple table.
    This table has a column "Type" which stores the order types as an integer value.
    This integer value should not be shown to the customer, but replaced by a localized text within the filter combo box.
    Therefore I've created a DataTable and assigned it to the custom filter editor as in the demo.
     
    private void InitializeFilterCtrl()
    {
         RadFilterDropDownEditor dropTypFilter = Filter.FieldEditors[0] as RadFilterDropDownEditor;
      if (OrderTypesTable == null)
      {
        OrderTypesTable = GetOrderTypeTable();
      }
      dropTypFilter.DataSource = OrderTypesTable;
    }

    private DataTable GetOrderTypeTable()
    {
      DataTable tab = new DataTable();
      tab.Columns.Add("Type");
      tab.Columns.Add("Name");
      tab.Rows.Add( new object [] { (int)OrderType.Final, LocalizationUtils.GetOrderName(OrderType.Final) });
      tab.Rows.Add(new object[] { (int)OrderType.Intermediate, LocalizationUtils.GetOrderName(OrderType.Intermediate) });
      tab.Rows.Add(new object[] { (int)OrderType.Piece, LocalizationUtils.GetOrderName(OrderType.Piece) });<br>  tab.Rows.Add(new object[] { (int)OrderType.Batch, LocalizationUtils.GetOrderName(OrderType.Batch) });
      return tab;
    }
     LocalizationUtils.GetOrderName(..) just returns the localized text out of the resources.

    public class RadFilterDropDownEditor : RadFilterDataFieldEditor
    {
        protected override void CopySettings(RadFilterDataFieldEditor baseEditor)
        {
            base.CopySettings(baseEditor);
            var editor = baseEditor as RadFilterDropDownEditor;
            if (editor != null)
            {
                DataSource = editor.DataSource;
                DataTextField = editor.DataTextField;
                DataValueField = editor.DataValueField;
            }
        }
     
        public override System.Collections.ArrayList ExtractValues()
        {
            ArrayList list = new ArrayList();
            list.Add(_combo.SelectedValue);
            return list;
        }
     
        public override void InitializeEditor(System.Web.UI.Control container)
        {
            _combo = new RadComboBox();
            _combo.ID = "MyCombo";
            _combo.DataTextField = DataTextField;
            _combo.DataValueField = DataValueField;
            _combo.DataSource = DataSource;          
            _combo.DataBind();
            container.Controls.Add(_combo);            
        }
     
        public override void SetEditorValues(System.Collections.ArrayList values)
        {
          if (values != null && values.Count > 0)
          {
            if (values[0] == null)
              return;
            var item = _combo.FindItemByValue(values[0].ToString());
            if (item != null)
              item.Selected = true;
          }
        }
     
        public string DataTextField
        {
            get
            {
                return (string)ViewState["DataTextField"] ?? string.Empty;
            }
            set
            {
                ViewState["DataTextField"] = value;
            }
        }
        public string DataValueField
        {
            get
            {
                return (string)ViewState["DataValueField"] ?? string.Empty;
            }
            set
            {
                ViewState["DataValueField"] = value;
            }
        }
        public DataTable DataSource
        {
            get
            {
                return (DataTable)ViewState["DataSource"] ?? new DataTable();
            }
            set
            {
                ViewState["DataSource"] = value;
            }
        }
     
        private RadComboBox _combo;
    }

    Any suggestions?

  2. Antonio Stoilkov
    Admin
    Antonio Stoilkov avatar
    530 posts

    Posted 12 Dec 2011 Link to this post

    Hi Timo,

    The issue really exists and I logged it for fixing in our bug tracking system, as well as updated your Telerik points. Here you can find the PITS Issue: Public URL and check its status.

    However, you could achieve your scenario by following the steps below:
    • Set the RadGrid EnableLinqExpressions="false"
    • Subscribe to the RadFilter ApplyExpressions event
    • Use RadFilterSqlQueryProvider to the build the FilterExpression for the RadGrid
    Additionally, you could go through the demo below guiding you through the process:

    Regards,
    Antonio Stoilkov
    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top