We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to programmatically select record based on key

I add the record rec to the data source (happens to be the last -and only one in its page-), which is accessed through a pager. Then I want to scroll the grid to that record and select it. I have tried many things but without success. The following code simply does not find it (so it does not work). Please help.

// var record = grid.View.Records.FirstOrDefault( item => ( item.Data as Persona ).personasid == personasId );

var record = objetoCN.getRegistro( personasId );   // this routine makes the previous line essentially

if( record != null ) {

//allways finds it, so it enters the if part

pager.MoveToFirstPage();

for( int p = 1; p <= pager.PageCount; ++p ) {

                    int ndx = grid.TableControl.ResolveToRowIndex( record );

                    if( ndx >= 0 ) {

                        grid.TableControl.ScrollRows.ScrollInView( ndx );

                        grid.TableControl.UpdateScrollBars();

                        grid.CurrentItem = record;

                         grid.SelectRows( ndx, ndx );

                        grid.SelectedItem = record;

                        return;   // this code is in a routine

                    }

        if( p < pager.PageCount ) pager.MoveToNextPage();

}

Thanks in advance.

Best regards,

Socrates A Rivera R

sa.riverar@gmail.com


3 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team January 16, 2023 03:04 PM UTC

Hi Socrates,

Your requirement to move to the newly added record and select that will be achievable by using the SfDataPager.MoveToNextPage method is shown in the below code snippet,

private void button1_Click(object sender, EventArgs e)

{

    viewModel.OrdersListDetails.Add(new OrderInfo { OrderID = 100, CustomerID = "xxx", ProductName = "yyy", ContactNumber = 121345, Quantity = 12, ShipCountry = "zzz", UnitPrice = 10 });

    for (int p = 1; p <= sfDataPager1.PageCount; ++p)

    {

        var record = sfDataGrid1.View.Records.FirstOrDefault(item => (item.Data as OrderInfo).OrderID == 100);

        sfDataPager1.MoveToNextPage();

        if (record != null)

            sfDataGrid1.SelectedItem = record.Data;

    }

}


We have prepared the sample based on the above suggestion, please have a look at this.

Regards,
Dhanasekar M.

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: SfDatagridDemo_e9faa841.zip

Marked as answer

SA socrates anibal rivera rivera replied to Dhanasekar Mohanraj January 18, 2023 12:01 AM UTC

Hello  Dhanasekar,

Thank you very much for your help.

It helped me to see my mistakes. Your solution still have some problems (among others I do not always insert records at the end of recordset (nor first position in page).

I prepared a solution with your suggestions (is attached).

Thank you very much again.

Best regards,

Sócrates A Rivera R


Attachment: SfDatagridDemoBack_28b3f68d.zip


DM Dhanasekar Mohanraj Syncfusion Team January 18, 2023 01:56 PM UTC

Socrates,

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help.


Loader.
Live Chat Icon For mobile
Up arrow icon