Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > GridView > Gridview doesn't update binding source current position

Not answered Gridview doesn't update binding source current position

Feed from this thread
  • Sandi Markon avatar

    Posted on Jan 23, 2012 (permalink)

    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

    Reply

  • Julian Benkov Julian Benkov admin's avatar

    Posted on Jan 26, 2012 (permalink)

    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).

    Reply

  • Sandi Markon avatar

    Posted on Jan 26, 2012 (permalink)

    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

    Reply

  • Sandi Markon avatar

    Posted on Jan 30, 2012 (permalink)

    Hi Julian ,

    any news on that matter?

    Reply

  • Julian Benkov Julian Benkov admin's avatar

    Posted on Jan 30, 2012 (permalink)

    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).

    Attached files

    Reply

  • Sandi Markon avatar

    Posted on Jan 31, 2012 (permalink)

    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

    Reply

  • Julian Benkov Julian Benkov admin's avatar

    Posted on Feb 2, 2012 (permalink)

    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).

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > GridView > Gridview doesn't update binding source current position
Related resources for "Gridview doesn't update binding source current position"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]