We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Obtain a non-unique list of all values in a column

Is there an available method to obtain a list of all values for a given column and have it be non-unique?

Basically, we are looking to write a non-unique version of GetFilterBarChoices (as strange as it sounds!).






7 Replies

RB Ragamathulla B Syncfusion Team September 27, 2011 04:38 AM UTC

Hi Andrew,

We regret deeply for the delay.

To achieve this, you need to implement a custom filterbar cell deriving the cellmodel / cellrenderer from the GridTableFilterBarCellModel / GridTableFilterBarCellRenderer.

The GridTableFilterBarCellModel.FillWithChoices method is overridden to remove the string "Custom" from the list.
The GridTableFilterBarCellModel.Select method is rewritten with the "Select_withoutcustom" method, which specifies the index for the selected item. You can pass the index for the "Custom" selected item as 0.
The GridTableFilterBarCellModel.ListBoxMouseUp() method is overridden to invoke the Select_withoutcustom method instead of the Select method.



public class GridTableFilterBarCellModel1: GridTableFilterBarCellModel
{
public GridTableFilterBarCellModel1(Syncfusion.Windows.Forms.Grid.GridModel gm):base(gm)
{
}

// Override FillWithChoices to remove 'Custom' option from the list.
public override void FillWithChoices(ListBox listBox, GridStyleInfo style, out bool exclusive)
{
exclusive = true;
GridTableCellStyleInfo tableStyleInfo = (GridTableCellStyleInfo) style;
object[] items = (object[]) GetFilterBarChoices(tableStyleInfo.TableCellIdentity);
listBox.Items.Clear();
if (items != null)
{
listBox.Items.Add(SelectAllText);
foreach (object item in items)
{
if (item is DBNull)
listBox.Items.Add(SelectEmptyText);
else if (item != null)
listBox.Items.Add(style.GetFormattedText(item));
}
}
}

// Customize the 'Select' method to hide the Custom option.
public void Select_WithoutCustom(GridTableCellStyleInfoIdentity tableCellIdentity, int index)
{
if (index >= 0)
{
if (index == 0)
{
ResetFilterBar(tableCellIdentity);
}
else if (index == 1)
{
SelectItem(tableCellIdentity, 0);
}
else
{
SelectItem(tableCellIdentity, index-1);
}
}
}
public override GridCellRendererBase CreateRenderer(GridControlBase control)
{
return new Cellrenderer(control, this);
}

}
public class Cellrenderer : GridTableFilterBarCellRenderer
{
public Cellrenderer(GridControlBase grid, GridCellModelBase cellModel): base(grid , cellModel)
{
}
public new Model.Form1.GridTableFilterBarCellModel1 Model
{
get
{
return (Model.Form1.GridTableFilterBarCellModel1) base.Model;
}
}

// Override ListBoxMouseUP method to call Customized Select method instead of the usual 'Select' method.
protected override void ListBoxMouseUp(object sender, MouseEventArgs e)
{
CurrentCell.CloseDropDown(PopupCloseType.Done);
GridTableCellStyleInfo tableStyleInfo = (GridTableCellStyleInfo) this.StyleInfo;
GridTableCellStyleInfoIdentity tableCellIdentity = tableStyleInfo.TableCellIdentity;
Model.Select_WithoutCustom(tableCellIdentity, this.ListBoxPart.SelectedIndex);

// Don't call base class - ignore.
SetTextBoxText(GetFilterBarText(StyleInfo), false);
}
}


Please refer to the following UG link.

http://help.syncfusion.com/ug_82/WindowsFormsUI_Grid/HowToHideTheCustomOptionInTheFilterBarDropDown1.html

Let me know if you have any further concerns.

Regards,
Ragamathullah B.



AW Andrew Whitlock October 3, 2011 02:16 PM UTC

Thank you for your reply, but I think you misunderstood my question.

We have a custom model already that removes the Custom option as you have detailed above. In the code you posted, you do this:

object[] items = (object[]) GetFilterBarChoices(tableStyleInfo.TableCellIdentity);

This line gets a unique list of all the available filter options for that column. What I am looking for is a way to get a non-unique list at this point.

For example, if my column had the values [0,0,1,2,3,4,4], I would like a function that gives me the list that also contains [0,0,1,2,3,4,4]. The default GetFilterBarChoices method would return [0,1,2,3,4] which is the list I want with the duplicates removed.

It's weird, I know, but is it possible?

Thanks,

Andrew



