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

Selecting first matching filtered row

4 Answers 245 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chet Musialowski
Top achievements
Rank 1
Chet Musialowski asked on 16 Feb 2011, 11:14 PM
Hello,

I have a filterable datagrid being used as a search screen by the user.  When the user begins a search, we select the entry they last selected as a starting point and then set the focus to the filter text box for the first column of the grid.

As they type, we scroll to the first matching entry by hooking the FilterChanged event by executing
this.TableElement.ScrollToRow(((Telerik.WinControls.UI.GridViewRowInfo)(this.MasterGridViewInfo.TableFilteringRow)).ViewInfo.Rows[0]);
and it seems to work well.

1. I'd like to enhance it by actually selecting the row (IsCurrent = true) and then place the focus (cursor) back to the filter text box at the end of the text so the user can keep typing without overwriting the original filter criteria.  I figured out how to do everything except placing the cursor at the end of the filter criteria text.

2. A problem did arise in using the mouse.  After entering a filter and scrolling the list to the first match, using the mouse to click on an entry in the list causes the list to scroll down the screen 1 line and a blank line appears between the filter row and the first row of results.

I am using VS 2008 version 9.0.30729.1 SP, Framework 3.5 SP1, Telerik Winforms 2010.3.10.1215

Thanks,
Chet

4 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 17 Feb 2011, 09:03 AM
Hello Chet,

Can you please post some of the code you are using here?

Maybe off topic, but If you want just to select a row you can use the GridNavigator, as for selecting the first visible row you can use the following:
var firstVisibleRow = this.radGridView1.TableElement.VisualRows.Where(r => r.RowInfo is GridViewDataRowInfo).FirstOrDefault();
if (firstVisibleRow != null)
{
    radGridView1.GridNavigator.SelectRow(firstVisibleRow.RowInfo);
}

Please provide some more details and i will try to help you achieve your goal.

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Chet Musialowski
Top achievements
Rank 1
answered on 18 Feb 2011, 11:12 PM
Thanks Emanuel,

I tried using the GridNavigator and its pretty cool.

What I did find was that as I typed in the filter, it would randomly set focus to the first matching record and anything else I typed didn't do anything (the grid is read only).

Right now, I'm using the following code to bring the first matching row to the top of the grid.
private void radGridView1_FilterChanged(object sender, GridViewCollectionChangedEventArgs e)
{
    if (((Telerik.WinControls.UI.GridViewRowInfo)(radGridView1.MasterGridViewInfo.TableFilteringRow)).ViewInfo.Rows.Count > 0)
    {
        radGridView1.TableElement.ScrollToRow(((Telerik.WinControls.UI.GridViewRowInfo)(radGridView1.MasterGridViewInfo.TableFilteringRow)).ViewInfo.Rows[0]);
    }
}
It leaves the cursor in the filter text box and brings the first matching row to the top of the grid.

Thanks for the help,
Chet
0
Jack
Telerik team
answered on 21 Feb 2011, 03:35 PM
Hello Chet,

Thank you for contacting us. I am glad to hear that you have found a solution for this issue. Could you please confirm that everything is OK? We will be glad to help further if you have questions.

Just a suggestion, you can set the cursor position by accessing the ActiveEditor property and the textbox hosted inside. Please consider the code below:
this.radGridView1.MasterView.TableFilteringRow.IsCurrent = true;
this.radGridView1.Columns[1].IsCurrent = true;
this.radGridView1.BeginEdit();
RadTextBoxEditor editor = this.radGridView1.ActiveEditor as RadTextBoxEditor;
if (editor != null)
{
    RadTextBoxEditorElement editorElement = (RadTextBoxEditorElement)editor.EditorElement;
    TextBox textBox = (TextBox)editorElement.HostedControl;
    textBox.SelectionStart = textBox.Text.Length;
    textBox.SelectionLength = 0;
}

Kind regards,
Jack
the Telerik team
0
Chet Musialowski
Top achievements
Rank 1
answered on 22 Feb 2011, 04:11 PM
Jack,

Thanks for getting back to me.  I have incorporated the information from yourself and Emanuel into my code and everything is working well.

Thanks for all the help,
Chet
Tags
GridView
Asked by
Chet Musialowski
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Chet Musialowski
Top achievements
Rank 1
Jack
Telerik team
Share this question
or