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

RadGridView and DateTimeOffset

1 Answer 292 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Frans
Top achievements
Rank 1
Frans asked on 06 Dec 2011, 08:08 PM
Hi,

I have a user control with RadGridView bound to the database table.

The table has a DateTimeOffset column.

It seems that the filter doesnt function properly with this DateTimeOffset datatype (the Date time picker in the filter window of the Grid View Column filter doesnt show up, and the options for filtering is constricted to only isEqualTo and IsNotEqualTo. Other options are not available).

I tried binding the columns with DateTime datatype where ever I could bind the grid to a Object (or Property) which is in turn bound to the database column.

But, where ever I need a direct binding to the database, I am not able to do this.

Can you please suggest any alternate solution?

Thanks and regards

Pavan

1 Answer, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 07 Dec 2011, 12:16 PM
Hi Frans,

The RadDateTimePicker shows up only for the DateTime data type. For all other types you will get a plain TextBox. You can't enter DateTimeOffset's through the RadDateTimePicker.

Now, for custom scenarios, the developer can specify his very own custom editor to be shown in the lower part of the filtering control. This is done by inheriting from GridViewBoundColumnBase and overriding the CreateFieldFilterEditor method. There you can supply any editor for any type. You only need to create a binding which connects the editor's value to the property named "Value" on our view model which is the DataContext for this editor. Here is an excerpt of our code that shows how we create our default editors. You will have to bind your custom editor in a similar manner:

/// <summary>
  
/// Creates the string editor.
  
/// </summary>
  
/// <returns></returns>
  
publicstaticFrameworkElement CreateStringEditor()
  
{
  
    var stringFilterEditor = newStringFilterEditor();
  
   
  
    var textBinding = newBinding("Value")
  
    {
  
        Mode = BindingMode.TwoWay,
  
        FallbackValue = string.Empty,
  
        Converter = newStringFilterEditorConverter()
  
    };
  
    stringFilterEditor.SetBinding(StringFilterEditor.TextProperty, textBinding);
  
   
  
    var isCaseSensitiveBinding = newBinding("IsCaseSensitive")
  
    {
  
        Mode = BindingMode.TwoWay,
  
        FallbackValue = false,
  
    };
  
    stringFilterEditor.SetBinding(StringFilterEditor.IsCaseSensitiveProperty, isCaseSensitiveBinding);
  
   
  
    returnstringFilterEditor;
  
}
  
   
  
/// <summary>
  
/// Creates the date time editor.
  
/// </summary>
  
/// <returns></returns>
  
publicstaticFrameworkElement CreateDateTimeEditor()
  
{
  
    var dateTimePicker = newRadDateTimePicker();
  
    dateTimePicker.InputMode = InputMode.DatePicker;
  
    dateTimePicker.IsTooltipEnabled = false;
  
   
  
    var selectedValueBinding = newBinding("Value")
  
    {
  
        Mode = BindingMode.TwoWay,
  
        FallbackValue = null,
  
        Converter = newDateTimeFilterEditorConverter()
  
    };
  
    dateTimePicker.SetBinding(RadDateTimePicker.SelectedValueProperty, selectedValueBinding);
  
   
  
    returndateTimePicker;
  
}
  
   
  
/// <summary>
  
/// Creates the boolean editor.
  
/// </summary>
  
/// <param name="type">The type.</param>
  
/// <returns></returns>
  
publicstaticFrameworkElement CreateBooleanEditor(Type type)
  
{
  
    var checkBox = newCheckBox();
  
    checkBox.IsThreeState = type == typeof(bool?);
  
       
  
    var isCheckedBinding = newBinding("Value")
  
    {
  
        Mode = BindingMode.TwoWay,
  
        FallbackValue = false
  
    };
  
    checkBox.SetBinding(CheckBox.IsCheckedProperty, isCheckedBinding);
  
   
  
    returncheckBox;
  
}
  
   
  
/// <summary>
  
/// Creates the enum editor.
  
/// </summary>
  
/// <param name="type">The type.</param>
  
/// <returns></returns>
  
publicstaticFrameworkElement CreateEnumEditor(Type type)
  
{
  
    var comboBox = newRadComboBox();
  
   
  
    comboBox.SelectedValuePath = "Value";
  
    comboBox.DisplayMemberPath = "DisplayName";
  
    comboBox.ItemsSource = EnumDataSource.FromType(type);
  
       
  
    var selectedItemBinding = newBinding("Value")
  
    {
  
        Mode = BindingMode.TwoWay,
  
        FallbackValue = null
  
    };
  
    comboBox.SetBinding(RadComboBox.SelectedValueProperty, selectedItemBinding);
  
   
  
    returncomboBox;
  
}
  
   
  
/// <summary>
  
/// Creates the default editor.
  
/// </summary>
  
/// <returns></returns>
  
publicstaticFrameworkElement CreateDefaultEditor()
  
{
  
    var textBox = newTextBox();
 
   
  
    var textBinding = newBinding("Value")
  
    {
  
        Mode = BindingMode.TwoWay,
  
        FallbackValue = null,
  
        Converter = newStringFilterEditorConverter()
  
    };
  
    textBox.SetBinding(TextBox.TextProperty, textBinding);
  
   
  
    TextBoxBehavior.SetUpdateTextOnEnter(textBox, true);
  
    TextBoxBehavior.SetSelectAllOnGotFocus(textBox, true);
  
   
  
    returntextBox;
  
}
  
   
  
privateclassDateTimeFilterEditorConverter : IValueConverter
  
{
  
    publicobjectConvert(objectvalue, Type targetType, objectparameter, System.Globalization.CultureInfo culture)
  
    {
  
        if(value == FilterDescriptor.UnsetValue)
  
        {
  
            returnnull;
  
        }
  
   
  
        returnvalue;
  
    }
  
   
  
    publicobjectConvertBack(objectvalue, Type targetType, objectparameter, System.Globalization.CultureInfo culture)
  
    {
  
        if(value == null)
  
        {
  
            returnFilterDescriptor.UnsetValue;
  
        }
  
   
  
        returnvalue;
  
    }
  
}
  
   
  
privateclassStringFilterEditorConverter : IValueConverter
  
{
  
    publicobjectConvert(objectvalue, Type targetType, objectparameter, System.Globalization.CultureInfo culture)
  
    {
  
        if(value == FilterDescriptor.UnsetValue || value == null)
  
        {
  
            returnstring.Empty;
  
        }
  
   
  
        returnvalue.ToString();
  
    }
  
   
  
    publicobjectConvertBack(objectvalue, Type targetType, objectparameter, System.Globalization.CultureInfo culture)
  
    {
  
        if(string.IsNullOrEmpty(value asstring))
  
        {
  
            returnFilterDescriptor.UnsetValue;
  
        }
  
           
  
        returnvalue;
  
    }
  
}

So if you want to, you can create a new custom editor which should be bound to the "Value" property in the same manner.

Alternatively, if can work with DateTime's if you want to use the default functionality.

I hope this helps.

Greetings,
Ross
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Frans
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Share this question
or