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

RadGridViewComboboxColumn filter

1 Answer 46 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Szymon
Top achievements
Rank 1
Szymon asked on 01 Oct 2013, 08:26 AM
Hi!
is there any option to set filter to the cell not the whole column? I have table with two columns: Name, and refName. Name is typical textbox, and refName is combobox. I would like to have in my refName comboboxes all Names from first column except the one from the same row, eg.
Name refName
Name1 1
Name2 2
Name3 3

where 1 should contain Name2 and Name3; 2: Name1 and Name3; 3: Name1 and Name2.
I would also like to specify more filters in the future but for now this is all I need. :)

How can I achieve my goal?

Thanks in advance,
Szymon.

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Oct 2013, 08:55 AM
Hello Szymon,

Thank you for contacting Telerik Support.

In order to achieve your goal, please, have a look at the following approach:
List<string> names = new List<string>();
 
public Form1()
{
    InitializeComponent();
     
    List<MyName> objects = new List<MyName>()
    {
        new MyName("John Smith"),
        new MyName("Peter Jackson"),
        new MyName("Merry Stones"),
        new MyName("Bryan Cold"),
    };
     
    names.AddRange(GetNames(objects));
    this.radGridView1.AutoGenerateColumns = false;
 
    GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn("Name");
    radGridView1.MasterTemplate.Columns.Add(textBoxColumn);
 
    GridViewComboBoxColumn comboCol = new GridViewComboBoxColumn("Ref Name");
    comboCol.DataSource = names;
    this.radGridView1.Columns.Add(comboCol);
    this.radGridView1.DataSource = objects;
 
    this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
}
 
private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
    if (e.RowIndex > -1 && editor != null)
    {
        editor.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
        (editor.EditorElement as RadDropDownListEditorElement).DataSource =
            FilterList((editor.EditorElement as RadDropDownListEditorElement).DataSource, (e.Row.DataBoundItem as MyName).Name);
    }
}
 
private object FilterList(object dataSource, string filterName)
{
    List<string> dataNames = new List<string>();
    foreach (string name in dataSource as List<string>)
    {
        if (name != filterName)
        {
            dataNames.Add(name);
        }
    }
 
    return dataNames;
}
 
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.RowIndex > -1 && e.Column.Name == "Ref Name")
    {
        (e.CellElement as GridComboBoxCellElement).Text = (e.Row.DataBoundItem as MyName).Name;
    }
}
 
private IEnumerable<string> GetNames(List<MyName> objects)
{
    List<string> objectNames = new List<string>();
 
    foreach (MyName nameObj in objects)
    {
        objectNames.Add(nameObj.Name);
    }
 
    return objectNames;
}
 
public class MyName
{
    public string Name { get; set; }
 
    public MyName(string name)
    {
        this.Name = name;
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Szymon
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or