RB Ragamathulla B Syncfusion Team October 6, 2011 01:02 PM UTC

Hi Andrew,

We deeply regret for the delay.

You can obtain a non unique value by using 'FillWithChoices' method. The following code explains the same.

public override void FillWithChoices(ListBox listBox, GridStyleInfo style, out bool exclusive)
{
exclusive = true;
GridTableCellStyleInfo tableStyleInfo = (GridTableCellStyleInfo)style;
listBox.DataSource = null;
listBox.Items.Clear();
if (tableStyleInfo.DataSource != null)
{
Type listType = tableStyleInfo.DataSource.GetType();
if (listType.IsGenericType)
{
DataTable dt;

Type elementType = listType.GetGenericArguments()[0];
dt = new DataTable(elementType.Name + "List");

MemberInfo[] miArray = elementType.GetMembers(BindingFlags.Public | BindingFlags.Instance);
foreach (MemberInfo mi in miArray)
{
if (mi.MemberType == MemberTypes.Property)
{
PropertyInfo pi = mi as PropertyInfo;
dt.Columns.Add(pi.Name, pi.PropertyType);
}
else if (mi.MemberType == MemberTypes.Field)
{
FieldInfo fi = mi as FieldInfo;
dt.Columns.Add(fi.Name, fi.FieldType);
}
}

IList il = tableStyleInfo.DataSource as IList;
foreach (object record in il)
{
int i = 0;
object[] fieldValues = new object[dt.Columns.Count];
foreach (DataColumn c in dt.Columns)
{
MemberInfo mi = elementType.GetMember(c.ColumnName)[0];
if (mi.MemberType == MemberTypes.Property)
{
PropertyInfo pi = mi as PropertyInfo;
fieldValues[i] = pi.GetValue(record, null);
}
else if (mi.MemberType == MemberTypes.Field)
{
FieldInfo fi = mi as FieldInfo;
fieldValues[i] = fi.GetValue(record);
}
i++;
}
dt.Rows.Add(fieldValues);
listBox.DataSource = null;
}
listBox.DisplayMember = style.DisplayMember;
listBox.ValueMember = style.ValueMember;
if (string.IsNullOrEmpty(style.DisplayMember) && !string.IsNullOrEmpty(style.ValueMember))
{
listBox.DisplayMember = style.ValueMember;
}
listBox.BindingContext = this.BindingContext;


if (dt != null)
{

listBox.Items.Add(SelectAllText);
foreach (DataRow dr in dt.Rows)
{
listBox.Items.Add(dr[tableStyleInfo.DisplayMember]);
}
}
}
else
{
DataTable dtItems = tableStyleInfo.DataSource as DataTable;
if (dtItems != null)
{
listBox.Items.Add(SelectAllText);
foreach (DataRow dr in dtItems.Rows)
{
listBox.Items.Add(dr[tableStyleInfo.DisplayMember]);
}
}
}
}
}


Please let us know if you have any other concerns.

Regards,
Ragamathullah B.




AW Andrew Whitlock October 12, 2011 02:50 PM UTC

Thank you for your update.

I guess it is better to get to the heart of our defect rather that workaround ideas.

When we call the GetFilterBarChoices on our grid, we always have a "{}" (null) value returned for every column even when the column has a value in every row. This messes up logic we have to hide the (BLANK) option when it is not needed.

Is there any reason why GetFilterBarChoices would be always returning an array with "{}" (null) in it even though the column has a real value in every row?



AA Arulraj A Syncfusion Team October 20, 2011 11:35 AM UTC

Hi Andrew,

Thanks for the update.

We deeply regret for the inconvenience caused. We are unable to reproduce the reported issue in the Filter. Please let us know the following details so that we can find out the cause of the problem.
1. Essential Studio version you are using
2. Are you using the OptimizeFilterPerformance property settings?
3. DataSource as DataTable or Collection object
4. Is there any possibility to share your custom Filter classes?

Thanks for providing additional information if any.

Regards,
Arulraj A




AW Andrew Whitlock October 24, 2011 05:12 PM UTC

It's not possible to share our code.

We have come up with a workaround that we are happy with. Thanks for your time.

Andrew



RB Ragamathulla B Syncfusion Team October 25, 2011 11:50 AM UTC

Hi Andrew,

Thanks for your update.

Please do get in touch with us if you require any other assistance.

Regards,
Ragamathullah


Loader.
Live Chat Icon For mobile
Up arrow icon