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

Select some row on filter changing

1 Answer 83 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Arunkumar Dharuman
Top achievements
Rank 1
Arunkumar Dharuman asked on 16 Nov 2010, 07:32 AM
Hi,
I'm using the RadGridView in which I always wanted to keep a row selected, in the case when the filter expression is changed, all the rows are unselected. I presume that the filtering row is selected at that time. 
So I subscribed the FilterChanged event, and at which I select the first row. The problem I'm facing is that, the filter changed event is generated for every character I type, and the focus switches to the first row.

Please help me out for finding a solution. Thanks.

Regards
ArunDhaJ
www.arundhaj.com

1 Answer, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 16 Nov 2010, 10:28 AM
Hello,

In my point of view, there are a few approaches, I've proposed a solution a while back where filtering was just performed on enter or on cell leave, you can either use that,

Or you could always check if the selected row belongs to the visible rows and set it then, please take a look at the following example, sorry it is very basic, i didn't have to much time to clean everything, but it should do what you need:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
 
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        this.radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
        radGridView1.EnableFiltering = true;
        radGridView1.FilterChanged += new GridViewCollectionChangedEventHandler(radGridView1_FilterChanged);
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
 
        var gridElements = radGridView1.RootElement.ElementTree;
        radGridView1.DataSource = new TestsCollection();
    }
 
    void radGridView1_FilterChanged(object sender, GridViewCollectionChangedEventArgs e)
    {
        var timer = new Timer();
        timer.Interval = 10;
        timer.Tick += delegate
        {
            timer.Stop();
            var row = GetRowToSelect();
            if (row != null)
            {
                row.IsSelected = true;
            }
        };
        timer.Start();
    }
 
    private GridViewRowInfo GetRowToSelect()
    {
        List<GridViewDataRowInfo> visibleRows = new List<GridViewDataRowInfo>();
        foreach (var rowElement in radGridView1.TableElement.VisualRows)
        {
            var rowInfo = rowElement.RowInfo as GridViewDataRowInfo;
            if (rowInfo != null)
            {
                visibleRows.Add(rowInfo);
            }
        }
 
        if (visibleRows.Count == 0)
        {
            return null;
        }
 
        foreach (var visibleRowInfo in visibleRows)
        {
            foreach (var selectedRowInfo in radGridView1.SelectedRows)
            {
                if (visibleRowInfo == selectedRowInfo)
                {
                    return selectedRowInfo;
                }
            }
        }
 
        return visibleRows[0];
    }
}
 
public class Test
{
    public int Id
    {
        get;
        set;
    }
 
    public string Name
    {
        get;
        set;
    }
 
    public Test(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}
 
public class TestsCollection : BindingList<Test>
{
    public TestsCollection()
    {
        for (int i = 0; i < 100; i++)
        {
            this.Add(new Test(i, "Test" + i));
        }
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
Tags
GridView
Asked by
Arunkumar Dharuman
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Share this question
or