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

button to loop on selected rows with btnnext and btnprev buttons

1 Answer 24 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Imo
Top achievements
Rank 1
Imo asked on 04 Oct 2011, 04:05 AM
hi everybody is there a way that we could loop to selected rows using two buttons button1 and button2, like for example i want the small arrow to point to the first selected row with document name f10_oct1_en.pdf, currently it is pointing to f36_may07_en.pdf, and each time i click the button it jumps to the next selected row which in my case if the arrow is on f10_oct1_en.pdf will go to f21_dec17_en.pdf, same thing will happen to the prev button only going backwards..
i have attached a png file on the thread so you could understand what i mean...
thank you very much...

1 Answer, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 05 Oct 2011, 11:11 AM
Hello Imo,

There is no clean way to do this, but this method should do what you need:
For this to work you need .net 3.5 and add using System.Linq...

private void MoveToSelectedRow(bool next)
{
    if (_radGridView.CurrentRow == null)
    {
        return;
    }
 
    if (_radGridView.SelectedRows.Count <= 1) return;
 
    var index = _radGridView.Rows.IndexOf(_radGridView.CurrentRow);
    var rows =
        _radGridView.Rows.Where(
            r => (next ? r.Index > index : r.Index < index) && _radGridView.SelectedRows.Contains(r));
    var rowToSelect = next ? rows.FirstOrDefault() : rows.LastOrDefault();
    if (rowToSelect == null)
    {
        rowToSelect = next
                          ? _radGridView.Rows.Where(r => _radGridView.SelectedRows.Contains(r)).FirstOrDefault()
                          : _radGridView.Rows.Where(r => _radGridView.SelectedRows.Contains(r)).LastOrDefault();
    }
 
    var selectedRows = _radGridView.SelectedRows.ToArray();
    _radGridView.CurrentRow = rowToSelect;
 
    foreach (var row in selectedRows)
    {
        row.IsSelected = true;
    }
}

I know it's not that clean, but it will do what you need.

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
Imo
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Share this question
or