Gridview doesn't update binding source current position

9 posts, 0 answers
  1. Sandi Markon
    Sandi Markon avatar
    101 posts
    Member since:
    Jul 2012

    Posted 23 Jan 2012 Link to this post

    When sorting with rightclick->Descending/ascending, gridview doesn't update the current position of the binding source the first time. After that it works as it should. My sample is with 2011q2 but the behavior is the same with 2011q3.

    video:http://dl.dropbox.com/u/521104/Telerik/TelerikGrid.mp4
    sample:http://dl.dropbox.com/u/521104/Telerik/TestApp.7z
  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 26 Jan 2012 Link to this post

    Hi Sandi,

    By default the sorting operation does not change the current position in the related binding source. You can solve the issue by handling the SortingChanging and SortingChanged events of RadGridView.  The following sample demonstrates this:

    private int rowIndex = -1;
    public Form1()
    {
        m_Random = new Random();
        InitializeComponent();
        PoljubenTip();
        bindingSource1.DataSource = m_TestniPodatki;
     
        radGridView1.SortChanging += new Telerik.WinControls.UI.GridViewCollectionChangingEventHandler(radGridView1_SortChanging);
        radGridView1.SortChanged += new Telerik.WinControls.UI.GridViewCollectionChangedEventHandler(radGridView1_SortChanged);
     
    }
     
    void radGridView1_SortChanging(object sender, Telerik.WinControls.UI.GridViewCollectionChangingEventArgs e)
    {
        if (radGridView1.CurrentRow != null)
        {
            rowIndex = radGridView1.CurrentRow.Index;
        }
    }
     
    void radGridView1_SortChanged(object sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
    {
        if (rowIndex >= 0 && rowIndex < radGridView1.ChildRows.Count)
        {
            radGridView1.CurrentRow = radGridView1.ChildRows[rowIndex];
            object dataBoundItem = radGridView1.ChildRows[rowIndex].DataBoundItem;
            int index = bindingSource1.CurrencyManager.List.IndexOf(dataBoundItem);
            if (index >= 0)
            {
                bindingSource1.Position = index;
            }
        }
    }

    Do not hesitate to contact us if you have further questions or issues. 

    Kind regards,
    Julian Benkov
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

  3. Sandi Markon
    Sandi Markon avatar
    101 posts
    Member since:
    Jul 2012

    Posted 26 Jan 2012 Link to this post

    Hello Julian

    The solution you provided doesn't fix the problem. The problem we're having is that, when we sort the grid using the context menu( rightclick ->(ascending/descending)) there are some cases when the binding source position doesn't get updated when we click on an item in the grid (as seen in the video). When we click/move the second time, it works again...

    It is the same with the code you suggested.

    I think it's a bug in the gridview.

    Edit: I found the reason (it is not connected with the context menu). If you select the item in the 3rd row and then sort the column and click again in the 3rd column it should move the binding source, because it's a new element, but it doesn't because it thinks you didn't move. Will you fix this in a new release. And what's a temporary workaround?

    Best regards, Sandi
  4. Sandi Markon
    Sandi Markon avatar
    101 posts
    Member since:
    Jul 2012

    Posted 30 Jan 2012 Link to this post

    Hi Julian ,

    any news on that matter?
  5. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 30 Jan 2012 Link to this post

    Hi Sandi,

    I made a test with SortChaging and SortChaged events and did not find any issues with the position of the BindingSource. Here is the sample application used:

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
     
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
     
            public class MojTip
            {
                public string Ime { get; set; }
                public string Priimek { get; set; }
                public string Naslov { get; set; }
                public int Starost { get; set; }
                public int Cnt { get; set; }
     
                public MojTip() { }
            }
     
            private Random m_Random;
            private List<MojTip> m_TestniPodatki;
     
            public void PoljubenTip()
            {
                m_TestniPodatki = new List<MojTip>();
     
                for (int i = 0; i < 100; i++)
                {
                    MojTip item = new MojTip()
                    {
                        Ime = GetRandomIme(),
                        Priimek = GetRandomPriimek(),
                        Starost = GetRandomStarost(),
                        Cnt=i
                    };
                    m_TestniPodatki.Add(item);
                }
            }
            #region RandomFunctions
            private string GetRandomIme()
            {
                string[] strImena = { "Adam", "Marko", "Mitja", "Peter", "Miha", "Matjaž", "Emir", "Emil", "Franjo", "Amos" };
                return strImena[m_Random.Next(0, strImena.Length)];
            }
     
            private string GetRandomPriimek()
            {
                string[] strImena = { "Novak", "Horvat", "Tron", "Trom", "Linden", "Osper", "Gröyk", "How", "To", "Select", "Another", "Random", "Value", "NoIdea", "Tester" };
                return strImena[m_Random.Next(0, strImena.Length)];
            }
     
            private int GetRandomStarost()
            {
                return m_Random.Next(8, 40);
            }
            #endregion
     
            public Form1()
            {
                m_Random = new Random();
                InitializeComponent();
                PoljubenTip();
                bindingSource1.DataSource = m_TestniPodatki;
                bindingSource1.PositionChanged += new EventHandler(bindingSource1_PositionChanged);
     
                radGridView1.SortChanging += new Telerik.WinControls.UI.GridViewCollectionChangingEventHandler(radGridView1_SortChanging);
                radGridView1.SortChanged += new Telerik.WinControls.UI.GridViewCollectionChangedEventHandler(radGridView1_SortChanged);
     
            }
     
            void bindingSource1_PositionChanged(object sender, EventArgs e)
            {
                this.label1.Text = string.Format("Current pos: {0}", this.bindingSource1.Position);
            }
     
            private int rowIndex = -1;
     
            void radGridView1_SortChanging(object sender, Telerik.WinControls.UI.GridViewCollectionChangingEventArgs e)
            {
                if (radGridView1.CurrentRow != null)
                {
                    rowIndex = radGridView1.CurrentRow.Index;
                }
            }
     
            void radGridView1_SortChanged(object sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
            {
                if (rowIndex >= 0 && rowIndex < radGridView1.ChildRows.Count)
                {
                    radGridView1.CurrentRow = radGridView1.ChildRows[rowIndex];
                    object dataBoundItem = radGridView1.ChildRows[rowIndex].DataBoundItem;
                    int index = bindingSource1.CurrencyManager.List.IndexOf(dataBoundItem);
                    if (index >= 0)
                    {
                        bindingSource1.Position = index;
                    }
                }
            }
        }
    }

    Please take a look at the attached video for the result. 

    Greetings,
    Julian Benkov
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

  6. Sandi Markon
    Sandi Markon avatar
    101 posts
    Member since:
    Jul 2012

    Posted 31 Jan 2012 Link to this post

    Thanks, I had to upgrade the Telerik controls to 2011q3 (your workadound did not work with q2).

    However if I don't use your fix, the biding source still doesn't get updated if I pick the same row after sorting. Will this issue be fixed in the future? (see attached video http://dl.dropbox.com/u/521104/Telerik/telerik%20grid%20sorting2.mp4 )

    Best regards,
    Sandi
  7. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 02 Feb 2012 Link to this post

    Hello Sandi,

    I commented this logic and made a new test with the scenario and found the issue. We have logged the issue in our Public Issue Tracking System. The fix will be available in one of our next releases.  

    Thank you for the report. Your Telerik points have been updated.

    Regards,
    Julian Benkov
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

  8. Jessie
    Jessie avatar
    1 posts
    Member since:
    Aug 2013

    Posted 24 Aug 2013 Link to this post

    You may try this solution : Vbnet


    Public Class ...

    Dim BindingSourceName_intpos As Integer

    'Your subs here

    BindingSourceName.Datasource = ... (connection to db.. your query as datatable or dataset)
    'Populate Radgridview5 with data from BindingSourceName
    Radgridwiew5.datasource = BindingSourceName


    '...control Radgridview5 events binding position problem on column sort order asc , desc
    'Add to these events SortChanging and SortChanged on your Radgridview5

    Private Sub RadGridView5_SortChanging(ByVal sender As System.Object, ByVal e As _
                                                                       Telerik.WinControls.UI.GridViewCollectionChangingEventArgs) Handles _
                                                                       RadGridView5.SortChanging
           'get the current bindingsource position
           BindingSourceName_intpos = BindingSourceName.Position


    End Sub

     Private Sub RadGridView5_SortChanged(ByVal sender As System.Object, ByVal e As  _                              
                                                                       Telerik.WinControls.UI.GridViewCollectionChangedEventArgs) Handles _              
                                                                       RadGridView5.SortChanged
           'hold the bindingsource
           BindingSourceName.ResetBindings(False)
           'manually set position for the bindingsource
           BindingSourceName.Position = BindingSourceName_intpos

     End Sub

    'Q1 Q2 i hope it works for you..
  9. George
    Admin
    George avatar
    500 posts

    Posted 28 Aug 2013 Link to this post

    Hello Jessie,

    Thank you for sharing your solution with the community. I would like to mention that this issue was fixed and is working correctly in our latest release 2013 Q2 724.

    I hope someone will find this information helpful.
     
    Regards,
    George
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top