but when I run that project drop down list looks too small to hold its content. Please look at attached screenshot.
Could you advise me how to properly calculate size of dropdown, or in other way make it fit its content. The only way I found is to set
this.dropDown.MinSize = new Size(150, 30);
inside CreateChildElements method. But I'm not sure this is the best way to fix this issue. Could you please advise me how to handle those kinds of size issues?
2 Answers, 1 is accepted
Hello Ruslan,
Thank you for reaching out to us.
It seems to me that the approach described in the article is used as a starting point. I am assuming that you have your own implementation as I have tested the code and the custom input control is stretched. What I am guessing is happening is that the dropdown is not stretched. You can try setting the StretchHorizontally property of the RadDropDownListElement to true.
If that does not help, may I ask you to share the custom cell and any other code that targets the custom cell? This way I can try to add your code to a test project and debug it. If you can isolate your approach in a sample project, that will be even better.
Regards,
Dinko | Tech Support Engineer
Progress Telerik
Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.
Hello, thanks for your fast response, I haven't done anything fancy yet, just check outed code from
winforms-sdk/GridView/PermanentDropDownListEditorInFilterCell at master · telerik/winforms-sdk · GitHub
then change reference to my version of telerik (from. C:\Program Files (x86)\Progress\Telerik UI for WinForms 2024 Q3\Bin462)
Telerik.WinControls.UI, Version=2024.3.924.462, Culture=neutral, PublicKeyToken=5bb2a467cbec794e - that's the version
and just run the project.
As per your recomendation I've tried to set StretchHorizontally
property but it seems to have no effect.
Thank you for the provided project.
I was able to observe the described behavior using the default code. It turns out that the demo code is outdated. With one of the latest versions of our controls, we have made some improvements in how the RadDropDownList is measured and that is why in the example, the control size is small. You will need to manually arrange the control in the available size of the custom filter cell.
To do that we will need to override the ArrangeOverride() method of the custom filter cell and arrange the custom elements in the cell.
protected override SizeF ArrangeOverride(SizeF finalSize)
{
var res = base.ArrangeOverride(finalSize);
var clientRect = this.GetClientRectangle(finalSize);
var operatorRect = LayoutUtils.VAlign(this.operatorElement.DesiredSize, clientRect, ContentAlignment.MiddleLeft);
this.operatorElement.Arrange(operatorRect);
this.dropDown.Arrange(new RectangleF(clientRect.X + operatorElement.DesiredSize.Width + 2, clientRect.Y, clientRect.Width - operatorElement.DesiredSize.Width - 2, clientRect.Height));
return res;
}
As the controls inside are manually arranged, you can remove the StackLayoutPanel and leave only the LightVisualElement and RadDropDownListElement.

public class CustomDropDownFilterElement : GridFilterCellElement
{
RadDropDownListElement dropDown;
BindingList<object> filterValues = new BindingList<object>();
private string fieldName = string.Empty;
public CustomDropDownFilterElement(GridViewDataColumn column, GridRowElement row) : base(column, row)
{
this.fieldName = column.FieldName;
}
BindingContext bc = new BindingContext();
protected override void CreateChildElements()
{
base.CreateChildElements();
this.FilterOperatorText.Visibility = ElementVisibility.Hidden;
dropDown = new RadDropDownListElement();
dropDown.BindingContext = bc;
dropDown.DropDownStyle = RadDropDownStyle.DropDownList;
dropDown.SelectedIndexChanged += dropDown_SelectedIndexChanged;
this.Children.Add(dropDown);
UpdateFilterButtonVisibility(false);
}
private void dropDown_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
{
GridViewDataColumn dataColumn = this.ColumnInfo as GridViewDataColumn;
var rowInfo = this.RowInfo;
if (dataColumn.FilterDescriptor == null)
{
dataColumn.FilterDescriptor = new Telerik.WinControls.Data.FilterDescriptor(this.ColumnInfo.Name, Telerik.WinControls.Data.FilterOperator.IsEqualTo, null);
dataColumn.FilterDescriptor.IsFilterEditor = true;
}
if (e.Position > -1)
{
this.Value = dropDown.Items[e.Position].Text;
rowInfo.Tag = e.Position;
}
if (e.Position == 0)
{
dataColumn.FilterDescriptor = null;
}
}
protected override Type ThemeEffectiveType
{
get => typeof(GridFilterCellElement);
}
public override void Detach()
{
dropDown.SelectedIndexChanged -= dropDown_SelectedIndexChanged;
base.Detach();
}
protected override void SetContentCore(object value)
{
base.SetContentCore(value);
GridViewDataColumn dataColumn = this.ColumnInfo as GridViewDataColumn;
if (dropDown.DataSource == null)
{
filterValues.Add("No filter");
foreach (object distinctValue in dataColumn.DistinctValues)
{
filterValues.Add(distinctValue?.ToString());
}
dropDown.SelectedIndexChanged -= dropDown_SelectedIndexChanged;
dropDown.DataSource = filterValues;
dropDown.SelectedIndex = 0;
dropDown.SelectedIndexChanged += dropDown_SelectedIndexChanged;
}
this.DrawText = false;
this.ForeColor = Color.Transparent;
if (this.RowInfo.Tag != null && this.RowInfo.Tag.ToString() != dropDown.SelectedIndex.ToString())
{
dropDown.SelectedIndexChanged -= dropDown_SelectedIndexChanged;
//synchronize the selected option
dropDown.SelectedIndex = (int)this.RowInfo.Tag;
dropDown.Text = value?.ToString();
dropDown.SelectedIndexChanged += dropDown_SelectedIndexChanged;
}
if (dataColumn == null || dataColumn.FilterDescriptor == null)
{
dropDown.SelectedIndexChanged -= dropDown_SelectedIndexChanged;
dropDown.SelectedIndex = 0;
dropDown.SelectedIndexChanged += dropDown_SelectedIndexChanged;
}
}
//hide the default filter button and filter description
protected override void UpdateFilterButtonVisibility(bool enabled)
{
enabled = false;
base.UpdateFilterButtonVisibility(enabled);
}
protected override SizeF ArrangeOverride(SizeF finalSize)
{
var res = base.ArrangeOverride(finalSize);
var clientRect = this.GetClientRectangle(finalSize);
this.dropDown.Arrange(clientRect);
return res;
}
public override bool IsCompatible(GridViewColumn data, object context)
{
return IsFilterDropDownColumn(data, context, this.fieldName, true);
}
public static bool IsFilterDropDownColumn(GridViewColumn data, object context)
{
return IsFilterDropDownColumn(data, context, null, false);
}
private static bool IsFilterDropDownColumn(GridViewColumn data, object context, string fieldName, bool checkFieldName)
{
// some custom code related to my business logic
}
}
}