Update bindingsource position in sfdatagrid?

Hi,

are we going to see an update for sfdatagrid to update the position on the bindingsource automatically?

thanks.


5 Replies

VS Vijayarasan Sivanandham Syncfusion Team September 7, 2022 04:45 PM UTC

Hi Ehs psy,

We suspect that your requirement is to update the current position in the related binding source. By default, SfDataGrid does not change the current position in the related binding source.

However, your requirement to update the current position in the related binding source in SfDataGrid can be achieved by customizing the DrawCell event in SfDataGrid. Please refer to the below code snippet,

sfDataGrid1.DrawCell += OnDrawCell;

 

private void OnDrawCell(object sender, DrawCellEventArgs e)

{

     var dataGrid = sender as SfDataGrid;

     //here customize based on your scenario

     if (dataGrid.CurrentItem != null && dataGrid.CurrentCell != null && dataGrid.CurrentCell.RowIndex == e.RowIndex && e.ColumnIndex == dataGrid.CurrentCell.ColumnIndex )

     {               

         //here get the index of CurrentItem

         var index = bindingSource1.CurrencyManager.List.IndexOf(dataGrid.CurrentItem);

         //here set the current item index into Position of bindingSource1

         bindingSource1.Position = index;

     }

}     


Please find the sample in the attachment.

API Link: https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.SfDataGrid.html#Syncfusion_WinForms_DataGrid_SfDataGrid_DrawCell

UG Link: https://help.syncfusion.com/windowsforms/datagrid/databinding

If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further.

Regards,

Vijayarasan S


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



Attachment: SfDataGridDemo_ccc33972.zip


EP ehs psy September 8, 2022 07:51 AM UTC

Hi Vijayarasan Sivanandham ;

var index = bindingSource1.CurrencyManager.List.IndexOf(dataGrid.CurrentItem);  

that will loop through the dataset to get the index.

what if we have thousands of rows !!!!!!!!

thank you .



VS Vijayarasan Sivanandham Syncfusion Team September 9, 2022 04:28 PM UTC

Hi Ehs psy,

The provided suggestion should not cause any kind of performance issue depending on the rows count. However, you can achieve your requirement with the help of the ResolveToRecordIndex helper method shown below,

private void OnDrawCell(object sender, DrawCellEventArgs e)

{

    var dataGrid = sender as SfDataGrid;

    //here customize based on your scenario

    if (dataGrid.CurrentItem != null && dataGrid.CurrentCell != null && dataGrid.CurrentCell.RowIndex == e.RowIndex && e.ColumnIndex == dataGrid.CurrentCell.ColumnIndex )

    {

        //here get the index of CurrentItem

        //below mentioned way does not affect any performance issue depending on the rows count

        //var index = bindingSource1.CurrencyManager.List.IndexOf(dataGrid.CurrentItem);

        //here set the current item index into Position of bindingSource1

        //bindingSource1.Position = index;

 

        //here get the recordindex of the current item

        var recordIndex = dataGrid.TableControl.ResolveToRecordIndex(e.RowIndex);

        bindingSource1.Position = recordIndex;

       

    }

}     


UG Link: https://help.syncfusion.com/windowsforms/datagrid/helpers

But this way has some limitations, whenever you are grouping the records, the index gets mismatched. Because when you are grouping the records summary rows are added in SfDataGrid. In this case, it will returns the current index of record that is present in the view.

Regards,

Vijayarasan S


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



Attachment: SfDataGridDemo_cadef6b7.zip


MS Mark Sami September 24, 2023 12:01 AM UTC

I hope you will do it, it will facilitate a lot of things

Even if your solution work's it will become more difficult to deal with if your app became more complex

regards



VS Vijayarasan Sivanandham Syncfusion Team September 25, 2023 02:20 PM UTC

Hi Mark,

If you encounter any problems with the solution provided, please provide the details. We would be glad to assist you.

Regards,
Vijayarasan S


Loader.
Up arrow icon