Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1336
Jump to row on key press
posted

Hi,

We have a XamDataGrid containing a list of lots of people.  The persons name appears in the first column, along with further details like e-mail address in other columns.  The grid is sorted by the first column.

We would like it so pressing 'm' (for example), jumps to the first person whose name starts with 'm'.

I was surprised I couldn't see any other posts in the forum regarding this... I can no doubt get it to work but was wondering if anyone has implemented behaviour like this already they could share?

Cheers,
Dave

Parents
No Data
Reply
  • 69686
    Verified Answer
    posted

    Hello Dave,

    You can handle the PreviewKeyDown event on the XamDataGrid and make a quick search on the primary field of the XamDataGrid. Here is a small simple example of what I have in mind :

    void xamDataGrid1_PreviewKeyDown(object sender, KeyEventArgs e)

            {

                if (xamDataGrid1.ActiveCell!=null && xamDataGrid1.ActiveCell.IsInEditMode)

                    return;

                else

                {

                    Field primeField = xamDataGrid1.DefaultFieldLayout.PrimaryField;

                    if (primeField != null)

                    {

                        var firstObject = (from dr in xamDataGrid1.Records

                                           where (dr as DataRecord).Cells[primeField].Value.ToString().StartsWith(e.Key.ToString())

                                           select dr).FirstOrDefault();

     

                        if (firstObject != null)

                        {

                            (firstObject as DataRecord).IsActive = true;

                            xamDataGrid1.BringRecordIntoView(firstObject);

                        }

                    }

                }

            }

Children